Completed WiFi credentials code
WiFi data now set/resest/saved as needed. Next up: saving/resetting Setup Code.
This commit is contained in:
parent
d93c45c0bc
commit
b0307c9ab0
|
|
@ -51,15 +51,14 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo
|
|||
Serial.print(__TIME__);
|
||||
Serial.print("\n\n");
|
||||
|
||||
if(!digitalRead(resetPin)){ // factory reset pin is low
|
||||
if(!digitalRead(resetPin)){ // factory reset pin is low upon start-up
|
||||
nvs_flash_erase(); // erase NVS storage
|
||||
Serial.print("** FACTORY RESET PIN LOW! ALL STORED DATA ERASED **\n** PROGRAM HALTED **\n");
|
||||
while(1){
|
||||
digitalWrite(LED_BUILTIN,HIGH);
|
||||
delay(100);
|
||||
digitalWrite(LED_BUILTIN,LOW);
|
||||
delay(500);
|
||||
}
|
||||
Serial.print("** FACTORY RESET PIN LOW! ALL STORED DATA ERASED **\n");
|
||||
statusLED.start(100);
|
||||
delay(5000);
|
||||
Serial.print("Re-starting...\n\n");
|
||||
statusLED.off();
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
} // begin
|
||||
|
|
@ -207,13 +206,6 @@ void Span::initWifi(){
|
|||
|
||||
Network network; // initialization of WiFi credentials and Setup Code
|
||||
|
||||
struct {
|
||||
char ssid[MAX_SSID+1];
|
||||
char pwd[MAX_PWD+1];
|
||||
} wifiData;
|
||||
|
||||
char setupCode[9];
|
||||
|
||||
char id[18]; // create string version of Accessory ID for MDNS broadcast
|
||||
memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes
|
||||
id[17]='\0'; // add terminating null
|
||||
|
|
@ -230,9 +222,9 @@ void Span::initWifi(){
|
|||
nvs_open("WIFI",NVS_READWRITE,&wifiHandle); // open WIFI data namespace in NVS
|
||||
|
||||
if(!nvs_get_blob(wifiHandle,"WIFIDATA",NULL,&len)){ // if found WiFi data in NVS
|
||||
nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data
|
||||
} else {
|
||||
nvs_get_blob(wifiHandle,"WIFIDATA",&network.wifiData,&len); // retrieve data
|
||||
|
||||
} else { // configure network and setup code
|
||||
|
||||
network.scan(); // scan for networks
|
||||
|
||||
|
|
@ -280,32 +272,36 @@ void Span::initWifi(){
|
|||
|
||||
} // while loop
|
||||
|
||||
Serial.println(status);
|
||||
Serial.print("'"); Serial.print(network.ssid); Serial.println("'");
|
||||
Serial.print("'"); Serial.print(mask(network.pwd,2)); Serial.println("'");
|
||||
Serial.print("'"); Serial.print(network.setupCode); Serial.println("'");
|
||||
while(1);
|
||||
Serial.print("Saving WiFi credentials for: ");
|
||||
Serial.print(network.wifiData.ssid);
|
||||
Serial.print("...\n");
|
||||
|
||||
// delay(2000); // pause while prior page is displayed
|
||||
// WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point
|
||||
// ESP.restart(); // re-start device
|
||||
|
||||
|
||||
nvs_set_blob(wifiHandle,"WIFIDATA",&wifiData,sizeof(wifiData)); // update data
|
||||
nvs_set_blob(wifiHandle,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
|
||||
nvs_commit(wifiHandle); // commit to NVS
|
||||
|
||||
if(strlen(network.setupCode)){
|
||||
Serial.print("Saving new Setup Code: ");
|
||||
Serial.print(network.setupCode);
|
||||
Serial.print("...\n");
|
||||
}
|
||||
|
||||
Serial.print("\n*** Re-starting ***\n\n");
|
||||
delay(500);
|
||||
ESP.restart(); // re-start device
|
||||
|
||||
} // configure network
|
||||
|
||||
int nTries=0;
|
||||
|
||||
statusLED.start(1000);
|
||||
|
||||
while(WiFi.status()!=WL_CONNECTED){
|
||||
Serial.print("Connecting to: ");
|
||||
Serial.print(wifiData.ssid);
|
||||
Serial.print(network.wifiData.ssid);
|
||||
Serial.print("... ");
|
||||
nTries++;
|
||||
|
||||
if(WiFi.begin(wifiData.ssid,wifiData.pwd)!=WL_CONNECTED){
|
||||
if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){
|
||||
int delayTime=nTries%6?5000:60000;
|
||||
char buf[8]="";
|
||||
Serial.print("Can't connect. Re-trying in ");
|
||||
|
|
|
|||
|
|
@ -36,23 +36,23 @@ void Network::scan(){
|
|||
|
||||
boolean Network::serialConfigure(){
|
||||
|
||||
sprintf(ssid,"");
|
||||
sprintf(pwd,"");
|
||||
sprintf(wifiData.ssid,"");
|
||||
sprintf(wifiData.pwd,"");
|
||||
|
||||
while(!strlen(ssid)){
|
||||
while(!strlen(wifiData.ssid)){
|
||||
Serial.print(">>> WiFi SSID: ");
|
||||
readSerial(ssid,MAX_SSID);
|
||||
if(atoi(ssid)>0 && atoi(ssid)<=numSSID){
|
||||
strcpy(ssid,ssidList[atoi(ssid)-1]);
|
||||
readSerial(wifiData.ssid,MAX_SSID);
|
||||
if(atoi(wifiData.ssid)>0 && atoi(wifiData.ssid)<=numSSID){
|
||||
strcpy(wifiData.ssid,ssidList[atoi(wifiData.ssid)-1]);
|
||||
}
|
||||
Serial.print(ssid);
|
||||
Serial.print(wifiData.ssid);
|
||||
Serial.print("\n");
|
||||
}
|
||||
|
||||
while(!strlen(pwd)){
|
||||
while(!strlen(wifiData.pwd)){
|
||||
Serial.print(">>> WiFi PASS: ");
|
||||
readSerial(pwd,MAX_PWD);
|
||||
Serial.print(mask(pwd,2));
|
||||
readSerial(wifiData.pwd,MAX_PWD);
|
||||
Serial.print(mask(wifiData.pwd,2));
|
||||
Serial.print("\n");
|
||||
}
|
||||
|
||||
|
|
@ -60,10 +60,10 @@ boolean Network::serialConfigure(){
|
|||
|
||||
while(WiFi.status()!=WL_CONNECTED){
|
||||
Serial.print("\nConnecting to: ");
|
||||
Serial.print(ssid);
|
||||
Serial.print(wifiData.ssid);
|
||||
Serial.print("... ");
|
||||
|
||||
if(WiFi.begin(ssid,pwd)!=WL_CONNECTED){
|
||||
if(WiFi.begin(wifiData.ssid,wifiData.pwd)!=WL_CONNECTED){
|
||||
char buf[8]="";
|
||||
Serial.print("Can't connect. Re-trying in 5 seconds (or type 'X <return>' to cancel)...");
|
||||
long sTime=millis();
|
||||
|
|
@ -245,6 +245,11 @@ void Network::apConfigure(char *apName){
|
|||
|
||||
} // process HAP Client
|
||||
|
||||
if(client){
|
||||
Serial.print("*** Stopping Client ***\n");
|
||||
client.stop();
|
||||
}
|
||||
|
||||
} // while 1
|
||||
|
||||
}
|
||||
|
|
@ -272,14 +277,14 @@ void Network::processRequest(char *body, char *formData){
|
|||
|
||||
LOG1("In Post Configure...\n");
|
||||
|
||||
getFormValue(formData,"network",ssid,MAX_SSID);
|
||||
getFormValue(formData,"pwd",pwd,MAX_PWD);
|
||||
getFormValue(formData,"network",wifiData.ssid,MAX_SSID);
|
||||
getFormValue(formData,"pwd",wifiData.pwd,MAX_PWD);
|
||||
|
||||
timer=millis();
|
||||
homeSpan.statusLED.start(1000);
|
||||
|
||||
responseBody+="<meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" />"
|
||||
"<p>Initiating WiFi connection to:</p><p><b>" + String(ssid) + "</p>";
|
||||
"<p>Initiating WiFi connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
|
||||
|
||||
} else
|
||||
|
||||
|
|
@ -287,7 +292,7 @@ void Network::processRequest(char *body, char *formData){
|
|||
getFormValue(formData,"code",setupCode,8);
|
||||
|
||||
if(allowedCode(setupCode)){
|
||||
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan.</p><p>Please close this window...</p>";
|
||||
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan.</p><p>Closing window...</p>";
|
||||
alarmTimeOut=millis()+2000;
|
||||
apStatus=1;
|
||||
|
||||
|
|
@ -299,7 +304,7 @@ void Network::processRequest(char *body, char *formData){
|
|||
} else
|
||||
|
||||
if(!strncmp(body,"GET /cancel ",12)){ // GET CANCEL
|
||||
responseBody+="<p><b>Configuration Canceled!</b></p><p>Restarting HomeSpan.</p><p>Please close this window...</p>";
|
||||
responseBody+="<p><b>Configuration Canceled!</b></p><p>Restarting HomeSpan.</p><p>Closing window...</p>";
|
||||
alarmTimeOut=millis()+2000;
|
||||
apStatus=-1;
|
||||
} else
|
||||
|
|
@ -308,17 +313,17 @@ void Network::processRequest(char *body, char *formData){
|
|||
|
||||
LOG1("In Get WiFi Status...\n");
|
||||
|
||||
if(WiFi.status()!=WL_CONNECTED && WiFi.begin(ssid,pwd)!=WL_CONNECTED){
|
||||
if(WiFi.status()!=WL_CONNECTED && WiFi.begin(wifiData.ssid,wifiData.pwd)!=WL_CONNECTED){
|
||||
responseHead+="Refresh: 5\r\n";
|
||||
|
||||
responseBody+="<p>Re-trying connection to:</p><p><b>" + String(ssid) + "</p>";
|
||||
responseBody+="<p>Re-trying connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
|
||||
responseBody+="<p>Timeout in " + String((alarmTimeOut-millis())/1000) + " seconds.</p>";
|
||||
responseBody+="<center><button onclick=\"document.location='/landing-page'\">Cancel</button></center>";
|
||||
} else {
|
||||
|
||||
homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink
|
||||
|
||||
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(ssid) + "</b></p>";
|
||||
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(wifiData.ssid) + "</b></p>";
|
||||
responseBody+="<p>You may enter new 8-digit Setup Code below, or leave blank to retain existing code.</p>";
|
||||
|
||||
responseBody+="<form action=\"/save\" method=\"post\">"
|
||||
|
|
|
|||
|
|
@ -22,8 +22,11 @@ struct Network {
|
|||
boolean landingPage=false; // check to see if captive access point landing page is accessed
|
||||
int apStatus; // tracks access point status (0=timed-out, -1=cancel, 1=save)
|
||||
|
||||
struct {
|
||||
char ssid[MAX_SSID+1];
|
||||
char pwd[MAX_PWD+1];
|
||||
} wifiData;
|
||||
|
||||
char setupCode[8+1];
|
||||
|
||||
void scan(); // scan for WiFi networks and save only those with unique SSIDs
|
||||
|
|
|
|||
Loading…
Reference in New Issue