On Air mit counter

This commit is contained in:
2022-02-06 12:26:05 +01:00
parent 0beeb45846
commit 9be2b3905d
+64 -17
View File
@@ -3,15 +3,15 @@
const int optocoupler = D7; const int optocoupler = D7;
const int togglePin = D3;
const char* ssid = "LeWe"; const char* ssid = "LeWe";
const char* password = "5275274365464843"; const char* password = "5275274365464843";
ESP8266WebServer server(80); ESP8266WebServer server(80);
int lightStatus = 0; // 0 off, 1 pulse, 2 on int lightStatus = 0; // 0 off, 1 on, 2 pulse
int toggleStatus = 0;
//const int inputPin = D3;
//int val = 0;
void setup() { void setup() {
@@ -23,8 +23,9 @@ void setup() {
pinMode(optocoupler, OUTPUT); pinMode(optocoupler, OUTPUT);
digitalWrite(optocoupler, LOW); digitalWrite(optocoupler, LOW);
// pinMode(inputPin, INPUT); pinMode(togglePin, OUTPUT);
digitalWrite(togglePin, LOW);
// Connect WiFi // Connect WiFi
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@@ -41,8 +42,10 @@ void setup() {
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
server.on("/light", handle_OnConnect); server.on("/click", handle_click);
server.on("/light", handle_light);
server.onNotFound(handle_NotFound); server.onNotFound(handle_NotFound);
server.on("/toggle", handle_toggle);
server.begin(); server.begin();
Serial.println("HTTP server started"); Serial.println("HTTP server started");
@@ -50,18 +53,13 @@ void setup() {
void loop() { void loop() {
server.handleClient(); server.handleClient();
/*
val = digitalRead(inputPin);
digitalWrite(BUILTIN_LED, val);
*/
} }
void cycleStatus() { void cycleStatus() {
digitalWrite(optocoupler, HIGH); digitalWrite(optocoupler, HIGH);
delay(250); delay(350);
digitalWrite(optocoupler, LOW); digitalWrite(optocoupler, LOW);
delay(50); delay(150);
if (lightStatus < 2) { if (lightStatus < 2) {
lightStatus++; lightStatus++;
@@ -70,13 +68,16 @@ void cycleStatus() {
lightStatus = 0; lightStatus = 0;
} }
Serial.println("click"); Serial.print("click --> ");
Serial.println(lightStatus);
} }
void handle_OnConnect() { void handle_click() {
if (server.hasArg("status")) { if (server.hasArg("status")) {
int targetStatus = server.arg("status").toInt(); int targetStatus = server.arg("status").toInt();
Serial.print("targetStatus: ");
Serial.println(targetStatus);
if (targetStatus >= 0 && targetStatus <= 2) { if (targetStatus >= 0 && targetStatus <= 2) {
while (lightStatus != targetStatus) { while (lightStatus != targetStatus) {
@@ -88,8 +89,54 @@ void handle_OnConnect() {
cycleStatus(); 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 = "<!DOCTYPE html><html>";
response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
response += "<link rel=\"icon\" href=\"data:,\">";
response += "<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; } ";
response += ".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
response += "</style></head>";
response += "<body><h1>On Air</h1>";
response += "<p><a href=\"/light?status=0\"><button class=\"button\">Off</button></a></p>";
response += "<p><a href=\"/light?status=1\"><button class=\"button\">On</button></a></p>";
response += "<p><a href=\"/light?status=2\"><button class=\"button\">Pulse</button></a></p>";
response += "<p>" + String(lightStatus) + "</p>";
response += "</body></html>";
server.send(200, "text/html", response);
}
void handle_NotFound(){ void handle_NotFound(){
server.send(404, "text/plain", "Not found"); 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));
}