#include #include const int optocoupler = D2; const char* ssid = "LeWe"; const char* password = "5275274365464843"; ESP8266WebServer server(80); int lightStatus = 0; // 0 off, 1 on, 2 pulse void setup() { Serial.begin(9600); pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output digitalWrite(BUILTIN_LED, HIGH); pinMode(optocoupler, OUTPUT); digitalWrite(optocoupler, LOW); // Connect WiFi Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.hostname("OnAir"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/click", handle_click); server.on("/light", handle_light); server.onNotFound(handle_NotFound); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); } void cycleStatus() { digitalWrite(optocoupler, HIGH); delay(350); digitalWrite(optocoupler, LOW); delay(150); if (lightStatus < 2) { lightStatus++; } else { lightStatus = 0; } Serial.print("click --> "); Serial.println(lightStatus); } void handle_click() { if (server.hasArg("status")) { int targetStatus = server.arg("status").toInt(); Serial.print("targetStatus: "); Serial.println(targetStatus); if (targetStatus >= 0 && targetStatus <= 2) { while (lightStatus != targetStatus) { cycleStatus(); } } } else { cycleStatus(); } String response = "Status set to "; response.concat(lightStatus); server.send(200, "text/plain", response); } String getButtonStatus(int button) { if (lightStatus == button) { return "button_on"; } return "button_off"; } void handle_light(){ if (server.hasArg("status")) { int targetStatus = server.arg("status").toInt(); Serial.print("targetStatus: "); Serial.println(targetStatus); if (targetStatus >= 0 && targetStatus <= 2) { while (lightStatus != targetStatus) { cycleStatus(); } } } String response = ""; response += ""; response += ""; response += ""; response += "

On Air

"; response += "

"; response += "

"; response += "

"; response += ""; server.send(200, "text/html", response); } void handle_NotFound(){ server.send(404, "text/plain", "Not found"); }