tidied commenting in BTCWifi.cpp

This commit is contained in:
rljonesau 2019-01-15 07:03:35 +11:00
parent 1b74359084
commit abbd0fa5ae
2 changed files with 39 additions and 28 deletions

View file

@ -41,7 +41,7 @@ bool bTxWebData = false;
const int led = 13; const int led = 13;
void handleRoot() { void handleBTCRoot() {
String s = MAIN_PAGE; //Read HTML contents String s = MAIN_PAGE; //Read HTML contents
server.send(200, "text/html", s); //Send web page server.send(200, "text/html", s); //Send web page
} }
@ -67,7 +67,7 @@ void handleReset() {
} }
void handleNotFound() { void handleBTCNotFound() {
digitalWrite(led, 1); digitalWrite(led, 1);
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
@ -91,11 +91,10 @@ void initWebServer(void) {
DebugPort.println("MDNS responder started"); DebugPort.println("MDNS responder started");
} }
server.on("/", handleRoot); server.on("/", handleBTCRoot);
server.on("/btc", handleRoot);
server.on("/wmconfig", handleWMConfig); server.on("/wmconfig", handleWMConfig);
server.on("/resetwifi",handleReset); server.on("/resetwifi",handleReset);
server.onNotFound(handleNotFound); server.onNotFound(handleBTCNotFound);
server.begin(); server.begin();
webSocket.begin(); webSocket.begin();
@ -104,14 +103,9 @@ void initWebServer(void) {
} }
void stopWebServer()
{
server.close();
webSocket.close();
}
unsigned char cVal; unsigned char cVal;
// called my main sketch loop()
bool doWebServer(void) { bool doWebServer(void) {
webSocket.loop(); webSocket.loop();
server.handleClient(); server.handleClient();

View file

@ -2,7 +2,8 @@
* This file is part of the "bluetoothheater" distribution * This file is part of the "bluetoothheater" distribution
* (https://gitlab.com/mrjones.id.au/bluetoothheater) * (https://gitlab.com/mrjones.id.au/bluetoothheater)
* *
* Copyright (C) 2018 James Clark * Copyright (C) 2019 Ray Jones
* Copyright (C) 2019 James Clark
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -39,11 +40,10 @@ void APstartedCallback(WiFiManager*);
WiFiManager wm; WiFiManager wm;
bool isPortalAP = false; bool isPortalAP = false; // true if config portal is running
bool isSTA = false; bool isSTA = false; // true if connected to an access point
int TRIG_PIN; // which pin triggers the configuration portal when set to LOW int TRIG_PIN; // pin that triggers the configuration portal when set to LOW
unsigned restartServer = 0; // set to time of portal reconfig - will cause reboot a while later
unsigned restartServer = 0;
bool initWifi(int initpin,const char *failedssid, const char *failedpassword) bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
@ -51,6 +51,7 @@ 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);
// report the MAC addresses - note individual values for STA and AP modes
uint8_t MAC[6]; uint8_t MAC[6];
esp_read_mac(MAC, ESP_MAC_WIFI_STA); esp_read_mac(MAC, ESP_MAC_WIFI_STA);
char msg[64]; char msg[64];
@ -90,7 +91,6 @@ bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
// REMOVED wm.setAPStaticIPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0)); // REMOVED wm.setAPStaticIPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
bool res = wm.autoConnect(failedssid, failedpassword); // auto generated AP name from chipid bool res = wm.autoConnect(failedssid, failedpassword); // auto generated AP name from chipid
// bool res = false;
DebugPort.print("WifiMode after autoConnect = "); DebugPort.println(WiFi.getMode()); DebugPort.print("WifiMode after autoConnect = "); DebugPort.println(WiFi.getMode());
int chnl = 1; int chnl = 1;
@ -123,16 +123,21 @@ bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
if(shouldBootIntoConfigPortal()) { if(shouldBootIntoConfigPortal()) {
DebugPort.println("Manually starting web portal"); DebugPort.println("Manually starting web portal");
wm.startWebPortal(); wm.startWebPortal();
isPortalAP = true; isPortalAP = true; // we started portal, we have to flag it!
} }
return retval; return retval;
} }
// call from main sketch loop()
void doWiFiManager() void doWiFiManager()
{ {
wm.process(); wm.process();
// handle the tail end of new credentials being saved by WiFiManager
// whilst the new connection is established OK, the web server is detached
// Reboot the ESP so the config portal's root link is removed and our's becomes active
// reboot takes palce 7.5 seconds after the config portal params are saved
if(restartServer) { if(restartServer) {
long tDelta = millis() - restartServer; long tDelta = millis() - restartServer;
if(tDelta > 7500) { if(tDelta > 7500) {
@ -141,6 +146,12 @@ void doWiFiManager()
} }
} }
// manage handling of pin to enter WiFManager config portal
// we typically use the BOOT pin for this (pins.h)
//
// Quick Press (< 1 sec) - enable config portal
// > 1 second (< 5 sec) press - disable config portal
// > 5 second press - erase credentials, enable config portal
static bool pinDown = false; static bool pinDown = false;
static long pinTime = 0; static long pinTime = 0;
if(digitalRead(TRIG_PIN) == LOW) { if(digitalRead(TRIG_PIN) == LOW) {
@ -153,40 +164,45 @@ void doWiFiManager()
pinDown = false; pinDown = false;
unsigned long tDelta = millis() - pinTime; unsigned long tDelta = millis() - pinTime;
DebugPort.print("Wifi config button tDelta = "); DebugPort.println(tDelta); DebugPort.print("Wifi config button tDelta = "); DebugPort.println(tDelta);
if(tDelta > 5000) { // > 5 second press // > 5 second press?
if(tDelta > 5000) {
prepBootIntoConfigPortal(true); // very long press - clear credentials, boot into portal prepBootIntoConfigPortal(true); // very long press - clear credentials, boot into portal
wm.resetSettings(); wm.resetSettings();
DebugPort.println("*** Clearing credentials and rebooting into portal ***"); DebugPort.println("*** Clearing credentials and rebooting into portal ***");
delay(1000); delay(1000);
ESP.restart(); ESP.restart();
} }
else if(tDelta > 1000) { // > 1 second press // > 1 second press?
else if(tDelta > 1000) {
prepBootIntoConfigPortal(false); // long press - boot into SoftAP prepBootIntoConfigPortal(false); // long press - boot into SoftAP
DebugPort.println("*** Rebooting into web server ***"); DebugPort.println("*** Rebooting into web server ***");
delay(1000); delay(1000);
ESP.restart(); ESP.restart();
} }
// > 50ms press?
else if(tDelta > 50) { else if(tDelta > 50) {
prepBootIntoConfigPortal(true); // short press - boot into Portal prepBootIntoConfigPortal(true); // short press - boot into Portal
DebugPort.println("*** Rebooting into config portal ***"); DebugPort.println("*** Rebooting into config portal ***");
delay(1000); delay(1000);
ESP.restart(); ESP.restart();
} }
// contact bounce otherwise! // consider as contact bounce if < 50ms!
} }
} }
} }
// callback is invoked by WiFiManager after new credentials are saved and verified
void saveParamsCallback() void saveParamsCallback()
{ {
restartServer = millis() | 1; // prepare to reboot in the near future - ensure non zero! restartServer = millis() | 1; // prepare to reboot in the near future - ensure non zero!
prepBootIntoConfigPortal(false); // ensure we fall back to SoftAP with our web page in future prepBootIntoConfigPortal(false); // ensure we present our web page after reboot
isPortalAP = false; isPortalAP = false; // clear CFG adornment from OLED WiFi icon
} }
// callback called if the WiFiManager started the config portal
void APstartedCallback(WiFiManager*) void APstartedCallback(WiFiManager*)
{ {
isPortalAP = true; isPortalAP = true; // will add CFG adornment to OLED WiFi icon
} }
const char* getWifiAPAddrStr() const char* getWifiAPAddrStr()
@ -219,15 +235,15 @@ bool isWifiAP()
bool isWifiSTA() bool isWifiSTA()
{ {
return isSTA; return isSTA; // true: STAtion mode link is active
} }
bool isConfigPortal() bool isConfigPortal()
{ {
return isPortalAP; return isPortalAP; // true: config portal is running
} }
// save an NV flag to determine whether config portal should run after reboot
void prepBootIntoConfigPortal(bool state) void prepBootIntoConfigPortal(bool state)
{ {
Preferences NV; Preferences NV;
@ -237,6 +253,7 @@ void prepBootIntoConfigPortal(bool state)
DebugPort.print("Setting boot config portal if WiFiManager fails = "); DebugPort.println(state); DebugPort.print("Setting boot config portal if WiFiManager fails = "); DebugPort.println(state);
} }
// test the NV flag whether the config portal should run after reboot
bool shouldBootIntoConfigPortal() bool shouldBootIntoConfigPortal()
{ {
Preferences NV; Preferences NV;