From 2da7555ce1d74a03f2605a6627a2beb068dfc590 Mon Sep 17 00:00:00 2001 From: tilman Date: Fri, 17 Jul 2026 22:43:55 +0000 Subject: [PATCH] garage_esp32.ino bugfixes --- garage/garage_esp32/garage_esp32.ino | 117 +++++++++++---------------- 1 file changed, 46 insertions(+), 71 deletions(-) diff --git a/garage/garage_esp32/garage_esp32.ino b/garage/garage_esp32/garage_esp32.ino index b002ca0..8a3293c 100644 --- a/garage/garage_esp32/garage_esp32.ino +++ b/garage/garage_esp32/garage_esp32.ino @@ -13,14 +13,14 @@ #include #include -const char* ssid = "LeWe"; +const char* ssid = "LeWe"; const char* password = "5275274365464843"; const char* apiToken = "a9k1uyzypuaz1yszqqdzmp17s6r5q2"; const char* userToken = "ukja84pd2n2cmxvdrnfwdrf8eo1wc5"; const char* pushoverApiEndpoint = "https://api.pushover.net/1/messages.json"; -const char *PUSHOVER_ROOT_CA = "-----BEGIN CERTIFICATE-----\n" +const char* PUSHOVER_ROOT_CA = "-----BEGIN CERTIFICATE-----\n" "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" @@ -75,7 +75,7 @@ void pushover(String message) { StaticJsonDocument<512> notification; notification["token"] = apiToken; notification["user"] = userToken; - notification["title"] = "Garage"; //optional + notification["title"] = "Garage"; //optional notification["message"] = message; // Serialize the JSON object to a string @@ -100,7 +100,7 @@ void pushover(String message) { https.end(); } -void notFound(AsyncWebServerRequest *request) { +void notFound(AsyncWebServerRequest* request) { request->send(404, "text/plain", "Not found"); } @@ -127,7 +127,7 @@ void pinStatus(AsyncWebServerRequest *request) { "/garage[?action=(open|close)] \n" + "/gate[?action=(open|close)] \n" + "/sprinkler[?action=(stop|(start&device={int}&duration={int}))] \n" + - "/sprinkler/program[?action=(stop|(start&duration={int}))] \n"; + "/irrigation[?action=(stop|(start&duration={int}))] \n"; request->send(200, "text/plain", status); } @@ -156,24 +156,20 @@ void mowerSafety() { pushover("Mower (STOP) HTTP Error: " + http.errorToString(httpCode)); } http.end(); - } - else { + } else { Serial.println("Could not send HTTP request (STOP) to mower"); pushover("Could not send HTTP request (STOP) to mower"); } - } - else { + } else { Serial.println("no status to be stopped"); } } - } - else { + } else { Serial.printf("Mower (STATUS) HTTP Error: ", http.errorToString(httpCode).c_str()); pushover("Mower (STATUS) HTTP Error: " + http.errorToString(httpCode)); } http.end(); - } - else { + } else { Serial.println("Could not send HTTP request (STATUS) to mower"); pushover("Could not send HTTP request (STATUS) to mower"); } @@ -190,8 +186,7 @@ void mowerAuto() { pushover("Mower (AUTO) HTTP Error: " + http.errorToString(httpCode)); } http.end(); - } - else { + } else { Serial.println("Could not send HTTP request (AUTO) to mower"); pushover("Could not send HTTP request (AUTO) to mower"); } @@ -225,12 +220,10 @@ void triggerGate() { if (digitalRead(PIN_MOWER_ACTIVE) == LOW) { if (toSafety) { mowerSafety(); - } - else { + } else { mowerAuto(); } - } - else { + } else { Serial.println("Mower set to inactive"); } } @@ -249,7 +242,7 @@ void setup() { Serial.begin(115200); Serial.println(); - btStop(); // turn off bluetooth + btStop(); // turn off bluetooth pinMode(PIN_LED, OUTPUT); pinMode(PIN_GARAGE_BUTTON, INPUT_PULLUP); @@ -281,14 +274,15 @@ void setup() { Serial.println("Connecting to WiFi..."); } Serial.println(WiFi.localIP()); + Serial.println(WiFi.macAddress()); WiFi.setAutoReconnect(true); WiFi.persistent(true); - server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { + server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { pinStatus(request); }); - server.on("/garage", HTTP_GET, [] (AsyncWebServerRequest * request) { + server.on("/garage", HTTP_GET, [](AsyncWebServerRequest* request) { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); @@ -296,26 +290,22 @@ void setup() { if (action.equals("open") and digitalRead(PIN_DOOR_CLOSED) and not digitalRead(PIN_DOOR_OPEN)) { request->send(200, "application/json", "{ \"action\": \"open\" }"); triggerGarage(); - } - else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) { + } else if (action.equals("close") and digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) { request->send(200, "application/json", "{ \"action\": \"close\" }"); triggerGarage(); - } - else { + } else { pinStatus(request); } - } - else { + } else { if (digitalRead(PIN_DOOR_OPEN) and not digitalRead(PIN_DOOR_CLOSED)) { request->send(200, "application/json", "{ \"status\": \"true\" }"); - } - else { + } else { request->send(200, "application/json", "{ \"status\": \"false\" }"); } } }); - server.on("/gate", HTTP_GET, [] (AsyncWebServerRequest * request) { + server.on("/gate", HTTP_GET, [](AsyncWebServerRequest* request) { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); @@ -323,33 +313,29 @@ void setup() { if (action.equals("open") and not digitalRead(PIN_SENSOR_GATE)) { request->send(200, "application/json", "{ \"action\": \"open\" }"); triggerGate(); - } - else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) { + } else if (action.equals("close") and digitalRead(PIN_SENSOR_GATE)) { request->send(200, "application/json", "{ \"action\": \"close\" }"); triggerGate(); - } - else { + } else { pinStatus(request); } - } - else { + } else { if (digitalRead(PIN_SENSOR_GATE)) { request->send(200, "application/json", "{ \"status\": \"true\" }"); - } - else { + } else { request->send(200, "application/json", "{ \"status\": \"false\" }"); } } }); - server.on("/sprinkler", HTTP_GET, [] (AsyncWebServerRequest * request) { + server.on("/sprinkler", HTTP_GET, [](AsyncWebServerRequest* request) { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); Serial.println("Sprinkler " + action); if (action.equals("start") and (request->hasParam("device")) and (request->hasParam("duration"))) { - stopSprinklers(); // only run one sprinkler at a time + stopSprinklers(); // only run one sprinkler at a time String device = request->getParam("device")->value(); String duration = request->getParam("duration")->value(); sprinklerDuration = duration.toInt(); @@ -357,34 +343,29 @@ void setup() { sprinklerStartMillis = millis(); if (device.equals("1")) { digitalWrite(PIN_SPRINKLER1, LOW); - } - else if (device.equals("2")) { + } else if (device.equals("2")) { digitalWrite(PIN_SPRINKLER2, LOW); - } - else if (device.equals("3")) { + } else if (device.equals("3")) { digitalWrite(PIN_SPRINKLER3, LOW); - } - else if (device.equals("4")) { + } else if (device.equals("4")) { digitalWrite(PIN_SPRINKLER4, LOW); - } - else { + } else { sprinklerDuration = 0; } request->send(200, "application/json", "{ \"action\": \"start\" }"); - } - else { + } else { stopSprinklers(); request->send(200, "application/json", "{ \"action\": \"stop\" }"); } - } - else { + } else { stopSprinklers(); request->send(200, "application/json", "{ \"action\": \"stop\" }"); } + programStatus = 0; }); - server.on("/irrigation", HTTP_GET, [] (AsyncWebServerRequest * request) { + server.on("/irrigation", HTTP_GET, [](AsyncWebServerRequest* request) { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); @@ -392,13 +373,12 @@ void setup() { if (action.equals("start") and request->hasParam("duration")) { String duration = request->getParam("duration")->value(); sprinklerDuration = duration.toInt(); - sprinklerDuration = sprinklerDuration * 60 * 500; // XXX Terrasse nur halb so lange bewässern + sprinklerDuration = sprinklerDuration * 60 * 500; // XXX Terrasse nur halb so lange bewässern sprinklerStartMillis = millis(); programStatus = 4; digitalWrite(PIN_SPRINKLER4, LOW); request->send(200, "application/json", "{ \"action\": \"start\" }"); - } - else { + } else { stopSprinklers(); programStatus = 0; request->send(200, "application/json", "{ \"action\": \"stop\" }"); @@ -430,26 +410,23 @@ void loop() { if ((sprinklerDuration > 0) and ((millis() - sprinklerStartMillis) > sprinklerDuration)) { stopSprinklers(); - if (programStatus > 0) { - + if (programStatus > 1) { + if (programStatus == 4) { - sprinklerDuration = sprinklerDuration * 2; // XXX Terrasse nur halb so lange bewässern + sprinklerDuration = sprinklerDuration * 2; // XXX Terrasse nur halb so lange bewässern } - + programStatus--; sprinklerStartMillis = millis(); if (programStatus == 3) { digitalWrite(PIN_SPRINKLER3, LOW); - } - else if (programStatus == 2) { + } else if (programStatus == 2) { digitalWrite(PIN_SPRINKLER2, LOW); - } - else if (programStatus == 1) { + } else if (programStatus == 1) { digitalWrite(PIN_SPRINKLER1, LOW); } - } - else { + } else { sprinklerDuration = 0; sprinklerStartMillis = 0; } @@ -467,8 +444,7 @@ void loop() { if (newVal == LOW) { triggerGarage(); } - } - else { + } else { Serial.println("fluke"); } } @@ -485,8 +461,7 @@ void loop() { if (newVal == LOW) { triggerGate(); } - } - else { + } else { Serial.println("fluke"); } }