From 3872d693a5cfa4c7cce411f276cc9e96c30b65ad Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 10 Oct 2020 08:22:02 -0500 Subject: [PATCH] Updated Command Mode to include timeout and for apConfigure() to have more cancel options --- src/HomeSpan.cpp | 39 ++++++++++++++++++++++++++++++++------- src/HomeSpan.h | 12 +++++++----- src/Network.cpp | 18 +++++++++++++++++- src/Settings.h | 22 +++++++++++----------- 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 1249229..9a8c909 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -213,11 +213,22 @@ void Span::commandMode(){ boolean done=false; statusLED.start(500,0.3,mode,1000); + unsigned long alarmTime=millis()+comModeLife; + while(!done){ + if(millis()>alarmTime){ + Serial.print("*** Command Mode: Timed Out ("); + Serial.print(comModeLife/1000); + Serial.print(" seconds).\n\n"); + mode=1; + done=true; + statusLED.start(LED_ALERT); + delay(1000); + } else if(controlButton.triggered(10,3000)){ if(!controlButton.longPress()){ mode++; - if(mode==4) + if(mode==5) mode=1; statusLED.start(500,0.3,mode,1000); } else { @@ -250,6 +261,10 @@ void Span::commandMode(){ processSerialCommand("U"); break; + case 4: + processSerialCommand("X"); + break; + } // switch Serial.print("*** EXITING COMMAND MODE ***\n\n"); @@ -291,6 +306,8 @@ void Span::initWifi(){ while(millis()-sTime - change Log Level to \n"); Serial.print(" S - change Setup Code to 8-digit \n"); Serial.print("\n*** End Commands ***\n\n"); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index cae7222..a92b44e 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -57,11 +57,12 @@ struct Span{ int nFatalErrors=0; // number of fatal errors in user-defined configuration String configLog="\n*** Config Log ***\n\n"; // log of configuration process, including any errors - char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing - uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED - uint8_t controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton - uint8_t logLevel=DEFAULT_LOG_LEVEL; // level for writing out log messages to serial monitor - uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections + char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing + uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED + uint8_t controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton + uint8_t logLevel=DEFAULT_LOG_LEVEL; // level for writing out log messages to serial monitor + uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections + unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations Blinker statusLED; // indicates HomeSpan status PushButton controlButton; // controls HomeSpan configuration and resets @@ -101,6 +102,7 @@ struct Span{ void setStatusPin(uint8_t pin){statusPin=pin;} // sets Status Pin void setApPassword(char *pwd){network.apPassword=pwd;} // sets Access Point Password void setApTimeout(uint16_t nSec){network.lifetime=nSec*1000;} // sets Access Point Timeout (seconds) + void setCommandTimeout(uint16_t nSec){comModeLife=nSec*1000;} // sets Command Mode Timeout (seconds) void setLogLevel(uint8_t level){logLevel=level;} // sets Log Level for log messages (0=baseline, 1=intermediate, 2=all) void setMaxConnections(uint8_t nCon){maxConnections=nCon;} // sets maximum number of simultaneous HAP connections (HAP requires devices support at least 8) }; diff --git a/src/Network.cpp b/src/Network.cpp index 11eafbe..cb944e4 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -123,6 +123,15 @@ void Network::apConfigure(char *apName){ while(1){ // loop until we get timed out (which will be accelerated if save/cancel selected) + if(homeSpan.controlButton.triggered(9999,3000)){ + Serial.print("\n*** Access Point Terminated."); + homeSpan.statusLED.start(LED_ALERT); + homeSpan.controlButton.wait(); + Serial.print(" Restarting... \n\n"); + homeSpan.statusLED.off(); + ESP.restart(); + } + if(millis()>alarmTimeOut){ WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point delay(100); @@ -134,9 +143,13 @@ void Network::apConfigure(char *apName){ Serial.print("\n*** Access Point: Timed Out ("); Serial.print(lifetime/1000); Serial.print(" seconds)."); - } else + } else { Serial.print("\n*** Access Point: Configuration Canceled."); + } Serial.print(" Restarting...\n\n"); + homeSpan.statusLED.start(LED_ALERT); + delay(1000); + homeSpan.statusLED.off(); ESP.restart(); } } @@ -317,6 +330,9 @@ void Network::processRequest(char *body, char *formData){ responseBody+="
" ""; + + responseBody+="
"; + } else if(!landingPage) diff --git a/src/Settings.h b/src/Settings.h index 68c7dda..934701d 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -12,22 +12,23 @@ ////////////////////////////////////////////////////// // DEFAULT SETTINGS // -#define DEFAULT_DISPLAY_NAME "HomeSpan Server" // change with optional second argument in homeSpan.begin() -#define DEFAULT_HOST_NAME "HomeSpan" // change with optional third argument in homeSpan.begin() -#define DEFAULT_MODEL_NAME "HomeSpan-ESP32" // change with optional fourth argument in homeSpan.begin() +#define DEFAULT_DISPLAY_NAME "HomeSpan Server" // change with optional second argument in homeSpan.begin() +#define DEFAULT_HOST_NAME "HomeSpan" // change with optional third argument in homeSpan.begin() +#define DEFAULT_MODEL_NAME "HomeSpan-ESP32" // change with optional fourth argument in homeSpan.begin() -#define DEFAULT_SETUP_CODE "46637726" // changed during network setup or with 'S' command +#define DEFAULT_SETUP_CODE "46637726" // changed during network setup or with 'S' command -#define DEFAULT_CONTROL_PIN 21 // change with homeSpan.setControlPin(pin) -#define DEFAULT_STATUS_PIN LED_BUILTIN // change with homeSpan.setStatusPin(pin) +#define DEFAULT_CONTROL_PIN 21 // change with homeSpan.setControlPin(pin) +#define DEFAULT_STATUS_PIN LED_BUILTIN // change with homeSpan.setStatusPin(pin) -#define DEFAULT_AP_PASSWORD "homespan" // change with homeSpan.setApPassword(pwd) +#define DEFAULT_AP_PASSWORD "homespan" // change with homeSpan.setApPassword(pwd) -#define DEFAULT_AP_TIMEOUT 120 // change with homeSpan.setApTimeout(nSeconds) +#define DEFAULT_AP_TIMEOUT 120 // change with homeSpan.setApTimeout(nSeconds) +#define DEFAULT_COMMAND_TIMEOUT 30 // change with homeSpan.setCommandTimeout(nSeconds) -#define DEFAULT_LOG_LEVEL 0 // change with homeSpan.setLogLevel(level) +#define DEFAULT_LOG_LEVEL 0 // change with homeSpan.setLogLevel(level) -#define DEFAULT_MAX_CONNECTIONS 8 // change with homeSpan.setMaxConnections(num); +#define DEFAULT_MAX_CONNECTIONS 8 // change with homeSpan.setMaxConnections(num); ///////////////////////////////////////////////////// @@ -37,7 +38,6 @@ #define LED_PAIRING_NEEDED 300,0.5,2,2550 // slow double-blink #define LED_ALERT 100 // rapid flashing #define LED_WIFI_CONNECTING 2000 // slow flashing -#define LED_INPUT_NEEDED 500 // medium flashing #define LED_AP_STARTED 100,0.5,2,500 // rapid double-blink #define LED_AP_CONNECTED 500,0.3,2,1000 // slow double-blink