diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2cc2614..8576666 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -79,6 +79,11 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS nvs_open("OTA",NVS_READWRITE,&otaNVS); // open OTA data namespace in NVS + nvs_get_u8(wifiNVS,"REBOOTS",&rebootCount); + rebootCount++; + nvs_set_u8(wifiNVS,"REBOOTS",rebootCount); + nvs_commit(wifiNVS); + size_t len; if(strlen(network.wifiData.ssid)){ // if setWifiCredentials was already called @@ -304,6 +309,14 @@ void Span::pollTask() { } statusLED->check(); + + if(rebootCallback && snapTime>rebootCallbackTime){ + rebootCallback(rebootCount-1); + rebootCount=0; + rebootCallback=NULL; + nvs_set_u8(wifiNVS,"REBOOTS",rebootCount); + nvs_commit(wifiNVS); + } } // poll diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 302b0c9..2fb0f97 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -213,11 +213,13 @@ class Span{ const char *sketchVersion="n/a"; // version of the sketch nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data - nvs_handle otaNVS; // handle for non-volatile storaget of OTA data + nvs_handle otaNVS; // handle for non-volatile storage of OTA data char pairingCodeCommand[12]=""; // user-specified Pairing Code - only needed if Pairing Setup Code is specified in sketch using setPairingCode() String lastClientIP="0.0.0.0"; // IP address of last client accessing device through encrypted channel boolean newCode; // flag indicating new application code has been loaded (based on keeping track of app SHA256) boolean serialInputDisabled=false; // flag indiating that serial input is disabled + uint8_t rebootCount=0; // counts number of times device was rebooted (used in optional Reboot callback) + uint32_t rebootCallbackTime; // length of time to wait (in milliseconds) before calling optional Reboot callback int connected=0; // WiFi connection status (increments upon each connect and disconnect) unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts @@ -238,6 +240,7 @@ class Span{ boolean autoStartAPEnabled=false; // enables auto start-up of Access Point when WiFi Credentials not found void (*apFunction)()=NULL; // optional function to invoke when starting Access Point void (*statusCallback)(HS_STATUS status)=NULL; // optional callback when HomeSpan status changes + void (*rebootCallback)(uint8_t)=NULL; // optional callback when device reboots WiFiServer *hapServer; // pointer to the HAP Server connection Blinker *statusLED; // indicates HomeSpan status @@ -355,6 +358,8 @@ class Span{ Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);} + Span& setRebootCallback(void (*f)(uint8_t),uint32_t t=DEFAULT_REBOOT_CALLBACK_TIME){rebootCallback=f;rebootCallbackTime=t;return(*this);} + void autoPoll(uint32_t stackSize=8192, uint32_t priority=1, uint32_t cpu=0){ // start pollTask() xTaskCreateUniversal([](void *parms){ for(;;){ diff --git a/src/Settings.h b/src/Settings.h index d60ee5d..9b6e30b 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -84,7 +84,9 @@ #define DEFAULT_WEBLOG_URL "status" // change with optional fourth argument in homeSpan.enableWebLog() -#define DEFAULT_LOW_MEM_THRESHOLD 80000 // default low watermark memory threshold that triggers warning +#define DEFAULT_LOW_MEM_THRESHOLD 80000 // default low watermark memory threshold that triggers warning + +#define DEFAULT_REBOOT_CALLBACK_TIME 5000 // default time (in milliseconds) to check for reboot callback ///////////////////////////////////////////////////// // OTA PARTITION INFO // diff --git a/src/src.ino b/src/src.ino index 2b30fe6..9e998b3 100644 --- a/src/src.ino +++ b/src/src.ino @@ -74,7 +74,7 @@ void setup() { // homeSpan.enableOTA(); homeSpan.setWifiCallback(wifiCB); - homeSpan.setWifiCallbackAll(wifiCB_ALL).setVerboseWifiReconnect(true); + homeSpan.setWifiCallbackAll(wifiCB_ALL).setVerboseWifiReconnect(true).setRebootCallback(rebootCB,10000); new SpanUserCommand('D', " - disconnect WiFi", [](const char *buf){WiFi.disconnect();}); @@ -88,8 +88,8 @@ void setup() { // homeSpan.autoPoll(); - for(int i=0;i<300;i++) - WEBLOG("Here is some text of a log file %d",i); +// for(int i=0;i<300;i++) +// WEBLOG("Here is some text of a log file %d",i); } @@ -113,3 +113,9 @@ void wifiCB(){ void wifiCB_ALL(int n){ Serial.printf("\n\n****** IN WIFI CALLBACK ALL. Count=%d *******\n\n",n); } + +////////////////////////////////////// + +void rebootCB(uint8_t count){ + Serial.printf("\n\n******* IN REBOOT CALLBACK: %d\n\n",count); +}