cleaning up Configure()

TO DO: Add max length to other form inputs;  try limiting number of connections to exactly 1.  Next:  parse form data and initiate wifi connection.
This commit is contained in:
Gregg 2020-09-04 07:27:50 -05:00
parent 4e2c28bcf8
commit a5e0b1c0ad
3 changed files with 38 additions and 38 deletions

View File

@ -6,11 +6,10 @@
void Configure::processRequest(WiFiClient &client, char *body, char *formData){
String s;
String header;
header+="<html><head><style>p{font-size:300%; margin:25px}label{font-size:300%; margin:25px}input{font-size:250%; margin:25px}</style></head>";
header+="<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan Setup</b></p></center>";
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>"
"<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan Setup</b></p></center>";
if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE
strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form
@ -20,9 +19,8 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
LOG1("In Post Configure...\n");
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n" + header;
s+="<meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" />";
s+="<p>Initiating WiFi Connection...</p></body></html>";
responseBody+="<meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" />"
"<p>Initiating WiFi Connection...</p>";
} else
@ -30,9 +28,8 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
LOG1("In Get WiFi Status...\n");
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\nRefresh: 5\r\n\r\n" + header;
s+="<p>Trying to Connect (";
s+=String(millis()/1000) + "sec)...</p></body></html>";
responseHead+="Refresh: 5\r\n";
responseBody+="<p>Trying to Connect (" + String(millis()/1000) + "sec)...</p>";
} else { // LOGIN PAGE
@ -40,27 +37,24 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
int n=WiFi.scanNetworks();
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n" + header;
s+="<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>";
s+="<p>The LED on this device should be <em>double-blinking</em> during this configuration.<p>";
s+="<form action=\"/configure\" method=\"post\">";
s+="<label for=\"ssid\">WiFi Network:</label>";
s+="<input list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required>";
s+="<datalist id=\"network\">";
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>"
"<form action=\"/configure\" method=\"post\">"
"<label for=\"ssid\">WiFi Network:</label>"
"<input list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required maxlength=" + String(MAX_SSID) + ">"
"<datalist id=\"network\">";
for(int i=0;i<n;i++){
if(s.indexOf(WiFi.SSID(i))==-1) // first time this SSID founx
s+="<option value=\"" + WiFi.SSID(i) + "\">" + WiFi.SSID(i) + "</option>";
if(responseBody.indexOf(WiFi.SSID(i))==-1) // first time this SSID found
responseBody+="<option value=\"" + WiFi.SSID(i) + "\">" + WiFi.SSID(i) + "</option>";
}
s+="</datalist><br><br>";
s+="<label for=\"pwd\">WiFi Password:</label>";
s+="<input type=\"password\" id=\"pwd\" name=\"pwd\">";
s+="<br><br>";
s+="<label for=\"code\">Setup Code:</label>";
s+="<input type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\">";
responseBody+="</datalist><br><br>"
"<label for=\"pwd\">WiFi Password:</label>"
"<input type=\"password\" id=\"pwd\" name=\"pwd\">"
"<br><br>"
"<label for=\"code\">Setup Code:</label>"
"<input type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\">";
/*
apClient.print("<p><em>");
@ -69,18 +63,21 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
apClient.print("</p>");
*/
s+="<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>";
s+="</form></body></html>";
responseBody+="<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>"
"</form>";
}
responseHead+="\r\n"; // add blank line between reponse header and body
responseBody+="</body></html>"; // close out body and html tags
LOG2("\n>>>>>>>>>> ");
LOG2(client.remoteIP());
LOG2(" >>>>>>>>>>\n");
LOG2(s);
LOG2(responseHead);
LOG2(responseBody);
LOG2("\n");
client.print(s);
client.print(responseHead);
client.print(responseBody);
LOG2("------------ SENT! --------------\n");
} // processRequest

View File

@ -205,9 +205,6 @@ int Span::getFreeSlot(){
void Span::initWifi(){
const int MAX_SSID=32;
const int MAX_PWD=64;
struct {
char ssid[MAX_SSID+1];
char pwd[MAX_PWD+1];

View File

@ -14,6 +14,12 @@ const char HOMESPAN_VERSION[]="1.0.0";
const int MAX_CONNECTIONS=8;
//////////////////////////////////////////////////////
// Maximum characters in WiFi SSID and Password //
const int MAX_SSID=32;
const int MAX_PWD=64;
/////////////////////////////////////////////////////
// Verbosity -- controls message output //
// 0=Minimal, 1=Informative, 2=All //