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(__TIME__);
|
||||||
Serial.print("\n\n");
|
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
|
nvs_flash_erase(); // erase NVS storage
|
||||||
Serial.print("** FACTORY RESET PIN LOW! ALL STORED DATA ERASED **\n** PROGRAM HALTED **\n");
|
Serial.print("** FACTORY RESET PIN LOW! ALL STORED DATA ERASED **\n");
|
||||||
while(1){
|
statusLED.start(100);
|
||||||
digitalWrite(LED_BUILTIN,HIGH);
|
delay(5000);
|
||||||
delay(100);
|
Serial.print("Re-starting...\n\n");
|
||||||
digitalWrite(LED_BUILTIN,LOW);
|
statusLED.off();
|
||||||
delay(500);
|
ESP.restart();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // begin
|
} // begin
|
||||||
|
|
@ -207,13 +206,6 @@ void Span::initWifi(){
|
||||||
|
|
||||||
Network network; // initialization of WiFi credentials and Setup Code
|
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
|
char id[18]; // create string version of Accessory ID for MDNS broadcast
|
||||||
memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes
|
memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes
|
||||||
id[17]='\0'; // add terminating null
|
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
|
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
|
if(!nvs_get_blob(wifiHandle,"WIFIDATA",NULL,&len)){ // if found WiFi data in NVS
|
||||||
nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data
|
nvs_get_blob(wifiHandle,"WIFIDATA",&network.wifiData,&len); // retrieve data
|
||||||
} else {
|
|
||||||
|
|
||||||
|
} else { // configure network and setup code
|
||||||
|
|
||||||
network.scan(); // scan for networks
|
network.scan(); // scan for networks
|
||||||
|
|
||||||
|
|
@ -280,20 +272,24 @@ void Span::initWifi(){
|
||||||
|
|
||||||
} // while loop
|
} // while loop
|
||||||
|
|
||||||
Serial.println(status);
|
Serial.print("Saving WiFi credentials for: ");
|
||||||
Serial.print("'"); Serial.print(network.ssid); Serial.println("'");
|
Serial.print(network.wifiData.ssid);
|
||||||
Serial.print("'"); Serial.print(mask(network.pwd,2)); Serial.println("'");
|
Serial.print("...\n");
|
||||||
Serial.print("'"); Serial.print(network.setupCode); Serial.println("'");
|
|
||||||
while(1);
|
|
||||||
|
|
||||||
// delay(2000); // pause while prior page is displayed
|
nvs_set_blob(wifiHandle,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
|
||||||
// WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point
|
nvs_commit(wifiHandle); // commit to NVS
|
||||||
// ESP.restart(); // re-start device
|
|
||||||
|
|
||||||
|
if(strlen(network.setupCode)){
|
||||||
|
Serial.print("Saving new Setup Code: ");
|
||||||
|
Serial.print(network.setupCode);
|
||||||
|
Serial.print("...\n");
|
||||||
|
}
|
||||||
|
|
||||||
nvs_set_blob(wifiHandle,"WIFIDATA",&wifiData,sizeof(wifiData)); // update data
|
Serial.print("\n*** Re-starting ***\n\n");
|
||||||
nvs_commit(wifiHandle); // commit to NVS
|
delay(500);
|
||||||
}
|
ESP.restart(); // re-start device
|
||||||
|
|
||||||
|
} // configure network
|
||||||
|
|
||||||
int nTries=0;
|
int nTries=0;
|
||||||
|
|
||||||
|
|
@ -301,11 +297,11 @@ void Span::initWifi(){
|
||||||
|
|
||||||
while(WiFi.status()!=WL_CONNECTED){
|
while(WiFi.status()!=WL_CONNECTED){
|
||||||
Serial.print("Connecting to: ");
|
Serial.print("Connecting to: ");
|
||||||
Serial.print(wifiData.ssid);
|
Serial.print(network.wifiData.ssid);
|
||||||
Serial.print("... ");
|
Serial.print("... ");
|
||||||
nTries++;
|
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;
|
int delayTime=nTries%6?5000:60000;
|
||||||
char buf[8]="";
|
char buf[8]="";
|
||||||
Serial.print("Can't connect. Re-trying in ");
|
Serial.print("Can't connect. Re-trying in ");
|
||||||
|
|
|
||||||
|
|
@ -36,23 +36,23 @@ void Network::scan(){
|
||||||
|
|
||||||
boolean Network::serialConfigure(){
|
boolean Network::serialConfigure(){
|
||||||
|
|
||||||
sprintf(ssid,"");
|
sprintf(wifiData.ssid,"");
|
||||||
sprintf(pwd,"");
|
sprintf(wifiData.pwd,"");
|
||||||
|
|
||||||
while(!strlen(ssid)){
|
while(!strlen(wifiData.ssid)){
|
||||||
Serial.print(">>> WiFi SSID: ");
|
Serial.print(">>> WiFi SSID: ");
|
||||||
readSerial(ssid,MAX_SSID);
|
readSerial(wifiData.ssid,MAX_SSID);
|
||||||
if(atoi(ssid)>0 && atoi(ssid)<=numSSID){
|
if(atoi(wifiData.ssid)>0 && atoi(wifiData.ssid)<=numSSID){
|
||||||
strcpy(ssid,ssidList[atoi(ssid)-1]);
|
strcpy(wifiData.ssid,ssidList[atoi(wifiData.ssid)-1]);
|
||||||
}
|
}
|
||||||
Serial.print(ssid);
|
Serial.print(wifiData.ssid);
|
||||||
Serial.print("\n");
|
Serial.print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
while(!strlen(pwd)){
|
while(!strlen(wifiData.pwd)){
|
||||||
Serial.print(">>> WiFi PASS: ");
|
Serial.print(">>> WiFi PASS: ");
|
||||||
readSerial(pwd,MAX_PWD);
|
readSerial(wifiData.pwd,MAX_PWD);
|
||||||
Serial.print(mask(pwd,2));
|
Serial.print(mask(wifiData.pwd,2));
|
||||||
Serial.print("\n");
|
Serial.print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,10 +60,10 @@ boolean Network::serialConfigure(){
|
||||||
|
|
||||||
while(WiFi.status()!=WL_CONNECTED){
|
while(WiFi.status()!=WL_CONNECTED){
|
||||||
Serial.print("\nConnecting to: ");
|
Serial.print("\nConnecting to: ");
|
||||||
Serial.print(ssid);
|
Serial.print(wifiData.ssid);
|
||||||
Serial.print("... ");
|
Serial.print("... ");
|
||||||
|
|
||||||
if(WiFi.begin(ssid,pwd)!=WL_CONNECTED){
|
if(WiFi.begin(wifiData.ssid,wifiData.pwd)!=WL_CONNECTED){
|
||||||
char buf[8]="";
|
char buf[8]="";
|
||||||
Serial.print("Can't connect. Re-trying in 5 seconds (or type 'X <return>' to cancel)...");
|
Serial.print("Can't connect. Re-trying in 5 seconds (or type 'X <return>' to cancel)...");
|
||||||
long sTime=millis();
|
long sTime=millis();
|
||||||
|
|
@ -245,6 +245,11 @@ void Network::apConfigure(char *apName){
|
||||||
|
|
||||||
} // process HAP Client
|
} // process HAP Client
|
||||||
|
|
||||||
|
if(client){
|
||||||
|
Serial.print("*** Stopping Client ***\n");
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
|
||||||
} // while 1
|
} // while 1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -272,14 +277,14 @@ void Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
LOG1("In Post Configure...\n");
|
LOG1("In Post Configure...\n");
|
||||||
|
|
||||||
getFormValue(formData,"network",ssid,MAX_SSID);
|
getFormValue(formData,"network",wifiData.ssid,MAX_SSID);
|
||||||
getFormValue(formData,"pwd",pwd,MAX_PWD);
|
getFormValue(formData,"pwd",wifiData.pwd,MAX_PWD);
|
||||||
|
|
||||||
timer=millis();
|
timer=millis();
|
||||||
homeSpan.statusLED.start(1000);
|
homeSpan.statusLED.start(1000);
|
||||||
|
|
||||||
responseBody+="<meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" />"
|
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
|
} else
|
||||||
|
|
||||||
|
|
@ -287,7 +292,7 @@ void Network::processRequest(char *body, char *formData){
|
||||||
getFormValue(formData,"code",setupCode,8);
|
getFormValue(formData,"code",setupCode,8);
|
||||||
|
|
||||||
if(allowedCode(setupCode)){
|
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;
|
alarmTimeOut=millis()+2000;
|
||||||
apStatus=1;
|
apStatus=1;
|
||||||
|
|
||||||
|
|
@ -299,7 +304,7 @@ void Network::processRequest(char *body, char *formData){
|
||||||
} else
|
} else
|
||||||
|
|
||||||
if(!strncmp(body,"GET /cancel ",12)){ // GET CANCEL
|
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;
|
alarmTimeOut=millis()+2000;
|
||||||
apStatus=-1;
|
apStatus=-1;
|
||||||
} else
|
} else
|
||||||
|
|
@ -308,17 +313,17 @@ void Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
LOG1("In Get WiFi Status...\n");
|
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";
|
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+="<p>Timeout in " + String((alarmTimeOut-millis())/1000) + " seconds.</p>";
|
||||||
responseBody+="<center><button onclick=\"document.location='/landing-page'\">Cancel</button></center>";
|
responseBody+="<center><button onclick=\"document.location='/landing-page'\">Cancel</button></center>";
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink
|
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+="<p>You may enter new 8-digit Setup Code below, or leave blank to retain existing code.</p>";
|
||||||
|
|
||||||
responseBody+="<form action=\"/save\" method=\"post\">"
|
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
|
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)
|
int apStatus; // tracks access point status (0=timed-out, -1=cancel, 1=save)
|
||||||
|
|
||||||
char ssid[MAX_SSID+1];
|
struct {
|
||||||
char pwd[MAX_PWD+1];
|
char ssid[MAX_SSID+1];
|
||||||
|
char pwd[MAX_PWD+1];
|
||||||
|
} wifiData;
|
||||||
|
|
||||||
char setupCode[8+1];
|
char setupCode[8+1];
|
||||||
|
|
||||||
void scan(); // scan for WiFi networks and save only those with unique SSIDs
|
void scan(); // scan for WiFi networks and save only those with unique SSIDs
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue