diff --git a/src/Network.cpp b/src/Network.cpp index 0fee554..9e566f7 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -8,7 +8,7 @@ void Network::configure(char *apName){ - Serial.print("WiFi Configuration required. Please enter details here of connect to Access Point: "); + Serial.print("WiFi Configuration required. Please enter details here, or connect to Access Point: "); Serial.print(apName); Serial.print("\n"); @@ -22,8 +22,8 @@ void Network::configure(char *apName){ IPAddress apIP(192, 168, 4, 1); WiFi.mode(WIFI_AP); - WiFi.softAP(apName,"homespan",1,false,1); // only allow one connection at a time - dnsServer.start(DNS_PORT, "*", apIP); + WiFi.softAP(apName,"homespan"); // start access point + dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device apServer.begin(); boolean needsConfiguration=true; @@ -82,14 +82,19 @@ void Network::configure(char *apName){ LOG2("\n------------ END BODY! ------------\n"); content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based - processRequest(body, (char *)content); // process request - + processRequest(body, (char *)content); // process request + + delay(1); + client.stop(); + + /* if(!client){ // client disconnected by server LOG1("** Disconnecting AP Client ("); LOG1(millis()/1000); LOG1(" sec)\n"); } - + */ + LOG2("\n"); } // process HAP Client @@ -104,10 +109,18 @@ void Network::configure(char *apName){ void Network::processRequest(char *body, char *formData){ + boolean restart=false; + String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n"; - String responseBody="
" - "HomeSpan Setup
HomeSpan Setup
Initiating WiFi connection to:
" + String(wifiData.ssid) + "
"; + "Initiating WiFi connection to:
" + String(wifiData.ssid) + "
"; + responseBody+="Settings saved!
Restarting HomeSpan in 2 seconds...
"; + restart=true; + } else + + if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART + responseBody+="Update canceled!
Restarting HomeSpan in 2 seconds...
"; + restart=true; + } else + if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS LOG1("In Get WiFi Status...\n"); if(WiFi.status()!=WL_CONNECTED){ responseHead+="Refresh: 5\r\n"; - responseBody+="Re-trying (" + String((millis()-timer)/1000) + "sec) connection to:
" + String(wifiData.ssid) + "
"; + responseBody+="Re-trying (" + String((millis()-timer)/1000) + " seconds) connection to:
" + String(wifiData.ssid) + "
"; + responseBody+="SUCCESS! Connected to:
" + String(wifiData.ssid) + "
"; + responseBody+="SUCCESS! Connected to:
" + String(wifiData.ssid) + "
"; + responseBody+="Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network, and (if needed) to create a Setup Code for pairing this device to HomeKit.
" - "The LED on this device should be double-blinking during this configuration.
" + "
The LED on this device should be double-blinking during this configuration.
" ""; @@ -188,6 +208,11 @@ void Network::processRequest(char *body, char *formData){ client.print(responseHead); client.print(responseBody); LOG2("------------ SENT! --------------\n"); + + if(restart){ + //delay(2000); + //ESP.restart(); + } } // processRequest diff --git a/src/Network.h b/src/Network.h index b2ac5be..d583a77 100644 --- a/src/Network.h +++ b/src/Network.h @@ -17,6 +17,7 @@ struct Network { WiFiClient client; // client used for HTTP calls unsigned long timer; // time of trying to connect to WiFi + unsigned long alarmTimeOut; // alarm time after which access point is shut down and HomeSpan is paused until reset void configure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName' void processRequest(char *body, char *formData); // process the HTTP request