Created
February 28, 2018 20:29
-
-
Save msraynsford/ab482016464ebb1485e4d51f9cc26a0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#include <FS.h> | |
#include "EEPROMAnything.h" | |
#define RESETPIN D1 | |
ESP8266WebServer webServer(80); | |
struct config_t{ | |
char ssid[17] = "ESP8266 Test"; | |
char password[17] = "password"; | |
} config; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(RESETPIN, INPUT_PULLUP); | |
SPIFFS.begin(); | |
//Start the EEPROM library with enough memory to hold all the config data | |
EEPROM.begin(sizeof(config)); | |
//Check the reset pin and overwrite the eeprom with the default values if required | |
if(!digitalRead(RESETPIN)) { | |
Serial.printf("Firmware Reset Detected\n"); | |
EEPROM_writeAnything(0, config); | |
EEPROM.commit(); | |
} | |
//Retreive the network values from the EEPROM and output them to serial as a confirmation test. | |
EEPROM_readAnything(0, config); | |
Serial.printf("SSID: '%s'\n", config.ssid); | |
Serial.printf("Password: '%s'\n", config.password); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP(config.ssid, config.password); | |
webServer.on("/", [](){ | |
webServer.send(200, "text/plain", "visit /admin for wifi configuration"); | |
}); | |
webServer.serveStatic("/admin", SPIFFS, "/admin.html"); | |
webServer.on("/admin", handleAdmin); | |
webServer.begin(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
webServer.handleClient(); | |
} | |
void handleAdmin() { | |
// Create a string containing all the arguments, send them out to the serial port | |
Serial.printf("handleAdmin\n"); | |
// Construct a list of all the arguments for debug | |
String message = "#Args:" + String(webServer.args()) + "\n"; | |
for (int i = 0; i < webServer.args(); i++) | |
message.concat(webServer.argName(i) + ": "+ webServer.arg(i) + "\n"); | |
Serial.print(message); | |
// Check to see that the submitted password is the same as the current password | |
if(webServer.hasArg("oldpassword")) { | |
// Check to see that the submitted password is the same as the current password | |
if(strcmp(webServer.arg("oldpassword").c_str(), config.password) == 0) { | |
// If the passwords match we can update the config with the new settings | |
// Check to see if there is a new value (also doubles to check the length of the new value is long enough) | |
if(webServer.arg("newpassword").length() > 7) | |
webServer.arg("newpassword").toCharArray(config.password, sizeof(config.password)); | |
if(webServer.arg("newssid").length() > 7) | |
webServer.arg("newssid").toCharArray(config.ssid, sizeof(config.ssid)); | |
// Store the new settings to EEPROM | |
EEPROM_writeAnything(0, config); | |
EEPROM.commit(); | |
// Construct a message to tell the user that the change worked | |
message = "New settings will take effect after restart"; | |
Serial.println(message); | |
} | |
else { | |
// Construct a message to tell the user that the change failed | |
message = "Incorrect password please try again"; | |
} | |
// Reply with a web page to indicate success or failure | |
message = "<html><head><meta http-equiv='refresh' content='5;url=/' /></head><body><h1>" + message; | |
message += "<br/>Redirecting in 5 seconds...</h1></body></html>"; | |
webServer.send(200, "text/html", message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment