From 81ee9e2dbc5e19edd6d27958e01948a62957de4e Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 28 Oct 2023 17:06:40 -0500 Subject: [PATCH] Added homeSpan.setWifiCallbackAll(int n) Adds a second type of WiFi Callback that is called every time WiFi is established OR re-established after a disconnect. Passes the number of times WiFi has been connected as an argument. --- src/HomeSpan.cpp | 9 ++++++++- src/HomeSpan.h | 6 ++++-- src/src.ino | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index dac0451..874799c 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -422,8 +422,11 @@ void Span::checkConnect(){ addWebLog(true,"WiFi Connected! IP Address = %s",WiFi.localIP().toString().c_str()); - if(connected>1) // Do not initialize everything below if this is only a reconnect + if(connected>1){ // Do not initialize everything below if this is only a reconnect + if(wifiCallbackAll) + wifiCallbackAll((connected+1)/2); return; + } char id[18]; // create string version of Accessory ID for MDNS broadcast memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes @@ -530,6 +533,10 @@ void Span::checkConnect(){ if(wifiCallback) wifiCallback(); + if(wifiCallbackAll) + wifiCallbackAll((connected+1)/2); + + free(hostName); } // initWiFi diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 1c05833..f13be25 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -231,7 +231,8 @@ class Span{ unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations uint16_t tcpPortNum=DEFAULT_TCP_PORT; // port for TCP communications between HomeKit and HomeSpan char qrID[5]=""; // Setup ID used for pairing with QR Code - void (*wifiCallback)()=NULL; // optional callback function to invoke once WiFi connectivity is established + void (*wifiCallback)()=NULL; // optional callback function to invoke once WiFi connectivity is initially established + void (*wifiCallbackAll)(int)=NULL; // optional callback function to invoke every time WiFi connectivity is established or re-established void (*weblogCallback)(String &)=NULL; // optional callback function to invoke after header table in Web Log is produced void (*pairCallback)(boolean isPaired)=NULL; // optional callback function to invoke when pairing is established (true) or lost (false) boolean autoStartAPEnabled=false; // enables auto start-up of Access Point when WiFi Credentials not found @@ -321,7 +322,8 @@ class Span{ Span& setQRID(const char *id); // sets the Setup ID for optional pairing with a QR Code Span& setSketchVersion(const char *sVer){sketchVersion=sVer;return(*this);} // set optional sketch version number const char *getSketchVersion(){return sketchVersion;} // get sketch version number - Span& setWifiCallback(void (*f)()){wifiCallback=f;return(*this);} // sets an optional user-defined function to call once WiFi connectivity is established + Span& setWifiCallback(void (*f)()){wifiCallback=f;return(*this);} // sets an optional user-defined function to call once WiFi connectivity is initially established + Span& setWifiCallbackAll(void (*f)(int)){wifiCallbackAll=f;return(*this);} // sets an optional user-defined function to call every time WiFi connectivity is established or re-established Span& setPairCallback(void (*f)(boolean isPaired)){pairCallback=f;return(*this);} // sets an optional user-defined function to call when Pairing is established (true) or lost (false) Span& setApFunction(void (*f)()){apFunction=f;return(*this);} // sets an optional user-defined function to call when activating the WiFi Access Point Span& enableAutoStartAP(){autoStartAPEnabled=true;return(*this);} // enables auto start-up of Access Point when WiFi Credentials not found diff --git a/src/src.ino b/src/src.ino index 5bf602d..f21667b 100644 --- a/src/src.ino +++ b/src/src.ino @@ -49,8 +49,8 @@ struct LED_Service : Service::LightBulb { ////////////////////////////////////// void extraData(String &r){ - r+="Free RAM:" + String((double)(esp_get_free_internal_heap_size() / 1024),2) + " Kb (" + String(esp_get_free_internal_heap_size()) + " bytes)\n"; - r+="Free PSRAM:" + String((double)(esp_get_free_heap_size() / 1024 / 1024),2) + " Mb (" + String(esp_get_free_heap_size()) + " bytes)\n"; + r+="Free DRAM:" + String(esp_get_free_internal_heap_size()) + " bytes\n"; + r+="

Click Here to Access HomeSpan Repo

"; } ////////////////////////////////////// @@ -73,6 +73,11 @@ void setup() { // homeSpan.setSerialInputDisable(true); // homeSpan.enableOTA(); + homeSpan.setWifiCallback(wifiCB); + homeSpan.setWifiCallbackAll(wifiCB_ALL); + + new SpanUserCommand('D', " - disconnect WiFi", [](const char *buf){WiFi.disconnect();}); + homeSpan.begin(Category::Lighting,"HomeSpan LED"); new SpanAccessory(); @@ -88,3 +93,13 @@ void loop(){ } ////////////////////////////////////// + +void wifiCB(){ + Serial.printf("\n\n****** IN WIFI CALLBACK *******\n\n"); +} + +////////////////////////////////////// + +void wifiCB_ALL(int n){ + Serial.printf("\n\n****** IN WIFI CALLBACK ALL. Count=%d *******\n\n",n); +}