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.
This commit is contained in:
Gregg 2023-10-28 17:06:40 -05:00
parent 4de61e5914
commit 81ee9e2dbc
3 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -49,8 +49,8 @@ struct LED_Service : Service::LightBulb {
//////////////////////////////////////
void extraData(String &r){
r+="<tr><td>Free RAM:</td><td>" + String((double)(esp_get_free_internal_heap_size() / 1024),2) + " Kb (" + String(esp_get_free_internal_heap_size()) + " bytes)</td></tr>\n";
r+="<tr><td>Free PSRAM:</td><td>" + String((double)(esp_get_free_heap_size() / 1024 / 1024),2) + " Mb (" + String(esp_get_free_heap_size()) + " bytes)</td></tr>\n";
r+="<tr><td>Free DRAM:</td><td>" + String(esp_get_free_internal_heap_size()) + " bytes</td></tr>\n";
r+="</table><p><a href=\"https://github.com/HomeSpan/HomeSpan\">Click Here to Access HomeSpan Repo</a><p>";
}
//////////////////////////////////////
@ -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);
}