From 123e2924a572a5dc6d3ae187ec0b39ead0b2a45f Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 12 Sep 2020 10:06:44 -0500 Subject: [PATCH] Completed Network::serialConfigure() Next up: update and complete Network::apConfigure() --- src/HomeSpan.cpp | 48 +++++++++++++------ src/Network.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++---- src/Network.h | 10 +++- 3 files changed, 151 insertions(+), 24 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 3de1e1c..6a41628 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -232,16 +232,35 @@ void Span::initWifi(){ if(!nvs_get_blob(wifiHandle,"WIFIDATA",NULL,&len)){ // if found WiFi data in NVS nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data } else { + + + network.scan(); // scan for networks + + resetPressed=0; + int status=-1; + char key[2]; + + while(status<1){ // loop until a configuration method is chosen and completed - statusLED.start(250); - Serial.print("Network configuration required!\nPress control button for 3 seconds to start AccessPoint or enter network data here.\n\n"); - Serial.print(">>> WiFi SSID: "); - - resetPressed=0; - int status=0; - - while(status==0){ // loop until a configuration method is chosen and completed + if(status==-1){ + if(WiFi.status()==WL_CONNECTED) + WiFi.disconnect(); + + Serial.print("Network configuration required! Found the following SSIDs:\n\n"); + statusLED.start(250); // rapidly blink Status LED + + for(int i=0;i to set WiFi credentials or press control button for 3 seconds to start Access Point...\n\n"); + status=0; + } + if(!resetPressed){ if(!digitalRead(resetPin)){ resetPressed=1; @@ -251,19 +270,18 @@ void Span::initWifi(){ resetPressed=0; } else if(millis()>resetTime){ statusLED.start(100,0.3,3,500); -// statusLED.start(2000,0.75); - Serial.print("(skipping)\n\n"); - status=network.apConfigure(hostName); + network.apConfigure(hostName); } - if(Serial.available()) - status=network.serialConfigure(); + if(Serial.available() && *readSerial(key,1)=='W'){ + status=network.serialConfigure()?1:-1; + } - } // while loop + } // while loop Serial.println(status); Serial.print("'"); Serial.print(network.ssid); Serial.println("'"); - Serial.print("'"); Serial.print(network.pwd); Serial.println("'"); + Serial.print("'"); Serial.print(mask(network.pwd,2)); Serial.println("'"); Serial.print("'"); Serial.print(network.setupCode); Serial.println("'"); while(1); diff --git a/src/Network.cpp b/src/Network.cpp index 94a3b37..cfd0ce4 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -9,18 +9,42 @@ using namespace Utils; /////////////////////////////// -int Network::serialConfigure(){ +void Network::scan(){ + + int n=WiFi.scanNetworks(); + + free(ssidList); + ssidList=(char **)calloc(n,sizeof(char *)); + numSSID=0; + + for(int i=0;i>> WiFi SSID: "); - readSerial(ssid,MAX_SSID); + readSerial(ssid,MAX_SSID); + if(atoi(ssid)>0 && atoi(ssid)<=numSSID){ + strcpy(ssid,ssidList[atoi(ssid)-1]); + } Serial.print(ssid); Serial.print("\n"); } @@ -32,9 +56,85 @@ int Network::serialConfigure(){ Serial.print("\n"); } - Serial.print("Done"); - while(1); + homeSpan.statusLED.start(1000); // slowly blink Status LED + while(WiFi.status()!=WL_CONNECTED){ + Serial.print("\nConnecting to: "); + Serial.print(ssid); + Serial.print("... "); + + if(WiFi.begin(ssid,pwd)!=WL_CONNECTED){ + char buf[8]=""; + Serial.print("Can't connect. Re-trying in 5 seconds (or type 'X ' to cancel)..."); + long sTime=millis(); + while(millis()-sTime<5000){ + if(Serial.available()){ + readSerial(buf,1); + if(buf[0]=='X'){ + Serial.print("Canceled!\n\n"); + return(0); + } + } + } + } + } // WiFi not yet connected + + Serial.print("Success! IP: "); + Serial.print(WiFi.localIP()); + Serial.print("\n\n"); + + homeSpan.statusLED.start(250); // rapidly blink Status LED + + boolean okay=false; + Serial.print("Specify new 8-digit Setup Code or leave blank to retain existing code...\n\n"); + + while(!okay){ + Serial.print("Setup Code: "); + sprintf(setupCode,""); + readSerial(setupCode,8); + + if(strlen(setupCode)==0){ + okay=true; + Serial.print("(skipping)\n\n"); + } else { + Serial.print(setupCode); + + int n=0; + while(setupCode[n]!='\0' && setupCode[n]>='0' && setupCode[n]<='9') + n++; + + if(n<8) + Serial.print("\n** Invalid format!\n\n"); + else if(!allowedCode(setupCode)) + Serial.print("\n** Disallowed code!\n\n"); + else { + Serial.print(" Accepted!\n\n"); + okay=true; + } + + } // if Setup Code not blank + } // while !okay + + char k[2]=""; + while(k[0]!='y' && k[0]!='n'){ + Serial.print("Confirm settings (y/n)? "); + readSerial(k,1); + Serial.print(k); + Serial.print("\n"); + } + + Serial.print("\n"); + + return(k[0]=='y'); +} + +/////////////////////////////// + +boolean Network::allowedCode(char *s){ + return( + strcmp(s,"00000000") && strcmp(s,"11111111") && strcmp(s,"22222222") && strcmp(s,"33333333") && + strcmp(s,"44444444") && strcmp(s,"55555555") && strcmp(s,"66666666") && strcmp(s,"77777777") && + strcmp(s,"88888888") && strcmp(s,"99999999") && strcmp(s,"12345678") && strcmp(s,"87654321")); } /////////////////////////////// @@ -169,6 +269,7 @@ int Network::processRequest(char *body, char *formData){ timer=millis(); WiFi.begin(ssid,pwd); + homeSpan.statusLED.start(1000); responseBody+="" "

Initiating WiFi connection to:

" + String(ssid) + "

"; diff --git a/src/Network.h b/src/Network.h index 3a1200b..53a23c3 100644 --- a/src/Network.h +++ b/src/Network.h @@ -1,7 +1,10 @@ #include +#include #include "Settings.h" +using std::unordered_set; + /////////////////////////////// struct Network { @@ -10,6 +13,9 @@ struct Network { 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 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 @@ -18,7 +24,9 @@ struct Network { char pwd[MAX_PWD+1]; char setupCode[8+1]; - int serialConfigure(); // configure homeSpan WiFi and Setup Code from Serial Monitor; return 1=save connection, -1=cancel and restart + 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 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