On Air mit counter
This commit is contained in:
+63
-16
@@ -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,7 +23,8 @@ void setup() {
|
||||
pinMode(optocoupler, OUTPUT);
|
||||
digitalWrite(optocoupler, LOW);
|
||||
|
||||
// pinMode(inputPin, INPUT);
|
||||
pinMode(togglePin, OUTPUT);
|
||||
digitalWrite(togglePin, LOW);
|
||||
|
||||
// Connect WiFi
|
||||
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 = "<!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(){
|
||||
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user