Added setApFunction(); simplified enableAutoStartAP()

Simplified enableAutoStartAP() so it no longer takes any arguments.  It now simply enables the auto launch of the WiFi Access Point at start-up if WiFi Credentials are not found.

New method setApFunction() will now be used to set an alternative method for the WiFi Access Point.  This will be called anytime 'A' is typed into the CLI, which also covers the auto-launch of the AP at start-up a well as starting it via the Control Button, since both of these functions call processCommand('A').
This commit is contained in:
Gregg 2021-06-20 15:20:56 -05:00
parent fc01e37590
commit 6d77e2559a
3 changed files with 16 additions and 13 deletions

View File

@ -158,12 +158,8 @@ void Span::poll() {
if(!strlen(network.wifiData.ssid)){ if(!strlen(network.wifiData.ssid)){
Serial.print("*** WIFI CREDENTIALS DATA NOT FOUND. "); Serial.print("*** WIFI CREDENTIALS DATA NOT FOUND. ");
if(autoStartAPEnabled){ if(autoStartAPEnabled){
if(apFunction){ Serial.print("AUTO-START OF ACCESS POINT ENABLED...\n\n");
apFunction(); processSerialCommand("A");
} else {
Serial.print("AUTO-START OF ACCESS POINT ENABLED...\n\n");
processSerialCommand("A");
}
} else { } else {
Serial.print("YOU MAY CONFIGURE BY TYPING 'W <RETURN>'.\n\n"); Serial.print("YOU MAY CONFIGURE BY TYPING 'W <RETURN>'.\n\n");
statusLED.start(LED_WIFI_NEEDED); statusLED.start(LED_WIFI_NEEDED);
@ -776,6 +772,11 @@ void Span::processSerialCommand(const char *c){
WiFi.disconnect(); WiFi.disconnect();
} }
if(apFunction){
apFunction();
return;
}
network.apConfigure(); network.apConfigure();
nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(wifiNVS); // commit to NVS nvs_commit(wifiNVS); // commit to NVS
@ -968,7 +969,7 @@ void Span::processSerialCommand(const char *c){
/////////////////////////////// ///////////////////////////////
void Span::setWifiCredentials(char *ssid, char *pwd){ void Span::setWifiCredentials(const char *ssid, const char *pwd){
sprintf(network.wifiData.ssid,"%.*s",MAX_SSID,ssid); sprintf(network.wifiData.ssid,"%.*s",MAX_SSID,ssid);
sprintf(network.wifiData.pwd,"%.*s",MAX_PWD,pwd); sprintf(network.wifiData.pwd,"%.*s",MAX_PWD,pwd);
if(wifiNVS){ // is begin() already called and wifiNVS is open if(wifiNVS){ // is begin() already called and wifiNVS is open

View File

@ -108,7 +108,7 @@ struct Span{
HapQR qrCode; // optional QR Code to use for pairing HapQR qrCode; // optional QR Code to use for pairing
const char *sketchVersion="n/a"; // version of the sketch const char *sketchVersion="n/a"; // version of the sketch
nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data
nvs_handle wifiNVS=NULL; // handle for non-volatile-storage of WiFi data nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data
boolean connected=false; // WiFi connection status boolean connected=false; // WiFi connection status
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
@ -127,7 +127,7 @@ struct Span{
boolean otaAuth; // OTA requires password when set to true boolean otaAuth; // OTA requires password when set to true
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 established
boolean autoStartAPEnabled=false; // enables auto start-up of Access Point when WiFi Credentials not found boolean autoStartAPEnabled=false; // enables auto start-up of Access Point when WiFi Credentials not found
void (*apFunction)()=NULL; // optional function to invoke when using enableAutoStartAP() void (*apFunction)()=NULL; // optional function to invoke when starting Access Point
WiFiServer *hapServer; // pointer to the HAP Server connection WiFiServer *hapServer; // pointer to the HAP Server connection
Blinker statusLED; // indicates HomeSpan status Blinker statusLED; // indicates HomeSpan status
@ -182,9 +182,10 @@ struct Span{
void setSketchVersion(const char *sVer){sketchVersion=sVer;} // set optional sketch version number void setSketchVersion(const char *sVer){sketchVersion=sVer;} // set optional sketch version number
const char *getSketchVersion(){return sketchVersion;} // get sketch version number const char *getSketchVersion(){return sketchVersion;} // get sketch version number
void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established
void setApFunction(void (*f)()){apFunction=f;} // sets an optional user-defined function to call when activating the WiFi Access Point
void enableAutoStartAP(void (*f)()=NULL){autoStartAPEnabled=true;apFunction=f;} // enables auto start-up of Access Point when WiFi Credentials not found (will call optional f, if specified) void enableAutoStartAP(){autoStartAPEnabled=true;} // enables auto start-up of Access Point when WiFi Credentials not found
void setWifiCredentials(char *ssid, char *pwd); // sets WiFi Credentials void setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials
}; };
/////////////////////////////// ///////////////////////////////

View File

@ -21,7 +21,8 @@ void setup() {
new SpanUserCommand('d',"My Description",userCom1); new SpanUserCommand('d',"My Description",userCom1);
new SpanUserCommand('d',"My second Description",userCom2); new SpanUserCommand('d',"My second Description",userCom2);
homeSpan.enableAutoStartAP(myWiFiAP); // homeSpan.enableAutoStartAP();
// homeSpan.setApFunction(myWiFiAP);
homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan"); homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan");