const char SSID[] = ""; const char PASSWORD[] = ""; const char version[] = " 0.8"; #include "globalvars.h" #include #include #include #include #include #include ESP8266WebServer server; IPAddress staticIP(192, 168, 2, 5); // static IP address IPAddress gateway(192, 168, 2, 1); // Router's IP address IPAddress subnet(255, 255, 255, 0); IPAddress dns(192, 168, 2, 1); Timezone myTime; char time_all[8][8][3] = { {}, {}, {}, {}, {}, {}, {}, {}}; // 3D array for load_EEPROM() and handleTime() const unsigned int pins[8] = {5, 4, 14, 12, 13, 10, 9}; // TODO: only 7 of 8 pins defined int r = 0; char stat[8][4] = {"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS"}; // text on button char t_stat[8][4] = {"", "", "", "", "", "", "", ""}; // for "status of timer changed" bool override[8] = {false, false, false, false, false, false, false, false}; // for manual override char home_html[3000]; // be careful when changing these values (might cause // overflow)! char settings_html[2014]; const char settings_html_pre[] PROGMEM = {"\ \ \ \ \ \ Einstellungen\ \ \ \
\
\ \
\ \ - \
\ \ - \
\ \
\
\ \ \ \ \ \ \ \ "}; void reload_home(); void reload_settings(); void handleToggle(); void load_EEPROM() { unsigned int EEPROM_Addr = 0; for (unsigned int i0 = 0; i0 < 8; i0++) { for (unsigned int i = 0; i < 8; i++) { sprintf(time_all[i0][i], "%02d", EEPROM.read(EEPROM_Addr)); EEPROM_Addr += 4; yield(); } } } void handleHome() { reload_home(); server.send(200, "text/html", home_html); debugln(ESP.getFreeHeap(), DEC); } void handleSettings() { reload_settings(); server.setContentLength(strlen(settings_html_pre) + strlen(settings_html)); server.send(200, "text/html", settings_html_pre); server.sendContent(settings_html); debugln(ESP.getFreeHeap(), DEC); } void handleTime() { const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"}; for (unsigned int arg = 0; arg < 4; arg++) { if (server.arg(args[arg]) != "") { char buff_h[2][2] = {server.arg(args[arg])[0], server.arg(args[arg])[1]}; char buff_m[2][2] = {server.arg(args[arg])[3], server.arg(args[arg])[4]}; sprintf(time_all[arg * 2][server.arg("rule").toInt()], "%s%s", buff_h[0], buff_h[1]); sprintf(time_all[arg * 2 + 1][server.arg("rule").toInt()], "%s%s", buff_m[0], buff_m[1]); EEPROM.put(server.arg("rule").toInt() * 4 + arg * 64, atoi(time_all[arg * 2][server.arg("rule").toInt()])); EEPROM.commit(); EEPROM.put(server.arg("rule").toInt() * 4 + arg * 64 + 32, atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()])); EEPROM.commit(); } } reload_settings(); server.setContentLength(strlen(settings_html_pre) + strlen(settings_html)); server.send(301, "text/html", settings_html_pre); server.sendContent(settings_html); } void timer() { if (((myTime.hour() * 60 + myTime.minute() >= atoi(time_all[0][r]) * 60 + atoi(time_all[1][r])) and (myTime.hour() * 60 + myTime.minute() < atoi(time_all[2][r]) * 60 + atoi(time_all[3][r]))) or ((myTime.hour() * 60 + myTime.minute() >= atoi(time_all[4][r]) * 60 + atoi(time_all[5][r])) and (myTime.hour() * 60 + myTime.minute() < atoi(time_all[6][r]) * 60 + atoi(time_all[7][r])))) { if ((override[r]) and (strcmp(t_stat[r], "off") == 0)) { override[r] = false; debugln(F("debug#1")); } sprintf(t_stat[r], "on"); if (!override[r]) { digitalWrite(pins[r], LOW); if (strcmp(stat[r], "AUS") == 0) { debug(r); debugln(F(" is on")); sprintf(stat[r], "AN"); } } } else { if ((override[r]) and (strcmp(t_stat[r], "on") == 0)) { override[r] = false; } sprintf(t_stat[r], "off"); if (!override[r]) { digitalWrite(pins[r], HIGH); if (strcmp(stat[r], "AN") == 0) { debug(r); debugln(F(" is off")); sprintf(stat[r], "AUS"); } } } } void setup() { int p = 0; while (p <= 7) { pinMode(pins[p], OUTPUT); digitalWrite(pins[p], HIGH); p++; } Serial.begin(74880); EEPROM.begin(256); // 4 bits for EEPROM, address 0-255, value 0-255 debug(F("\nver.")); debugln(version); debugln(F("check for updates at " "https://somepi.ddns.net/gitea/gilex-dev/ESP8266-IOT-timer/\n")); load_EEPROM(); // WLAN-config // --------------------------------------------------------------------------------------------------------------------- WiFi.mode(WIFI_STA); WiFi.config(staticIP, gateway, subnet, dns); WiFi.hostname("ESP8266 IOT development"); WiFi.begin(SSID, PASSWORD); Serial.println(F("Connecting ...")); while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print('.'); } Serial.print(F("\nConnected to ")); Serial.println(WiFi.SSID()); Serial.print(F("IP address:\t")); Serial.println(WiFi.localIP()); //---------------------------------------------------------------------------------------------------------------------------------- myTime.setLocation("de"); waitForSync(); debug(F("Local time: ")); debugln(myTime.dateTime("H:i:s")); setInterval(); server.on("/", HTTP_GET, handleHome); server.on("/settings", HTTP_GET, handleSettings); server.on("/settings", HTTP_POST, handleTime); server.on("/toggle", HTTP_GET, handleToggle); server.on("/version", HTTP_GET, [] { server.send(200, "text/html", version); }); server.begin(); debugln(F("Webserver started")); } void loop() { server.handleClient(); timer(); r++; if (r == 7) { r = 0; } yield(); delay(50); }
von:bis:von:bis: