Moving serial wifi update into Network.cpp
This commit is contained in:
parent
fd68164917
commit
4a6c737c72
|
|
@ -212,6 +212,8 @@ void Span::initWifi(){
|
|||
char pwd[MAX_PWD+1];
|
||||
} wifiData;
|
||||
|
||||
char setupCode[9];
|
||||
|
||||
char id[18]; // create string version of Accessory ID for MDNS broadcast
|
||||
memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes
|
||||
id[17]='\0'; // add terminating null
|
||||
|
|
@ -230,26 +232,45 @@ void Span::initWifi(){
|
|||
if(!nvs_get_blob(wifiHandle,"WIFIDATA",NULL,&len)){ // if found WiFi data in NVS
|
||||
nvs_get_blob(wifiHandle,"WIFIDATA",&wifiData,&len); // retrieve data
|
||||
} else {
|
||||
statusLED.start(500,0.3,2,1000);
|
||||
|
||||
network.configure(hostName);
|
||||
statusLED.start(250);
|
||||
Serial.print("Network configuration required!\nPress control button for 3 seconds to start AccessPoint or enter network data here.\n\n");
|
||||
Serial.print(">>> WiFi SSID: ");
|
||||
|
||||
resetPressed=0;
|
||||
int status=0;
|
||||
|
||||
while(status==0){ // loop until a configuration method is chosen and completed
|
||||
|
||||
if(!resetPressed){
|
||||
if(!digitalRead(resetPin)){
|
||||
resetPressed=1;
|
||||
resetTime=millis()+3000;
|
||||
}
|
||||
} else if(digitalRead(resetPin)){
|
||||
resetPressed=0;
|
||||
} else if(millis()>resetTime){
|
||||
statusLED.start(100,0.3,3,500);
|
||||
// statusLED.start(2000,0.75);
|
||||
Serial.print("(skipping)\n\n");
|
||||
status=network.apConfigure(hostName);
|
||||
}
|
||||
|
||||
if(Serial.available())
|
||||
status=network.serialConfigure();
|
||||
|
||||
} // while loop
|
||||
|
||||
Serial.println(status);
|
||||
Serial.print("'"); Serial.print(network.ssid); Serial.println("'");
|
||||
Serial.print("'"); Serial.print(network.pwd); Serial.println("'");
|
||||
Serial.print("'"); Serial.print(network.setupCode); Serial.println("'");
|
||||
while(1);
|
||||
|
||||
// delay(2000); // pause while prior page is displayed
|
||||
// WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point
|
||||
// ESP.restart(); // re-start device
|
||||
|
||||
Serial.print("Please configure network...\n");
|
||||
sprintf(wifiData.ssid,"MyNetwork");
|
||||
sprintf(wifiData.pwd,"MyPassword");
|
||||
|
||||
Serial.print(">>> WiFi SSID (");
|
||||
Serial.print(wifiData.ssid);
|
||||
Serial.print("): ");
|
||||
readSerial(wifiData.ssid,MAX_SSID);
|
||||
Serial.print(wifiData.ssid);
|
||||
|
||||
Serial.print("\n>>> WiFi Password (");
|
||||
Serial.print(wifiData.pwd);
|
||||
Serial.print("): ");
|
||||
readSerial(wifiData.pwd,MAX_PWD);
|
||||
Serial.print(mask(wifiData.pwd,2));
|
||||
Serial.print("\n\n");
|
||||
|
||||
nvs_set_blob(wifiHandle,"WIFIDATA",&wifiData,sizeof(wifiData)); // update data
|
||||
nvs_commit(wifiHandle); // commit to NVS
|
||||
|
|
|
|||
105
src/Network.cpp
105
src/Network.cpp
|
|
@ -3,13 +3,48 @@
|
|||
|
||||
#include "Network.h"
|
||||
#include "Utils.h"
|
||||
#include "HAP.h"
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
///////////////////////////////
|
||||
|
||||
void Network::configure(char *apName){
|
||||
int Network::serialConfigure(){
|
||||
|
||||
Serial.print("WiFi Configuration required. Please enter details here, or connect to Access Point: ");
|
||||
sprintf(ssid,"");
|
||||
sprintf(pwd,"");
|
||||
|
||||
readSerial(ssid,MAX_SSID);
|
||||
Serial.print(ssid);
|
||||
Serial.print("\n");
|
||||
|
||||
while(!strlen(ssid)){
|
||||
Serial.print(">>> WiFi SSID: ");
|
||||
readSerial(ssid,MAX_SSID);
|
||||
Serial.print(ssid);
|
||||
Serial.print("\n");
|
||||
}
|
||||
|
||||
while(!strlen(pwd)){
|
||||
Serial.print(">>> WiFi PASS: ");
|
||||
readSerial(pwd,MAX_PWD);
|
||||
Serial.print(mask(pwd,2));
|
||||
Serial.print("\n");
|
||||
}
|
||||
|
||||
Serial.print("Done");
|
||||
while(1);
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
|
||||
int Network::apConfigure(char *apName){
|
||||
|
||||
Serial.print("Starting Access Point: ");
|
||||
Serial.print(apName);
|
||||
Serial.print(" / ");
|
||||
Serial.print(apPassword);
|
||||
Serial.print("\n");
|
||||
|
||||
WiFiServer apServer(80);
|
||||
|
|
@ -22,13 +57,21 @@ void Network::configure(char *apName){
|
|||
IPAddress apIP(192, 168, 4, 1);
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(apName,"homespan"); // start access point
|
||||
WiFi.softAP(apName,apPassword); // start access point
|
||||
dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device
|
||||
apServer.begin();
|
||||
|
||||
boolean needsConfiguration=true;
|
||||
int status=0; // initialize status
|
||||
alarmTimeOut=millis()+lifetime; // Access Point will stay alive until alarmTimeOut is reached
|
||||
|
||||
while(needsConfiguration){
|
||||
while(status==0){
|
||||
|
||||
if(millis()>alarmTimeOut){
|
||||
Serial.print("\n*** ERROR: Access Point timed Out (");
|
||||
Serial.print(lifetime/1000);
|
||||
Serial.print(" seconds)\n\n");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
dnsServer.processNextRequest();
|
||||
|
||||
|
|
@ -82,35 +125,25 @@ void Network::configure(char *apName){
|
|||
LOG2("\n------------ END BODY! ------------\n");
|
||||
|
||||
content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based
|
||||
processRequest(body, (char *)content); // process request
|
||||
|
||||
delay(1);
|
||||
client.stop();
|
||||
|
||||
/*
|
||||
if(!client){ // client disconnected by server
|
||||
LOG1("** Disconnecting AP Client (");
|
||||
LOG1(millis()/1000);
|
||||
LOG1(" sec)\n");
|
||||
}
|
||||
*/
|
||||
status=processRequest(body, (char *)content); // process request; returns 0=continue, 1=exit and save settings, 2=exit and cancel changes
|
||||
|
||||
LOG2("\n");
|
||||
|
||||
} // process HAP Client
|
||||
|
||||
}
|
||||
} // while status==0
|
||||
|
||||
while(1);
|
||||
return(status);
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
|
||||
void Network::processRequest(char *body, char *formData){
|
||||
|
||||
boolean restart=false;
|
||||
int Network::processRequest(char *body, char *formData){
|
||||
|
||||
int returnCode=0;
|
||||
|
||||
String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n";
|
||||
|
||||
String responseBody="<html><head><style>"
|
||||
|
|
@ -130,27 +163,27 @@ void Network::processRequest(char *body, char *formData){
|
|||
|
||||
LOG1("In Post Configure...\n");
|
||||
|
||||
getFormValue(formData,"network",wifiData.ssid,MAX_SSID);
|
||||
getFormValue(formData,"pwd",wifiData.pwd,MAX_PWD);
|
||||
getFormValue(formData,"network",ssid,MAX_SSID);
|
||||
getFormValue(formData,"pwd",pwd,MAX_PWD);
|
||||
getFormValue(formData,"code",setupCode,8);
|
||||
|
||||
timer=millis();
|
||||
WiFi.begin(wifiData.ssid,wifiData.pwd);
|
||||
WiFi.begin(ssid,pwd);
|
||||
|
||||
responseBody+="<meta http-equiv = \"refresh\" content = \"5; url = /wifi-status\" />"
|
||||
"<p>Initiating WiFi connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
|
||||
"<p>Initiating WiFi connection to:</p><p><b>" + String(ssid) + "</p>";
|
||||
responseBody+="<center><button onclick=\"document.location='/'\">Cancel</button></center>";
|
||||
|
||||
} 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;
|
||||
responseBody+="<p><b>Settings saved!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
||||
returnCode=1;
|
||||
} 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;
|
||||
responseBody+="<p><b>Update canceled!</b></p><p>Restarting HomeSpan in 2 seconds...</p>";
|
||||
returnCode=-1;
|
||||
} else
|
||||
|
||||
if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS
|
||||
|
|
@ -159,10 +192,10 @@ void Network::processRequest(char *body, char *formData){
|
|||
|
||||
if(WiFi.status()!=WL_CONNECTED){
|
||||
responseHead+="Refresh: 5\r\n";
|
||||
responseBody+="<p>Re-trying (" + String((millis()-timer)/1000) + " seconds) connection to:</p><p><b>" + String(wifiData.ssid) + "</p>";
|
||||
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>";
|
||||
} else {
|
||||
responseBody+="<p>SUCCESS! Connected to:</p><p><b>" + String(wifiData.ssid) + "</p>";
|
||||
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>"
|
||||
"<center><button onclick=\"document.location='/cancel-restart'\">CANCEL Changes and Re-Start</button></center>";
|
||||
}
|
||||
|
|
@ -171,6 +204,8 @@ void Network::processRequest(char *body, char *formData){
|
|||
|
||||
LOG1("In Captive Access...\n");
|
||||
|
||||
homeSpan.statusLED.start(500,0.3,2,1000);
|
||||
|
||||
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>"
|
||||
|
|
@ -209,10 +244,10 @@ void Network::processRequest(char *body, char *formData){
|
|||
client.print(responseBody);
|
||||
LOG2("------------ SENT! --------------\n");
|
||||
|
||||
if(restart){
|
||||
//delay(2000);
|
||||
//ESP.restart();
|
||||
}
|
||||
delay(1);
|
||||
client.stop();
|
||||
|
||||
return(returnCode);
|
||||
|
||||
} // processRequest
|
||||
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@
|
|||
|
||||
struct Network {
|
||||
|
||||
struct {
|
||||
char ssid[MAX_SSID+1];
|
||||
char pwd[MAX_PWD+1];
|
||||
} wifiData;
|
||||
const int MAX_HTTP=4095; // max number of bytes in HTTP message
|
||||
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 int MAX_HTTP=4095; // max number of bytes in HTTP message
|
||||
WiFiClient client; // client used for HTTP calls
|
||||
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
|
||||
|
||||
char setupCode[9]; // 8-digit Setup Code with room for null terminator
|
||||
char ssid[MAX_SSID+1];
|
||||
char pwd[MAX_PWD+1];
|
||||
char setupCode[8+1];
|
||||
|
||||
WiFiClient client; // client used for HTTP calls
|
||||
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 processRequest(char *body, char *formData); // process the HTTP request
|
||||
int serialConfigure(); // configure homeSpan WiFi and Setup Code from Serial Monitor; 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 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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue