continued to build out Network()

This commit is contained in:
Gregg 2020-09-06 14:38:02 -05:00
parent 3da2b9a311
commit fd68164917
2 changed files with 48 additions and 22 deletions

View File

@ -8,7 +8,7 @@
void Network::configure(char *apName){ void Network::configure(char *apName){
Serial.print("WiFi Configuration required. Please enter details here of connect to Access Point: "); Serial.print("WiFi Configuration required. Please enter details here, or connect to Access Point: ");
Serial.print(apName); Serial.print(apName);
Serial.print("\n"); Serial.print("\n");
@ -22,8 +22,8 @@ void Network::configure(char *apName){
IPAddress apIP(192, 168, 4, 1); IPAddress apIP(192, 168, 4, 1);
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
WiFi.softAP(apName,"homespan",1,false,1); // only allow one connection at a time WiFi.softAP(apName,"homespan"); // start access point
dnsServer.start(DNS_PORT, "*", apIP); dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device
apServer.begin(); apServer.begin();
boolean needsConfiguration=true; boolean needsConfiguration=true;
@ -82,14 +82,19 @@ void Network::configure(char *apName){
LOG2("\n------------ END BODY! ------------\n"); LOG2("\n------------ END BODY! ------------\n");
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
processRequest(body, (char *)content); // process request processRequest(body, (char *)content); // process request
delay(1);
client.stop();
/*
if(!client){ // client disconnected by server if(!client){ // client disconnected by server
LOG1("** Disconnecting AP Client ("); LOG1("** Disconnecting AP Client (");
LOG1(millis()/1000); LOG1(millis()/1000);
LOG1(" sec)\n"); LOG1(" sec)\n");
} }
*/
LOG2("\n"); LOG2("\n");
} // process HAP Client } // process HAP Client
@ -104,10 +109,18 @@ void Network::configure(char *apName){
void Network::processRequest(char *body, char *formData){ void Network::processRequest(char *body, char *formData){
boolean restart=false;
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";
String responseBody="<html><head><style>p{font-size:300%; margin:25px}label{font-size:300%; margin:25px}input{font-size:250%; margin:25px}</style></head>" String responseBody="<html><head><style>"
"<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan Setup</b></p></center>"; "p{font-size:300%; margin:1em}"
"label{font-size:300%; margin:1em}"
"input{font-size:250%; margin:1em}"
"button{font-size:250%; margin:1em}"
"</style></head>"
"<body style=\"background-color:lightyellow;\">"
"<center><p><b>HomeSpan Setup</b></p></center>";
if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE
strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form
@ -125,19 +138,33 @@ void Network::processRequest(char *body, char *formData){
WiFi.begin(wifiData.ssid,wifiData.pwd); WiFi.begin(wifiData.ssid,wifiData.pwd);
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>" + String(wifiData.ssid) + "</p>"; "<p>Initiating WiFi connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
responseBody+="<center><button onclick=\"document.location='/'\">Cancel</button></center>";
} else } else
if(!strncmp(body,"GET /confirm-restart ",21)){ // GET CONFIRM-RESTART
responseBody+="<p>Settings saved!</p><p>Restarting HomeSpan in 2 seconds...</p>";
restart=true;
} else
if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART
responseBody+="<p>Update canceled!</p><p>Restarting HomeSpan in 2 seconds...</p>";
restart=true;
} else
if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS
LOG1("In Get WiFi Status...\n"); LOG1("In Get WiFi Status...\n");
if(WiFi.status()!=WL_CONNECTED){ if(WiFi.status()!=WL_CONNECTED){
responseHead+="Refresh: 5\r\n"; responseHead+="Refresh: 5\r\n";
responseBody+="<p>Re-trying (" + String((millis()-timer)/1000) + "sec) connection to:</p><p>" + String(wifiData.ssid) + "</p>"; responseBody+="<p>Re-trying (" + String((millis()-timer)/1000) + " seconds) connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
responseBody+="<center><button onclick=\"document.location='/'\">Cancel</button></center>";
} else { } else {
responseBody+="<p>SUCCESS! Connected to:</p><p>" + String(wifiData.ssid) + "</p>"; responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(wifiData.ssid) + "</p>";
responseBody+="<center><button onclick=\"document.location='/confirm-restart'\">SAVE Settings and Re-Start</button></center>"
"<center><button onclick=\"document.location='/cancel-restart'\">CANCEL Changes and Re-Start</button></center>";
} }
} else { // LOGIN PAGE } else { // LOGIN PAGE
@ -147,10 +174,10 @@ void Network::processRequest(char *body, char *formData){
int n=WiFi.scanNetworks(); int n=WiFi.scanNetworks();
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>" 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>"
"<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>"
"<input list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required maxlength=" + String(MAX_SSID) + ">" "<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<n;i++){
@ -160,17 +187,10 @@ void Network::processRequest(char *body, char *formData){
responseBody+="</datalist><br><br>" responseBody+="</datalist><br><br>"
"<label for=\"pwd\">WiFi Password:</label>" "<label for=\"pwd\">WiFi Password:</label>"
"<input type=\"password\" id=\"pwd\" name=\"pwd\" maxlength=" + String(MAX_PWD) + ">" "<center><input size=\"32\" type=\"password\" id=\"pwd\" name=\"pwd\" maxlength=" + String(MAX_PWD) + "></center>"
"<br><br>" "<br><br>"
"<label for=\"code\">Setup Code:</label>" "<label for=\"code\">Setup Code:</label>"
"<input type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\" maxlength=8>"; "<center><input size=\"32\" type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\" maxlength=8></center>";
/*
apClient.print("<p><em>");
apClient.print("This device already has a Setup Code. You may leave Setup Code blank to retain the current one, or type a new Setup Code to change.");
apClient.print("This device does not yet have a Setup Code. You must create one above.");
apClient.print("</p>");
*/
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>";
@ -188,6 +208,11 @@ void Network::processRequest(char *body, char *formData){
client.print(responseHead); client.print(responseHead);
client.print(responseBody); client.print(responseBody);
LOG2("------------ SENT! --------------\n"); LOG2("------------ SENT! --------------\n");
if(restart){
//delay(2000);
//ESP.restart();
}
} // processRequest } // processRequest

View File

@ -17,6 +17,7 @@ struct Network {
WiFiClient client; // client used for HTTP calls WiFiClient client; // client used for HTTP calls
unsigned long timer; // time of trying to connect to WiFi unsigned long timer; // time of trying to connect to WiFi
unsigned long alarmTimeOut; // alarm time after which access point is shut down and HomeSpan is paused until reset
void configure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName' void configure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'
void processRequest(char *body, char *formData); // process the HTTP request void processRequest(char *body, char *formData); // process the HTTP request