#include #include #include #include static const char* SETUP_SSID = "sensor-setup"; static const byte EEPROM_INITIALIZED_MARKER = 0xF1; //Just a magic number #define SETUP_MODE_PIN (D7) ESP8266WebServer server(80); DNSServer dnsServer; static const byte DNS_PORT = 53; IPAddress apIP(192, 168, 4, 1); #define MAX_SSID_LENGTH (32) #define MAX_PASSWORD_LENGTH (64) char ssid_param[MAX_SSID_LENGTH+1]; char password_param[MAX_PASSWORD_LENGTH+1]; boolean in_setup_mode; void read_persistent_string(char* s, int max_length, int& adr) { int i = 0; byte c; do { c = EEPROM.read(adr++); if (i(c); } } while (c!=0); s[i] = 0; } void read_persistent_params() { int adr = 0; if (EEPROM_INITIALIZED_MARKER != EEPROM.read(adr++)) { ssid_param[0] = 0; password_param[0] = 0; } else { read_persistent_string(ssid_param, MAX_SSID_LENGTH, adr); read_persistent_string(password_param, MAX_PASSWORD_LENGTH, adr); } } void write_persistent_string(const char* s, size_t max_length, int& adr) { for (int i=0; i"\ ""\ ""\ ""\ "Setup"\ ""\ ""\ ""\ "
"\ "SSID:
"\ "Password:
"\ ""\ "
"\ ""\ ""); server.send(200, F("text/html"), body); } } void setup() { EEPROM.begin(1 + MAX_SSID_LENGTH + 1 + MAX_PASSWORD_LENGTH + 1); pinMode(SETUP_MODE_PIN, INPUT_PULLUP); if (LOW == digitalRead(SETUP_MODE_PIN)) { in_setup_mode = true; WiFi.softAP(SETUP_SSID); dnsServer.start(DNS_PORT, "*", apIP); server.on("/", handleSetupRoot); server.begin(); } else { in_setup_mode = false; read_persistent_params(); WiFi.mode(WIFI_STA); WiFi.begin(ssid_param, password_param); while (WiFi.status() != WL_CONNECTED) { delay(500); } //TODO } } void loop() { if (in_setup_mode) { dnsServer.processNextRequest(); } else { //TODO } }