Updated Network:apConfigure
Improved Robustness - all functionality now completed.
This commit is contained in:
parent
dd6c8d904a
commit
d93c45c0bc
|
|
@ -270,7 +270,8 @@ void Span::initWifi(){
|
||||||
} else if(digitalRead(resetPin)){
|
} else if(digitalRead(resetPin)){
|
||||||
resetPressed=0;
|
resetPressed=0;
|
||||||
} else if(millis()>resetTime){
|
} else if(millis()>resetTime){
|
||||||
status=network.apConfigure(hostName)?1:-1;
|
network.apConfigure(hostName);
|
||||||
|
status=1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Serial.available() && *readSerial(key,1)=='W'){
|
if(Serial.available() && *readSerial(key,1)=='W'){
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,7 @@ boolean Network::allowedCode(char *s){
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
boolean Network::apConfigure(char *apName){
|
void Network::apConfigure(char *apName){
|
||||||
|
|
||||||
Serial.print("Starting Access Point: ");
|
Serial.print("Starting Access Point: ");
|
||||||
Serial.print(apName);
|
Serial.print(apName);
|
||||||
|
|
@ -163,16 +163,27 @@ boolean Network::apConfigure(char *apName){
|
||||||
dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device
|
dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device
|
||||||
apServer.begin();
|
apServer.begin();
|
||||||
|
|
||||||
int status=0; // initialize status
|
|
||||||
alarmTimeOut=millis()+lifetime; // Access Point will shut down when alarmTimeOut is reached
|
alarmTimeOut=millis()+lifetime; // Access Point will shut down when alarmTimeOut is reached
|
||||||
|
apStatus=0; // status will be "timed out" unless changed
|
||||||
|
|
||||||
while(status==0){
|
while(1){ // loop until we get timed out (which will be accelerated if save/cancel selected)
|
||||||
|
|
||||||
if(millis()>alarmTimeOut){
|
if(millis()>alarmTimeOut){
|
||||||
Serial.print("\n*** WARNING: Access Point timed Out (");
|
WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point
|
||||||
Serial.print(lifetime/1000);
|
delay(100);
|
||||||
Serial.print(" seconds)\n\n");
|
if(apStatus==1){
|
||||||
ESP.restart();
|
Serial.print("\n*** Access Point: Exiting and Saving Settings\n\n");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
if(apStatus==0){
|
||||||
|
Serial.print("\n*** Access Point: Timed Out (");
|
||||||
|
Serial.print(lifetime/1000);
|
||||||
|
Serial.print(" seconds).");
|
||||||
|
} else
|
||||||
|
Serial.print("\n*** Access Point: Configuration Canceled.");
|
||||||
|
Serial.print(" Restarting...\n\n");
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsServer.processNextRequest();
|
dnsServer.processNextRequest();
|
||||||
|
|
@ -228,27 +239,19 @@ boolean 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, -1=cancel changes and restart
|
processRequest(body, (char *)content); // process request
|
||||||
|
|
||||||
LOG2("\n");
|
LOG2("\n");
|
||||||
|
|
||||||
} // process HAP Client
|
} // process HAP Client
|
||||||
|
|
||||||
} // while status==0
|
} // while 1
|
||||||
|
|
||||||
delay(2000);
|
|
||||||
|
|
||||||
if(status==-1)
|
|
||||||
ESP.restart();
|
|
||||||
|
|
||||||
return(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
int Network::processRequest(char *body, char *formData){
|
void Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
int returnCode=0;
|
|
||||||
|
|
||||||
String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n";
|
String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n";
|
||||||
|
|
||||||
|
|
@ -280,16 +283,25 @@ int Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
} else
|
} else
|
||||||
|
|
||||||
if(!strncmp(body,"POST /confirm-restart ",21)){ // GET CONFIRM-RESTART
|
if(!strncmp(body,"POST /save ",11)){ // GET SAVE
|
||||||
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
|
||||||
getFormValue(formData,"code",setupCode,8);
|
getFormValue(formData,"code",setupCode,8);
|
||||||
// insert code to check whether setup Code allowed or not
|
|
||||||
returnCode=1;
|
if(allowedCode(setupCode)){
|
||||||
|
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan.</p><p>Please close this window...</p>";
|
||||||
|
alarmTimeOut=millis()+2000;
|
||||||
|
apStatus=1;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
responseBody+="<meta http-equiv = \"refresh\" content = \"4; url = /wifi-status\" />"
|
||||||
|
"<p><b>Disallowed Setup Code - too simple!</b></p><p>Returning to configuration page...</p>";
|
||||||
|
}
|
||||||
|
|
||||||
} else
|
} else
|
||||||
|
|
||||||
if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART
|
if(!strncmp(body,"GET /cancel ",12)){ // GET CANCEL
|
||||||
responseBody+="<p><b>Update canceled!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
responseBody+="<p><b>Configuration Canceled!</b></p><p>Restarting HomeSpan.</p><p>Please close this window...</p>";
|
||||||
returnCode=-1;
|
alarmTimeOut=millis()+2000;
|
||||||
|
apStatus=-1;
|
||||||
} else
|
} else
|
||||||
|
|
||||||
if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS
|
if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS
|
||||||
|
|
@ -309,13 +321,13 @@ int Network::processRequest(char *body, char *formData){
|
||||||
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(ssid) + "</b></p>";
|
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+="<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\">"
|
responseBody+="<form action=\"/save\" method=\"post\">"
|
||||||
"<label for=\"code\">Setup Code:</label>"
|
"<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 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>"
|
"<center><input style=\"font-size:300%\" type=\"submit\" value=\"SAVE Settings\"></center>"
|
||||||
"</form>";
|
"</form>";
|
||||||
|
|
||||||
responseBody+="<center><button style=\"font-size:300%\" onclick=\"document.location='/cancel-restart'\">CANCEL Configuration</button></center>";
|
responseBody+="<center><button style=\"font-size:300%\" onclick=\"document.location='/cancel'\">CANCEL Configuration</button></center>";
|
||||||
}
|
}
|
||||||
|
|
||||||
} else
|
} else
|
||||||
|
|
@ -345,11 +357,10 @@ int Network::processRequest(char *body, char *formData){
|
||||||
|
|
||||||
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 {
|
} else
|
||||||
|
|
||||||
if(!landingPage)
|
if(!landingPage)
|
||||||
responseHead="HTTP/1.1 307 Temporary Redirect\r\nLocation: /landing-page\r\n";
|
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
|
||||||
responseBody+="</body></html>"; // close out body and html tags
|
responseBody+="</body></html>"; // close out body and html tags
|
||||||
|
|
@ -363,8 +374,6 @@ int Network::processRequest(char *body, char *formData){
|
||||||
client.print(responseHead);
|
client.print(responseHead);
|
||||||
client.print(responseBody);
|
client.print(responseBody);
|
||||||
LOG2("------------ SENT! --------------\n");
|
LOG2("------------ SENT! --------------\n");
|
||||||
|
|
||||||
return(returnCode);
|
|
||||||
|
|
||||||
} // processRequest
|
} // processRequest
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ struct Network {
|
||||||
WiFiClient client=NULL; // 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
|
boolean landingPage=false; // check to see if captive access point landing page is accessed
|
||||||
|
int apStatus; // tracks access point status (0=timed-out, -1=cancel, 1=save)
|
||||||
|
|
||||||
char ssid[MAX_SSID+1];
|
char ssid[MAX_SSID+1];
|
||||||
char pwd[MAX_PWD+1];
|
char pwd[MAX_PWD+1];
|
||||||
|
|
@ -28,8 +29,8 @@ 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)
|
||||||
boolean apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; return 1=save settinmgs, 0=cancel settings
|
void apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; only returns if sucessful, else ESP restarts
|
||||||
int processRequest(char *body, char *formData); // process the HTTP request; return 0=continue, 1=save connection, -1=cancel and re-start
|
void processRequest(char *body, char *formData); // process the HTTP request
|
||||||
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