Completed Network::apConfig()
This commit is contained in:
parent
123e2924a5
commit
dd6c8d904a
|
|
@ -259,6 +259,7 @@ void Span::initWifi(){
|
||||||
|
|
||||||
Serial.print("\nType 'W' <return> to set WiFi credentials or press control button for 3 seconds to start Access Point...\n\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;
|
status=0;
|
||||||
|
sprintf(key,"");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!resetPressed){
|
if(!resetPressed){
|
||||||
|
|
@ -269,8 +270,7 @@ void Span::initWifi(){
|
||||||
} else if(digitalRead(resetPin)){
|
} else if(digitalRead(resetPin)){
|
||||||
resetPressed=0;
|
resetPressed=0;
|
||||||
} else if(millis()>resetTime){
|
} else if(millis()>resetTime){
|
||||||
statusLED.start(100,0.3,3,500);
|
status=network.apConfigure(hostName)?1:-1;
|
||||||
network.apConfigure(hostName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Serial.available() && *readSerial(key,1)=='W'){
|
if(Serial.available() && *readSerial(key,1)=='W'){
|
||||||
|
|
|
||||||
|
|
@ -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("Starting Access Point: ");
|
||||||
Serial.print(apName);
|
Serial.print(apName);
|
||||||
|
|
@ -147,6 +147,8 @@ int Network::apConfigure(char *apName){
|
||||||
Serial.print(apPassword);
|
Serial.print(apPassword);
|
||||||
Serial.print("\n");
|
Serial.print("\n");
|
||||||
|
|
||||||
|
homeSpan.statusLED.start(100,0.5,2,500); // fast double blink
|
||||||
|
|
||||||
WiFiServer apServer(80);
|
WiFiServer apServer(80);
|
||||||
|
|
||||||
TempBuffer <uint8_t> tempBuffer(MAX_HTTP+1);
|
TempBuffer <uint8_t> tempBuffer(MAX_HTTP+1);
|
||||||
|
|
@ -162,15 +164,15 @@ int Network::apConfigure(char *apName){
|
||||||
apServer.begin();
|
apServer.begin();
|
||||||
|
|
||||||
int status=0; // initialize status
|
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){
|
while(status==0){
|
||||||
|
|
||||||
if(millis()>alarmTimeOut){
|
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(lifetime/1000);
|
||||||
Serial.print(" seconds)\n\n");
|
Serial.print(" seconds)\n\n");
|
||||||
return(-1);
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsServer.processNextRequest();
|
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
|
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");
|
LOG2("\n");
|
||||||
|
|
||||||
|
|
@ -234,8 +236,12 @@ int Network::apConfigure(char *apName){
|
||||||
|
|
||||||
} // while status==0
|
} // while status==0
|
||||||
|
|
||||||
return(status);
|
delay(2000);
|
||||||
|
|
||||||
|
if(status==-1)
|
||||||
|
ESP.restart();
|
||||||
|
|
||||||
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
@ -265,20 +271,19 @@ int Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
getFormValue(formData,"network",ssid,MAX_SSID);
|
getFormValue(formData,"network",ssid,MAX_SSID);
|
||||||
getFormValue(formData,"pwd",pwd,MAX_PWD);
|
getFormValue(formData,"pwd",pwd,MAX_PWD);
|
||||||
getFormValue(formData,"code",setupCode,8);
|
|
||||||
|
|
||||||
timer=millis();
|
timer=millis();
|
||||||
WiFi.begin(ssid,pwd);
|
|
||||||
homeSpan.statusLED.start(1000);
|
homeSpan.statusLED.start(1000);
|
||||||
|
|
||||||
responseBody+="<meta http-equiv = \"refresh\" content = \"5; url = /wifi-status\" />"
|
responseBody+="<meta http-equiv = \"refresh\" content = \"2; 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>";
|
||||||
responseBody+="<center><button onclick=\"document.location='/'\">Cancel</button></center>";
|
|
||||||
|
|
||||||
} else
|
} else
|
||||||
|
|
||||||
if(!strncmp(body,"GET /confirm-restart ",21)){ // GET CONFIRM-RESTART
|
if(!strncmp(body,"POST /confirm-restart ",21)){ // GET CONFIRM-RESTART
|
||||||
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
||||||
|
getFormValue(formData,"code",setupCode,8);
|
||||||
|
// insert code to check whether setup Code allowed or not
|
||||||
returnCode=1;
|
returnCode=1;
|
||||||
} else
|
} else
|
||||||
|
|
||||||
|
|
@ -291,45 +296,59 @@ int Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
LOG1("In Get WiFi Status...\n");
|
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";
|
responseHead+="Refresh: 5\r\n";
|
||||||
responseBody+="<p>Re-trying (" + String((millis()-timer)/1000) + " seconds) connection to:</p><p><b>" + String(ssid) + "</p>";
|
|
||||||
responseBody+="<center><button onclick=\"document.location='/'\">Cancel</button></center>";
|
responseBody+="<p>Re-trying connection to:</p><p><b>" + String(ssid) + "</p>";
|
||||||
|
responseBody+="<p>Timeout in " + String((alarmTimeOut-millis())/1000) + " seconds.</p>";
|
||||||
|
responseBody+="<center><button onclick=\"document.location='/landing-page'\">Cancel</button></center>";
|
||||||
} else {
|
} else {
|
||||||
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(ssid) + "</p>";
|
|
||||||
responseBody+="<center><button onclick=\"document.location='/confirm-restart'\">SAVE Settings and Re-Start</button></center>"
|
homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink
|
||||||
"<center><button onclick=\"document.location='/cancel-restart'\">CANCEL Changes and Re-Start</button></center>";
|
|
||||||
|
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(ssid) + "</b></p>";
|
||||||
|
responseBody+="<p>You may enter new 8-digit Setup Code below, or leave blank to retain existing code.</p>";
|
||||||
|
|
||||||
|
responseBody+="<form action=\"/confirm-restart\" method=\"post\">"
|
||||||
|
"<label for=\"code\">Setup Code:</label>"
|
||||||
|
"<center><input size=\"32\" type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\" maxlength=8></center>"
|
||||||
|
"<center><input style=\"font-size:300%\" type=\"submit\" value=\"SAVE Settings\"></center>"
|
||||||
|
"</form>";
|
||||||
|
|
||||||
|
responseBody+="<center><button style=\"font-size:300%\" onclick=\"document.location='/cancel-restart'\">CANCEL Configuration</button></center>";
|
||||||
}
|
}
|
||||||
|
|
||||||
} 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+="<p>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.</p>"
|
homeSpan.statusLED.start(500,0.3,2,1000); // slow double-blink
|
||||||
|
|
||||||
|
responseBody+="<p>Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network.</p>"
|
||||||
"<p>The LED on this device should be <em>double-blinking</em> during this configuration.</p>"
|
"<p>The LED on this device should be <em>double-blinking</em> during this configuration.</p>"
|
||||||
"<form action=\"/configure\" method=\"post\">"
|
"<form action=\"/configure\" method=\"post\">"
|
||||||
"<label for=\"ssid\">WiFi Network:</label>"
|
"<label for=\"ssid\">WiFi Network:</label>"
|
||||||
"<center><input size=\"32\" list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required maxlength=" + String(MAX_SSID) + "></center>"
|
"<center><input size=\"32\" list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required maxlength=" + String(MAX_SSID) + "></center>"
|
||||||
"<datalist id=\"network\">";
|
"<datalist id=\"network\">";
|
||||||
|
|
||||||
for(int i=0;i<n;i++){
|
for(int i=0;i<numSSID;i++)
|
||||||
if(responseBody.indexOf(WiFi.SSID(i))==-1) // first time this SSID found
|
responseBody+="<option value=\"" + String(ssidList[i]) + "\">" + String(ssidList[i]) + "</option>";
|
||||||
responseBody+="<option value=\"" + WiFi.SSID(i) + "\">" + WiFi.SSID(i) + "</option>";
|
|
||||||
}
|
|
||||||
|
|
||||||
responseBody+="</datalist><br><br>"
|
responseBody+="</datalist><br><br>"
|
||||||
"<label for=\"pwd\">WiFi Password:</label>"
|
"<label for=\"pwd\">WiFi Password:</label>"
|
||||||
"<center><input size=\"32\" type=\"password\" id=\"pwd\" name=\"pwd\" maxlength=" + String(MAX_PWD) + "></center>"
|
"<center><input size=\"32\" type=\"password\" id=\"pwd\" name=\"pwd\" required maxlength=" + String(MAX_PWD) + "></center>"
|
||||||
"<br><br>"
|
"<br><br>";
|
||||||
"<label for=\"code\">Setup Code:</label>"
|
|
||||||
"<center><input size=\"32\" type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\" maxlength=8></center>";
|
|
||||||
|
|
||||||
responseBody+="<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>"
|
responseBody+="<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>"
|
||||||
"</form>";
|
"</form>";
|
||||||
|
} 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
|
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);
|
client.print(responseBody);
|
||||||
LOG2("------------ SENT! --------------\n");
|
LOG2("------------ SENT! --------------\n");
|
||||||
|
|
||||||
delay(1);
|
|
||||||
client.stop();
|
|
||||||
|
|
||||||
return(returnCode);
|
return(returnCode);
|
||||||
|
|
||||||
} // processRequest
|
} // processRequest
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,10 @@ struct Network {
|
||||||
char **ssidList=NULL;
|
char **ssidList=NULL;
|
||||||
int numSSID;
|
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 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
|
||||||
|
boolean landingPage=false; // check to see if captive accerss point landing page is accessed
|
||||||
|
|
||||||
char ssid[MAX_SSID+1];
|
char ssid[MAX_SSID+1];
|
||||||
char pwd[MAX_PWD+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
|
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 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)
|
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 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
|
||||||
int badRequestError(); // return 400 error
|
int badRequestError(); // return 400 error
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue