From dd6c8d904a35c90f484ef56134d30feb3b3f9268 Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 15 Sep 2020 07:00:31 -0500 Subject: [PATCH] Completed Network::apConfig() --- src/HomeSpan.cpp | 4 +-- src/Network.cpp | 88 ++++++++++++++++++++++++++++-------------------- src/Network.h | 7 ++-- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 6a41628..2975413 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -259,6 +259,7 @@ void Span::initWifi(){ Serial.print("\nType 'W' to set WiFi credentials or press control button for 3 seconds to start Access Point...\n\n"); status=0; + sprintf(key,""); } if(!resetPressed){ @@ -269,8 +270,7 @@ void Span::initWifi(){ } else if(digitalRead(resetPin)){ resetPressed=0; } else if(millis()>resetTime){ - statusLED.start(100,0.3,3,500); - network.apConfigure(hostName); + status=network.apConfigure(hostName)?1:-1; } if(Serial.available() && *readSerial(key,1)=='W'){ diff --git a/src/Network.cpp b/src/Network.cpp index cfd0ce4..206b640 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -139,7 +139,7 @@ boolean Network::allowedCode(char *s){ /////////////////////////////// -int Network::apConfigure(char *apName){ +boolean Network::apConfigure(char *apName){ Serial.print("Starting Access Point: "); Serial.print(apName); @@ -147,6 +147,8 @@ int Network::apConfigure(char *apName){ Serial.print(apPassword); Serial.print("\n"); + homeSpan.statusLED.start(100,0.5,2,500); // fast double blink + WiFiServer apServer(80); TempBuffer tempBuffer(MAX_HTTP+1); @@ -162,15 +164,15 @@ int Network::apConfigure(char *apName){ apServer.begin(); int status=0; // initialize status - alarmTimeOut=millis()+lifetime; // Access Point will stay alive until alarmTimeOut is reached + alarmTimeOut=millis()+lifetime; // Access Point will shut down when alarmTimeOut is reached while(status==0){ if(millis()>alarmTimeOut){ - Serial.print("\n*** ERROR: Access Point timed Out ("); + Serial.print("\n*** WARNING: Access Point timed Out ("); Serial.print(lifetime/1000); Serial.print(" seconds)\n\n"); - return(-1); + ESP.restart(); } dnsServer.processNextRequest(); @@ -226,7 +228,7 @@ int 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, 2=exit and cancel changes + status=processRequest(body, (char *)content); // process request; returns 0=continue, 1=exit and save settings, -1=cancel changes and restart LOG2("\n"); @@ -234,8 +236,12 @@ int Network::apConfigure(char *apName){ } // while status==0 - return(status); + delay(2000); + + if(status==-1) + ESP.restart(); + return(1); } /////////////////////////////// @@ -265,71 +271,84 @@ int Network::processRequest(char *body, char *formData){ getFormValue(formData,"network",ssid,MAX_SSID); getFormValue(formData,"pwd",pwd,MAX_PWD); - getFormValue(formData,"code",setupCode,8); timer=millis(); - WiFi.begin(ssid,pwd); homeSpan.statusLED.start(1000); - responseBody+="" + responseBody+="" "

Initiating WiFi connection to:

" + String(ssid) + "

"; - responseBody+="
"; } else - if(!strncmp(body,"GET /confirm-restart ",21)){ // GET CONFIRM-RESTART + if(!strncmp(body,"POST /confirm-restart ",21)){ // GET CONFIRM-RESTART responseBody+="

Settings saved!

Restarting HomeSpan in 2 seconds...

"; + getFormValue(formData,"code",setupCode,8); + // insert code to check whether setup Code allowed or not returnCode=1; } else - if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART + if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART responseBody+="

Update canceled!

Restarting HomeSpan in 2 seconds...

"; - returnCode=-1; + returnCode=-1; } else if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS LOG1("In Get WiFi Status...\n"); - if(WiFi.status()!=WL_CONNECTED){ + if(WiFi.status()!=WL_CONNECTED && WiFi.begin(ssid,pwd)!=WL_CONNECTED){ responseHead+="Refresh: 5\r\n"; - responseBody+="

Re-trying (" + String((millis()-timer)/1000) + " seconds) connection to:

" + String(ssid) + "

"; - responseBody+="
"; + + responseBody+="

Re-trying connection to:

" + String(ssid) + "

"; + responseBody+="

Timeout in " + String((alarmTimeOut-millis())/1000) + " seconds.

"; + responseBody+="
"; } else { - responseBody+="

SUCCESS! Connected to:

" + String(ssid) + "

"; - responseBody+="
" - "
"; + + homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink + + 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+="
"; } - } else { // LOGIN PAGE + } else - LOG1("In Captive Access...\n"); + if(!strncmp(body,"GET /landing-page ",18)){ // GET LANDING-PAGE - homeSpan.statusLED.start(500,0.3,2,1000); + LOG1("In Landing Page...\n"); - int n=WiFi.scanNetworks(); + landingPage=true; - 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.

" + homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink + + responseBody+="

Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network.

" "

The LED on this device should be double-blinking during this configuration.

" "
" "" "
" ""; - for(int i=0;i" + WiFi.SSID(i) + ""; - } + for(int i=0;i" + String(ssidList[i]) + ""; responseBody+="

" "" - "
" - "

" - "" - "
"; - + "
" + "

"; + responseBody+="
" "
"; + } else { + + if(!landingPage) + responseHead="HTTP/1.1 307 Temporary Redirect\r\nLocation: /landing-page\r\n"; } responseHead+="\r\n"; // add blank line between reponse header and body @@ -345,9 +364,6 @@ int Network::processRequest(char *body, char *formData){ client.print(responseBody); LOG2("------------ SENT! --------------\n"); - delay(1); - client.stop(); - return(returnCode); } // processRequest diff --git a/src/Network.h b/src/Network.h index 53a23c3..d41d09f 100644 --- a/src/Network.h +++ b/src/Network.h @@ -12,13 +12,14 @@ struct Network { const int MAX_HTTP=4095; // max number of bytes in HTTP message const char *apPassword="homespan"; // Access Point password (does not need to be secret - only used to ensure excrypted WiFi connection) const unsigned long lifetime=120000; // length of time (in milliseconds) to keep Access Point alive before shutting down and re-starting - + char **ssidList=NULL; int numSSID; - WiFiClient client; // client used for HTTP calls + WiFiClient client=NULL; // client used for HTTP calls unsigned long timer; // length of time of trying to connect to WiFi unsigned long alarmTimeOut; // alarm time after which access point is shut down and HomeSpan is re-started + boolean landingPage=false; // check to see if captive accerss point landing page is accessed char ssid[MAX_SSID+1]; char pwd[MAX_PWD+1]; @@ -27,7 +28,7 @@ struct Network { void scan(); // scan for WiFi networks and save only those with unique SSIDs boolean serialConfigure(); // configure homeSpan WiFi and Setup Code from Serial Monitor; return 1=save settings, 0=cancel settings boolean allowedCode(char *s); // checks if Setup Code is allowed (HAP defines a list of disallowed codes) - int apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; return 1=save connection, -1=cancel and restart + boolean apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; return 1=save settinmgs, 0=cancel settings int processRequest(char *body, char *formData); // process the HTTP request; return 0=continue, 1=save connection, -1=cancel and re-start int getFormValue(char *formData, char *tag, char *value, int maxSize); // search for 'tag' in 'formData' and copy result into 'value' up to 'maxSize' characters; returns number of characters, else -1 if 'tag' not found int badRequestError(); // return 400 error