diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2975413..28a9067 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -270,7 +270,8 @@ void Span::initWifi(){ } else if(digitalRead(resetPin)){ resetPressed=0; } else if(millis()>resetTime){ - status=network.apConfigure(hostName)?1:-1; + network.apConfigure(hostName); + status=1; } if(Serial.available() && *readSerial(key,1)=='W'){ diff --git a/src/Network.cpp b/src/Network.cpp index 206b640..28d740d 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -139,7 +139,7 @@ boolean Network::allowedCode(char *s){ /////////////////////////////// -boolean Network::apConfigure(char *apName){ +void Network::apConfigure(char *apName){ Serial.print("Starting Access Point: "); Serial.print(apName); @@ -163,16 +163,27 @@ boolean Network::apConfigure(char *apName){ dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device apServer.begin(); - int status=0; // initialize status alarmTimeOut=millis()+lifetime; // Access Point will shut down when alarmTimeOut is reached + apStatus=0; // status will be "timed out" unless changed - while(status==0){ + while(1){ // loop until we get timed out (which will be accelerated if save/cancel selected) if(millis()>alarmTimeOut){ - Serial.print("\n*** WARNING: Access Point timed Out ("); - Serial.print(lifetime/1000); - Serial.print(" seconds)\n\n"); - ESP.restart(); + WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point + delay(100); + if(apStatus==1){ + Serial.print("\n*** Access Point: Exiting and Saving Settings\n\n"); + return; + } else { + if(apStatus==0){ + Serial.print("\n*** Access Point: Timed Out ("); + Serial.print(lifetime/1000); + Serial.print(" seconds)."); + } else + Serial.print("\n*** Access Point: Configuration Canceled."); + Serial.print(" Restarting...\n\n"); + ESP.restart(); + } } dnsServer.processNextRequest(); @@ -228,27 +239,19 @@ boolean Network::apConfigure(char *apName){ content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based - status=processRequest(body, (char *)content); // process request; returns 0=continue, 1=exit and save settings, -1=cancel changes and restart + processRequest(body, (char *)content); // process request LOG2("\n"); } // process HAP Client - } // while status==0 + } // while 1 - delay(2000); - - if(status==-1) - ESP.restart(); - - return(1); } /////////////////////////////// -int Network::processRequest(char *body, char *formData){ - - int returnCode=0; +void Network::processRequest(char *body, char *formData){ String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n"; @@ -280,16 +283,25 @@ int Network::processRequest(char *body, char *formData){ } else - if(!strncmp(body,"POST /confirm-restart ",21)){ // GET CONFIRM-RESTART - responseBody+="
Settings saved!
Restarting HomeSpan in 2 seconds...
"; + if(!strncmp(body,"POST /save ",11)){ // GET SAVE getFormValue(formData,"code",setupCode,8); - // insert code to check whether setup Code allowed or not - returnCode=1; + + if(allowedCode(setupCode)){ + responseBody+="Settings saved!
Restarting HomeSpan.
Please close this window...
"; + alarmTimeOut=millis()+2000; + apStatus=1; + + } else { + responseBody+="" + "Disallowed Setup Code - too simple!
Returning to configuration page...
"; + } + } else - if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART - responseBody+="Update canceled!
Restarting HomeSpan in 2 seconds...
"; - returnCode=-1; + if(!strncmp(body,"GET /cancel ",12)){ // GET CANCEL + responseBody+="Configuration Canceled!
Restarting HomeSpan.
Please close this window...
"; + alarmTimeOut=millis()+2000; + apStatus=-1; } else if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS @@ -309,13 +321,13 @@ int Network::processRequest(char *body, char *formData){ responseBody+="SUCCESS! Connected to:
" + String(ssid) + "
"; responseBody+="You may enter new 8-digit Setup Code below, or leave blank to retain existing code.
"; - responseBody+=""; - responseBody+="