diff --git a/config.h b/config.h new file mode 100644 index 0000000000000000000000000000000000000000..dcb79fdefd5ba618535da6c6a89d59f83635ec37 --- /dev/null +++ b/config.h @@ -0,0 +1,79 @@ + +// define EEPROM settings +// https://www.kriwanek.de/index.php/de/homeautomation/esp8266/364-eeprom-für-parameter-verwenden + +typedef struct { + uint8_t brightness; + uint8_t currentPatternIndex; + uint8_t red; + uint8_t green; + uint8_t blue; + uint8_t power; + uint8_t autoplay; + uint8_t autoplayDuration; + uint8_t currentPaletteIndex; + uint8_t speed; + char hostname[33]; + uint8_t MQTTEnabled; + char MQTTHost[65]; + uint16_t MQTTPort; + char MQTTUser[33]; + char MQTTPass[65]; + char MQTTTopic[65]; + char MQTTDeviceName[33]; +} configData_t; + + +configData_t cfg; +configData_t default_cfg; + +// set to true if config has changed +bool save_config = false; + + +void saveConfig(bool save) { + // Save configuration from RAM into EEPROM + if (save == true) { + EEPROM.begin(4095); + EEPROM.put(0, cfg ); + delay(200); + EEPROM.commit(); + EEPROM.end(); + + save_config = false; + } +} + +void resetConfig() { + + // delete EEPROM config + EEPROM.begin(4095); + for (int i = 0 ; i < sizeof(cfg) ; i++) { + EEPROM.write(i, 0); + } + delay(200); + EEPROM.commit(); + EEPROM.end(); + + // set to default config + cfg = default_cfg; + saveConfig(true); +} + +void setHostname(String new_hostname) +{ + int j = 0; + for (int i = 0; i < new_hostname.length() && i < sizeof(cfg.hostname); i++) { + if (new_hostname.charAt(i) == '-' or \ + (new_hostname.charAt(i) >= '0' && new_hostname.charAt(i) <= '9') or \ + (new_hostname.charAt(i) >= 'A' && new_hostname.charAt(i) <= 'Z') or \ + (new_hostname.charAt(i) >= 'a' && new_hostname.charAt(i) <= 'z')) { + + cfg.hostname[j] = new_hostname.charAt(i); + j++; + } + } + cfg.hostname[j] = '\0'; + save_config = true; +} +// EOF diff --git a/esp8266-fastled-iot-webserver.ino b/esp8266-fastled-iot-webserver.ino index 9325f597b84ee3b264aacd78f50088406df40029..ff2466785b0c75ca9d567b0fb4ba0d459860787b 100644 --- a/esp8266-fastled-iot-webserver.ino +++ b/esp8266-fastled-iot-webserver.ino @@ -370,35 +370,8 @@ if you have connected the ring first it should look like this: const int twpOffs uint8_t incomingPacket[PACKET_LENGTH + 1]; #endif -// define EEPROM settings -// https://www.kriwanek.de/index.php/de/homeautomation/esp8266/364-eeprom-für-parameter-verwenden - -typedef struct { - uint8_t brightness; - uint8_t currentPatternIndex; - uint8_t red; - uint8_t green; - uint8_t blue; - uint8_t power; - uint8_t autoplay; - uint8_t autoplayDuration; - uint8_t currentPaletteIndex; - uint8_t speed; - char hostname[33]; - uint8_t MQTTEnabled; - char MQTTHost[65]; - uint16_t MQTTPort; - char MQTTUser[33]; - char MQTTPass[65]; - char MQTTTopic[65]; - char MQTTDeviceName[33]; -} configData_t; - -configData_t cfg; -configData_t default_cfg; - -// set to true if config has changed -bool save_config = false; +// include config management +#include "config.h" ESP8266WebServer webServer(80); @@ -1095,10 +1068,10 @@ void setup() { } #endif if (force_restart) { - saveConfig(true); - handleReboot(); + saveConfig(true); + handleReboot(); } else { - webServer.send(200, "text/html", "<html><head><meta http-equiv=\"refresh\" content=\"0; url=/settings.htm\"/></head><body></body>"); + webServer.send(200, "text/html", "<html><head><meta http-equiv=\"refresh\" content=\"0; url=/settings.htm\"/></head><body></body>"); } }); @@ -1106,18 +1079,7 @@ void setup() { // delete EEPROM settings if (webServer.arg("type") == String("all")) { - // delete EEPROM config - EEPROM.begin(4095); - for (int i = 0 ; i < sizeof(cfg) ; i++) { - EEPROM.write(i, 0); - } - delay(200); - EEPROM.commit(); - EEPROM.end(); - - // set to default config - cfg = default_cfg; - saveConfig(true); + resetConfig(); } // delete wireless config @@ -1472,19 +1434,6 @@ void loop() { } } -void saveConfig(bool save) { - // Save configuration from RAM into EEPROM - if (save == true) { - EEPROM.begin(4095); - EEPROM.put(0, cfg ); - delay(200); - EEPROM.commit(); - EEPROM.end(); - - save_config = false; - } -} - void loadConfig() { // Loads configuration from EEPROM into RAM @@ -1736,23 +1685,6 @@ void setSpeed(uint8_t value) broadcastInt("speed", speed); } -void setHostname(String new_hostname) -{ - int j = 0; - for (int i = 0; i < new_hostname.length() && i < sizeof(cfg.hostname); i++) { - if (new_hostname.charAt(i) == '-' or \ - (new_hostname.charAt(i) >= '0' && new_hostname.charAt(i) <= '9') or \ - (new_hostname.charAt(i) >= 'A' && new_hostname.charAt(i) <= 'Z') or \ - (new_hostname.charAt(i) >= 'a' && new_hostname.charAt(i) <= 'z')) { - - cfg.hostname[j] = new_hostname.charAt(i); - j++; - } - } - cfg.hostname[j] = '\0'; - save_config = true; -} - void strandTest() { static uint8_t i = 0;