From fd6816491765a6c5fd36c3c5720aea2b5e2f0ab5 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 6 Sep 2020 14:38:02 -0500 Subject: [PATCH] continued to build out Network() --- src/Network.cpp | 69 +++++++++++++++++++++++++++++++++---------------- src/Network.h | 1 + 2 files changed, 48 insertions(+), 22 deletions(-) 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

"; + String responseBody="" + "" + "

HomeSpan Setup

"; if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form @@ -125,19 +138,33 @@ void Network::processRequest(char *body, char *formData){ WiFi.begin(wifiData.ssid,wifiData.pwd); responseBody+="" - "

Initiating WiFi connection to:

" + String(wifiData.ssid) + "

"; + "

Initiating WiFi connection to:

" + String(wifiData.ssid) + "

"; + responseBody+="
"; } else + if(!strncmp(body,"GET /confirm-restart ",21)){ // GET CONFIRM-RESTART + 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+="
"; } else { - responseBody+="

SUCCESS! Connected to:

" + String(wifiData.ssid) + "

"; + responseBody+="

SUCCESS! Connected to:

" + String(wifiData.ssid) + "

"; + responseBody+="
" + "
"; } } else { // LOGIN PAGE @@ -147,10 +174,10 @@ void Network::processRequest(char *body, char *formData){ int n=WiFi.scanNetworks(); 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.

" "
" "" - "" + "
" ""; for(int i=0;iWiFi Password:" - "" + "
" "

" "" - ""; - - /* - apClient.print("

"); - apClient.print("This device already has a Setup Code. You may leave Setup Code blank to retain the current one, or type a new Setup Code to change."); - apClient.print("This device does not yet have a Setup Code. You must create one above."); - apClient.print("

"); -*/ + "
"; responseBody+="
" ""; @@ -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