From c488ba1cd4f666197075e5ec6815a2dd7774ff44 Mon Sep 17 00:00:00 2001 From: Tilman Date: Wed, 17 May 2023 17:41:52 +0200 Subject: [PATCH] garage_esp32 with server and buttons --- garage/garage_esp32/garage_esp32.ino | 263 ++++++++++++++++----------- 1 file changed, 161 insertions(+), 102 deletions(-) diff --git a/garage/garage_esp32/garage_esp32.ino b/garage/garage_esp32/garage_esp32.ino index 59317bc..d56005a 100644 --- a/garage/garage_esp32/garage_esp32.ino +++ b/garage/garage_esp32/garage_esp32.ino @@ -1,17 +1,68 @@ +/* + * Sources: + * https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-button/ + * + */ + + #include +#include +#include #include + const char* ssid = "LeWe"; const char* password = "5275274365464843"; -WiFiServer server(80); -String header; +const int PIN_LED = 2; +const int PIN_GARAGE_BUTTON = 4; +const int PIN_GATE_BUTTON = 5; +const int PIN_GARAGE = 18; +const int PIN_GATE = 19; +const int PIN_DOOR_OPEN = 21; +const int PIN_DOOR_CLOSED = 22; +const int PIN_SENSOR_GATE= 23; + + +AsyncWebServer server(80); +const char* PARAM_MESSAGE = "message"; WiFiClient webClient; -String webhook = "http://www.google.com/"; HTTPClient http; + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} + +void pinStatus(AsyncWebServerRequest *request) { + String status = "PIN_DOOR_OPEN: " + String(digitalRead(PIN_DOOR_OPEN)) + "\n" + "PIN_DOOR_CLOSED: " + String(digitalRead(PIN_DOOR_CLOSED)); + request->send(200, "text/plain", status); +} + +void mowerSafety() { + if (http.begin(webClient, "http://192.168.178.54/json?cmd=status&user=lewe&pass=nFdcX3sMD115WAap")) { + int httpCode = http.GET(); + + if (httpCode > 0) { + if (httpCode == HTTP_CODE_OK) { + String payload = http.getString(); + + // TODO abhängig vom Status Befehl an den Mäher schicken + Serial.println(payload); + } + } + else { + Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); + } + http.end(); + } + else { + Serial.println("Could not send HTTP request to mower"); + } +} + void setup() { // start serial output Serial.begin(115200); @@ -19,118 +70,126 @@ void setup() { btStop(); // turn off bluetooth - Serial.print("Connecting to "); - Serial.println(ssid); - WiFi.hostname("garage-esp32"); + pinMode(PIN_LED, OUTPUT); + pinMode(PIN_GARAGE_BUTTON, INPUT); + pinMode(PIN_GATE_BUTTON, INPUT); + pinMode(PIN_GARAGE, OUTPUT); + pinMode(PIN_GATE, OUTPUT); + pinMode(PIN_DOOR_OPEN, INPUT); + pinMode(PIN_DOOR_CLOSED, INPUT); + pinMode(PIN_SENSOR_GATE, INPUT); + WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); + delay(1000); + Serial.println("Connecting to WiFi..."); } - Serial.println(""); - Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP()); WiFi.setAutoReconnect(true); WiFi.persistent(true); + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/html", "Hello World!"); + }); + + server.on("/garage", HTTP_GET, [] (AsyncWebServerRequest *request) { + String action; + if (request->hasParam("action")) { + action = request->getParam("action")->value(); + + if (action.equals("open") and digitalRead(PIN_DOOR_CLOSED) and not digitalRead(PIN_DOOR_OPEN)) { + request->send(200, "application/json", "{ \"action\": \"open\" }"); + } + else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) { + request->send(200, "application/json", "{ \"action\": \"close\" }"); + } + else { + pinStatus(request); + } + } + else { + if (digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) { + request->send(200, "application/json", "{ \"status\": \"true\" }"); + } + else { + request->send(200, "application/json", "{ \"status\": \"false\" }"); + } + } + }); + + server.on("/gate", HTTP_GET, [] (AsyncWebServerRequest *request) { + String action; + if (request->hasParam("action")) { + action = request->getParam("action")->value(); + + if (action.equals("open") and not digitalRead(PIN_SENSOR_GATE)) { + request->send(200, "application/json", "{ \"action\": \"open\" }"); + } + else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) { + request->send(200, "application/json", "{ \"action\": \"close\" }"); + } + else { + pinStatus(request); + } + } + else { + if (digitalRead(PIN_SENSOR_GATE)) { + request->send(200, "application/json", "{ \"status\": \"false\" }"); + } + else { + request->send(200, "application/json", "{ \"status\": \"true\" }"); + } + } + }); + + server.onNotFound(notFound); + server.begin(); Serial.println("Setup completed"); } -void loop() { - WiFiClient client = server.available(); // Listen for incoming clients - - if (client) { - String currentLine = ""; // make a String to hold incoming data from the client - while (client.connected()) { // loop while the client's connected - if (client.available()) { // if there's bytes to read from the client, - char c = client.read(); // read a byte, then - //Serial.write(c); // print it out the serial monitor - header += c; - if (c == '\n') { // if the byte is a newline character - // if the current line is blank, you got two newline characters in a row. - // that's the end of the client HTTP request, so send a response: - if (currentLine.length() == 0) { - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) - // and a content-type so the client knows what's coming, then a blank line: - client.println("HTTP/1.1 200 OK"); - client.println("Content-type: text/html; charset=utf-8"); - client.println("Connection: close"); - client.println(); - - // turns the GPIOs on and off - if (header.indexOf("GET /bell/on") >= 0) { - Serial.println("RING"); - } - else if (header.indexOf("GET /play/") >= 0) { // yes, I know this is not RESTful - String str1 = header; - str1 = str1.substring(header.indexOf("GET /play/") + 10); - int fileNo = str1.substring(0, str1.indexOf(" ")).toInt(); - - Serial.print("Play file "); - Serial.println(fileNo); - } - else if (header.indexOf("GET /restart") >= 0) { - Serial.println("Restarting..."); - ESP.restart(); - } - - // Display the HTML web page - client.println(""); - client.println(""); - client.println(""); - client.println("Doorbell"); - client.println(""); - - client.println("

Doorbell

"); - client.println("

"); - client.println(""); - - // The HTTP response ends with another blank line - client.println(); - // call webhook after ringing - if (header.indexOf("GET /bell/on") >= 0) { - - if (http.begin(webClient, webhook)) { - int httpCode = http.GET(); - - if (httpCode > 0) { - if (httpCode == HTTP_CODE_OK) { - String payload = http.getString(); - //Serial.println(payload); - } - } - else { - Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); - } - // close connection - http.end(); - } - else { - Serial.println("Could not send HTTP request"); - } - } - - break; - } - else { // if you got a newline, then clear currentLine - currentLine = ""; - } - } - else if (c != '\r') { // if you got anything else but a carriage return character, - currentLine += c; // add it to the end of the currentLine - } +void trigger(int pinNo) { + Serial.print("Trigger "); + Serial.println(pinNo); + digitalWrite(pinNo, HIGH); + digitalWrite(PIN_LED, HIGH); + delay(400); + digitalWrite(pinNo, LOW); + digitalWrite(PIN_LED, LOW); +} + +unsigned long lastDebounceTime = 0; +unsigned long debounceDelay = 80; +int lastVal = LOW; + +void loop() { + + int newVal = digitalRead(PIN_GARAGE_BUTTON); + if (newVal != lastVal) { + lastDebounceTime = millis(); + while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GARAGE_BUTTON) != lastVal) { + delay(1); + } + if (digitalRead(PIN_GARAGE_BUTTON) != lastVal) { + lastVal = newVal; + if (newVal == HIGH) { + trigger(PIN_GARAGE); + } + } + } + + newVal = digitalRead(PIN_GATE_BUTTON); + if (newVal != lastVal) { + lastDebounceTime = millis(); + while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GATE_BUTTON) != lastVal) { + delay(1); + } + if (digitalRead(PIN_GATE_BUTTON) != lastVal) { + lastVal = newVal; + if (newVal == HIGH) { + trigger(PIN_GATE); } } - // Clear the header variable - header = ""; - // Close the connection - client.stop(); - Serial.println("Client disconnected"); - Serial.println(""); } }