diff --git a/D1 mini/On_Air/On_Air.ino b/D1 mini/On_Air/On_Air.ino index b1e4fc6..baeecd6 100644 --- a/D1 mini/On_Air/On_Air.ino +++ b/D1 mini/On_Air/On_Air.ino @@ -3,15 +3,15 @@ const int optocoupler = D7; +const int togglePin = D3; + const char* ssid = "LeWe"; const char* password = "5275274365464843"; ESP8266WebServer server(80); -int lightStatus = 0; // 0 off, 1 pulse, 2 on - -//const int inputPin = D3; -//int val = 0; +int lightStatus = 0; // 0 off, 1 on, 2 pulse +int toggleStatus = 0; void setup() { @@ -23,8 +23,9 @@ void setup() { pinMode(optocoupler, OUTPUT); digitalWrite(optocoupler, LOW); -// pinMode(inputPin, INPUT); - + pinMode(togglePin, OUTPUT); + digitalWrite(togglePin, LOW); + // Connect WiFi Serial.println(); Serial.println(); @@ -41,8 +42,10 @@ void setup() { Serial.print("IP address: "); Serial.println(WiFi.localIP()); - server.on("/light", handle_OnConnect); + server.on("/click", handle_click); + server.on("/light", handle_light); server.onNotFound(handle_NotFound); + server.on("/toggle", handle_toggle); server.begin(); Serial.println("HTTP server started"); @@ -50,18 +53,13 @@ void setup() { void loop() { server.handleClient(); - -/* - val = digitalRead(inputPin); - digitalWrite(BUILTIN_LED, val); -*/ } void cycleStatus() { digitalWrite(optocoupler, HIGH); - delay(250); + delay(350); digitalWrite(optocoupler, LOW); - delay(50); + delay(150); if (lightStatus < 2) { lightStatus++; @@ -70,13 +68,16 @@ void cycleStatus() { lightStatus = 0; } - Serial.println("click"); + Serial.print("click --> "); + Serial.println(lightStatus); } -void handle_OnConnect() { +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) { @@ -88,8 +89,54 @@ void handle_OnConnect() { cycleStatus(); } - server.send(200, "text/plain", "status set to " + lightStatus); + String response = "Status set to "; + response.concat(lightStatus); + server.send(200, "text/plain", response); } + +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 += "

" + String(lightStatus) + "

"; + response += ""; + + server.send(200, "text/html", response); +} + void handle_NotFound(){ server.send(404, "text/plain", "Not found"); } + +void handle_toggle() { + if (toggleStatus == 0) { + toggleStatus = 1; + digitalWrite(togglePin, HIGH); + } + else { + toggleStatus = 0; + digitalWrite(togglePin, LOW); + } + + server.send(200, "text/html", "D3 is set to " + String(toggleStatus)); +}