Can finally get WifiManager configportal up using the pushbutton, but only if no credentials

This commit is contained in:
rljonesau 2019-01-13 08:32:13 +11:00
parent 59d24d96b9
commit bbb225e3b7
4 changed files with 40 additions and 6 deletions

View file

@ -316,12 +316,18 @@ void setup() {
#if USE_WIFI == 1 #if USE_WIFI == 1
initWifi(WiFi_TriggerPin, FAILEDSSID, FAILEDPASSWORD); bool wifiConnected = initWifi(WiFi_TriggerPin, FAILEDSSID, FAILEDPASSWORD);
#if USE_OTA == 1 #if USE_OTA == 1
initOTA(); initOTA();
#endif // USE_OTA #endif // USE_OTA
#if USE_WEBSERVER == 1 #if USE_WEBSERVER == 1
initWebServer(); if(wifiConnected) {
DebugPort.println("Starting heater web server");
initWebServer();
}
else {
DebugPort.println("SKIPPED starting heater web server");
}
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
#endif // USE_WIFI #endif // USE_WIFI

View file

@ -52,13 +52,18 @@ void handleWMConfig() {
} }
void handleReset() { void handleReset() {
server.send(200, "text/plain", "Resetting Wifi Settings!"); server.send(200, "text/plain", "Resetting Wifi Settings!");
DebugPort.println("diconnecting client and wifi"); DebugPort.println("diconnecting client and wifi, then rebooting");
//client.disconnect(); //client.disconnect();
// wifi_station_disconnect(); // wifi_station_disconnect();
wm.disconnect(); wm.disconnect();
wm.resetSettings(); wm.resetSettings();
delay(1000);
ESP.restart(); ESP.restart();
} }
void handleNotFound() { void handleNotFound() {
@ -96,6 +101,13 @@ void initWebServer(void) {
DebugPort.println("HTTP server started"); DebugPort.println("HTTP server started");
} }
void stopWebServer()
{
server.close();
webSocket.close();
}
unsigned char cVal; unsigned char cVal;
bool doWebServer(void) { bool doWebServer(void) {

View file

@ -23,18 +23,21 @@
#include "BTCWifi.h" #include "BTCWifi.h"
#include "../Utility/DebugPort.h" #include "../Utility/DebugPort.h"
#include <DNSServer.h> #include <DNSServer.h>
#include "esp_system.h"
// select which pin will trigger the configuration portal when set to LOW // select which pin will trigger the configuration portal when set to LOW
#define FAILEDSSID "BTCESP32" #define FAILEDSSID "BTCESP32"
#define FAILEDPASSWORD "thereisnospoon" #define FAILEDPASSWORD "thereisnospoon"
WiFiManager wm; WiFiManager wm;
extern void stopWebServer();
unsigned int timeout = 120; // seconds to run for unsigned int timeout = 120; // seconds to run for
unsigned int startTime = millis(); unsigned int startTime = millis();
bool isAP = false; bool isAP = false;
bool portalRunning = false; bool portalRunning = false;
bool startCP = true; // start AP and webserver if true, else start only webserver bool startCP = false;//true; // start AP and webserver if true, else start only webserver
int TRIG_PIN; int TRIG_PIN;
@ -42,11 +45,21 @@ void configModeCallback (WiFiManager *myWiFiManager);
void saveConfigCallback (); void saveConfigCallback ();
void initWifi(int initpin,const char *failedssid, const char *failedpassword) bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
{ {
TRIG_PIN = initpin; TRIG_PIN = initpin;
pinMode(TRIG_PIN, INPUT_PULLUP); pinMode(TRIG_PIN, INPUT_PULLUP);
uint8_t MAC[6];
esp_read_mac(MAC, ESP_MAC_WIFI_STA);
char msg[64];
sprintf(msg, "STA MAC address: %02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
DebugPort.println(msg);
esp_read_mac(MAC, ESP_MAC_WIFI_SOFTAP);
sprintf(msg, "AP MAC address: %02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
DebugPort.println(msg);
//reset settings - wipe credentials for testing //reset settings - wipe credentials for testing
// wm.resetSettings(); // wm.resetSettings();
@ -71,6 +84,7 @@ void initWifi(int initpin,const char *failedssid, const char *failedpassword)
if(isAP) { if(isAP) {
WiFi.softAPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0)); WiFi.softAPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
} }
return false;
} }
else { else {
//if you get here you have connected to the WiFi //if you get here you have connected to the WiFi
@ -79,6 +93,7 @@ void initWifi(int initpin,const char *failedssid, const char *failedpassword)
DebugPort.print("IP address: "); DebugPort.print("IP address: ");
DebugPort.println(WiFi.localIP()); DebugPort.println(WiFi.localIP());
} }
return true;
} }
void doWiFiManager(){ void doWiFiManager(){
@ -101,6 +116,7 @@ void doWiFiManager(){
// is configuration portal requested? // is configuration portal requested?
// if(TRIG_PIN == 1 && (!portalRunning)) { // if(TRIG_PIN == 1 && (!portalRunning)) {
if(digitalRead(TRIG_PIN) == LOW && !portalRunning) { if(digitalRead(TRIG_PIN) == LOW && !portalRunning) {
stopWebServer();
if(startCP){ if(startCP){
DebugPort.println("Button Pressed, Starting Config Portal"); DebugPort.println("Button Pressed, Starting Config Portal");
wm.setConfigPortalBlocking(false); wm.setConfigPortalBlocking(false);

View file

@ -27,7 +27,7 @@
#include <WiFi.h> #include <WiFi.h>
void doWiFiManager(); void doWiFiManager();
void initWifi(int initpin,const char *failedssid, const char *failedpassword); bool initWifi(int initpin,const char *failedssid, const char *failedpassword);
const char* getWifiAddrStr(); const char* getWifiAddrStr();
bool isWifiConnected(); bool isWifiConnected();
bool isWifiAP(); bool isWifiAP();