diff --git a/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino index 4e1ebac..8082f74 100644 --- a/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino +++ b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino @@ -2,19 +2,26 @@ /* Create a WiFi access point and provide a web server on it. */ #include -#include +#include #include #include #include +#include #include #define EEPROM_SIZE 512 char eeprom[EEPROM_SIZE]; -/* Mode could be 0 for Setup, 1 for normal use - * Setup mode is trigger by setting GPIO3 to ground or at first boot +#define BOOTMODE_SETUP 0 +#define BOOTMODE_NORMAL 1 +#define BOOTMODE_OTA 2 + +/* EEPROM LAYOUT + "BOOTMODE;SSID;PASSWORD;HOSTNAME;" + BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA + Setup mode is trigger by setting GPIO3 to ground or at first boot */ -int mode = 0; + /* Set these to your desired credentials. */ const char *ssid = "ESPConfigurator"; @@ -22,48 +29,48 @@ const char *ssid = "ESPConfigurator"; ESP8266WebServer server(80); /* Just a little test message. Go to http://192.168.4.1 in a web browser - * connected to this access point to see it. - */ + connected to this access point to see it. +*/ void handleRoot() { - server.send(200, "text/html", "

You are connected


" - "Setup
" - "ON
" - "OFF
" - ); + server.send(200, "text/html", "

You are connected


" + "Setup
" + "ON
" + "OFF
" + ); } void handleSetup() { server.send(200, "text/html", "
" - "
" - "
" - "
" - "
" - "
"); + "
" + "
" + "
" + "
" + ""); } void handleGpio() { - if(!server.hasArg("gpio") || !server.hasArg("value")){ + if (!server.hasArg("gpio") || !server.hasArg("value")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt()); pinMode(server.arg("gpio").toInt(), OUTPUT); - server.send(200, "text/html", "

GPIO" + server.arg("gpio") + " changed to " + server.arg("value") +"

"); + server.send(200, "text/html", "

GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "

"); } -int saveConfig(String ssid, String password, String host ){ +int saveConfig(int bootMode, String ssid, String password, String host ) { String eeprom; - - eeprom = "1;"+ssid+";"+password+";"+host+";"; - if(eeprom.length() > EEPROM_SIZE ) + eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";"; + + if (eeprom.length() > EEPROM_SIZE ) return -EMSGSIZE; - - Serial.println("Saving "+eeprom); - for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++){ - EEPROM.write(i, eeprom.charAt(i)); + Serial.println("Saving " + eeprom); + + for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) { + EEPROM.write(i, eeprom.charAt(i)); } EEPROM.commit(); @@ -77,77 +84,84 @@ void handleSave() { String ssid; String hostName; - if(!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")){ + if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } - if(saveConfig(server.arg("ssid"), server.arg("password"), server.arg("host")) < 0){ + if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host")) < 0) { server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } - + server.send(200, "text/html", "

Configuration Saved

"); } -void handleNotFound(){ +void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; - message += (server.method() == HTTP_GET)?"GET":"POST"; + message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; - for (uint8_t i=0; i