#include #include #include #include #include "eboot_command.h" #include "creds.h" const char* ssid = STASSID; const char* password = STAPSK; WiFiServer server(80); void ota_setup() { ArduinoOTA.onStart([]() { Serial.flush(); Serial.begin(74880); Serial.flush(); String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_FS type = "filesystem"; } // NOTE: if updating FS this would be the place to unmount FS using FS.end() Serial.println("[OTA] Start updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\n[OTA] End"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("[OTA] Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("[OTA] Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("[OTA] Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("[OTA] Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("[OTA] Receive Failed"); } else if (error == OTA_END_ERROR) { Serial.println("[OTA] End Failed"); } }); } void setup() { Serial.begin(74880); Serial.println("\n[Program] Booting"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("[Program] Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } ota_setup(); ArduinoOTA.begin(); server.begin(); Serial.printf("[Program] OTA and HTTPServer ready !\r\n"); Serial.printf("[Program] IP address: "); Serial.println(WiFi.localIP()); Serial.flush(); Serial.begin(9600); Serial.flush(); } int value = 0; void client_handler(WiFiClient* cl) { String currentLine = ""; while (cl->connected()) { if (cl->available()) { char c = cl->read(); Serial.write(c); if (c == '\n') { if (currentLine.length() == 0) { cl->println("HTTP/1.1 200 OK"); cl->println("Content-type:text/html"); cl->println(); cl->print("Relais Einschalten
"); cl->print("Relais Ausschalten
"); cl->println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } if (currentLine.endsWith("GET /H")) { Serial.println("EIN"); delay(10); byte close[] = {0xA0, 0x01, 0x01, 0xA2}; Serial.write(close, sizeof(close)); } if (currentLine.endsWith("GET /L")) { Serial.println("AUS"); delay(10); byte open[] = {0xA0, 0x01, 0x00, 0xA1}; Serial.write(open, sizeof(open)); } } } cl->stop(); } void loop() { ArduinoOTA.handle(); WiFiClient client = server.available(); if (client) { client_handler(&client); } }