diff --git a/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino new file mode 100644 index 0000000..fe60d76 --- /dev/null +++ b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino @@ -0,0 +1,218 @@ + +/* Create a WiFi access point and provide a web server on it. */ + +#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 +*/ +int mode = 0; + +/* Set these to your desired credentials. */ +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. + */ +void handleRoot() { + server.send(200, "text/html", "

You are connected


Setup"); +} + +void handleSetup() { + server.send(200, "text/html", "
" + "
" + "
" + "
" + "
" + "
"); +} + +void handleGpio() { + 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") +"

"); +} + +int saveConfig(String ssid, String password, String host ){ + String eeprom; + + eeprom = "1;"+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)); + } + + EEPROM.commit(); + delay(100); + + return 0; +} + +void handleSave() { + String password; + String ssid; + String hostName; + + 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){ + server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); + return; + } + + server.send(200, "text/html", "

Configuration Saved

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