Completed Network::serialConfigure()
Next up: update and complete Network::apConfigure()
This commit is contained in:
parent
4a6c737c72
commit
123e2924a5
|
|
@ -233,14 +233,33 @@ void Span::initWifi(){
|
||||||
nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data
|
nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
statusLED.start(250);
|
|
||||||
Serial.print("Network configuration required!\nPress control button for 3 seconds to start AccessPoint or enter network data here.\n\n");
|
network.scan(); // scan for networks
|
||||||
Serial.print(">>> WiFi SSID: ");
|
|
||||||
|
|
||||||
resetPressed=0;
|
resetPressed=0;
|
||||||
int status=0;
|
int status=-1;
|
||||||
|
char key[2];
|
||||||
|
|
||||||
while(status==0){ // loop until a configuration method is chosen and completed
|
while(status<1){ // 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<network.numSSID;i++){
|
||||||
|
Serial.print(" ");
|
||||||
|
Serial.print(i+1);
|
||||||
|
Serial.print(") ");
|
||||||
|
Serial.print(network.ssidList[i]);
|
||||||
|
Serial.print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("\nType 'W' <return> to set WiFi credentials or press control button for 3 seconds to start Access Point...\n\n");
|
||||||
|
status=0;
|
||||||
|
}
|
||||||
|
|
||||||
if(!resetPressed){
|
if(!resetPressed){
|
||||||
if(!digitalRead(resetPin)){
|
if(!digitalRead(resetPin)){
|
||||||
|
|
@ -251,19 +270,18 @@ void Span::initWifi(){
|
||||||
resetPressed=0;
|
resetPressed=0;
|
||||||
} else if(millis()>resetTime){
|
} else if(millis()>resetTime){
|
||||||
statusLED.start(100,0.3,3,500);
|
statusLED.start(100,0.3,3,500);
|
||||||
// statusLED.start(2000,0.75);
|
network.apConfigure(hostName);
|
||||||
Serial.print("(skipping)\n\n");
|
|
||||||
status=network.apConfigure(hostName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Serial.available())
|
if(Serial.available() && *readSerial(key,1)=='W'){
|
||||||
status=network.serialConfigure();
|
status=network.serialConfigure()?1:-1;
|
||||||
|
}
|
||||||
|
|
||||||
} // while loop
|
} // while loop
|
||||||
|
|
||||||
Serial.println(status);
|
Serial.println(status);
|
||||||
Serial.print("'"); Serial.print(network.ssid); Serial.println("'");
|
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("'");
|
Serial.print("'"); Serial.print(network.setupCode); Serial.println("'");
|
||||||
while(1);
|
while(1);
|
||||||
|
|
||||||
|
|
|
||||||
115
src/Network.cpp
115
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<n;i++){
|
||||||
|
boolean found=false;
|
||||||
|
for(int j=0;j<numSSID;j++){
|
||||||
|
if(!strcmp(WiFi.SSID(i).c_str(),ssidList[j]))
|
||||||
|
found=true;
|
||||||
|
}
|
||||||
|
if(!found){
|
||||||
|
ssidList[numSSID]=(char *)calloc(WiFi.SSID(i).length()+1,sizeof(char));
|
||||||
|
sprintf(ssidList[numSSID],"%s",WiFi.SSID(i).c_str());
|
||||||
|
numSSID++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
|
||||||
|
boolean Network::serialConfigure(){
|
||||||
|
|
||||||
sprintf(ssid,"");
|
sprintf(ssid,"");
|
||||||
sprintf(pwd,"");
|
sprintf(pwd,"");
|
||||||
|
|
||||||
readSerial(ssid,MAX_SSID);
|
|
||||||
Serial.print(ssid);
|
|
||||||
Serial.print("\n");
|
|
||||||
|
|
||||||
while(!strlen(ssid)){
|
while(!strlen(ssid)){
|
||||||
Serial.print(">>> WiFi SSID: ");
|
Serial.print(">>> 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(ssid);
|
||||||
Serial.print("\n");
|
Serial.print("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -32,9 +56,85 @@ int Network::serialConfigure(){
|
||||||
Serial.print("\n");
|
Serial.print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print("Done");
|
homeSpan.statusLED.start(1000); // slowly blink Status LED
|
||||||
while(1);
|
|
||||||
|
|
||||||
|
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 <return>' 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();
|
timer=millis();
|
||||||
WiFi.begin(ssid,pwd);
|
WiFi.begin(ssid,pwd);
|
||||||
|
homeSpan.statusLED.start(1000);
|
||||||
|
|
||||||
responseBody+="<meta http-equiv = \"refresh\" content = \"5; url = /wifi-status\" />"
|
responseBody+="<meta http-equiv = \"refresh\" content = \"5; url = /wifi-status\" />"
|
||||||
"<p>Initiating WiFi connection to:</p><p><b>" + String(ssid) + "</p>";
|
"<p>Initiating WiFi connection to:</p><p><b>" + String(ssid) + "</p>";
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <unordered_set>
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
|
using std::unordered_set;
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
struct Network {
|
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 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
|
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; // client used for HTTP calls
|
||||||
unsigned long timer; // length of time of trying to connect to WiFi
|
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
|
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 pwd[MAX_PWD+1];
|
||||||
char setupCode[8+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 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 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 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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue