From e118e68b3a9c2100b4aa8744f29f67a9c0c19c81 Mon Sep 17 00:00:00 2001 From: Tilman Date: Sat, 27 May 2023 14:24:20 +0200 Subject: [PATCH] garage_esp32 - v0.1 --- garage/garage_esp32/garage_esp32.ino | 72 +++++++++++++++------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/garage/garage_esp32/garage_esp32.ino b/garage/garage_esp32/garage_esp32.ino index 630db8b..61cb133 100644 --- a/garage/garage_esp32/garage_esp32.ino +++ b/garage/garage_esp32/garage_esp32.ino @@ -42,19 +42,21 @@ const char *PUSHOVER_ROOT_CA = "-----BEGIN CERTIFICATE-----\n" "-----END CERTIFICATE-----\n"; -const int PIN_LED = 2; // esp32 dev board integrated LED -const int PIN_GARAGE_BUTTON = 4; // button to open/close the garage door -const int PIN_GATE_BUTTON = 16; // button to open/close the gate -const int PIN_MOWER_ACTIVE = 15; // only contact mower if pin is HIGH -const int PIN_GARAGE = 17; // relais pin for garage door -const int PIN_GATE = 5; // relais pin for gate -const int PIN_DOOR_OPEN = 18; // sensor pin for garage door in position 'open' -const int PIN_DOOR_CLOSED = 19; // sensor pin for garage door in position 'closed' -const int PIN_SENSOR_GATE= 21; // sensor pin for gate (HIGH is open) -const int PIN_SPRINKLER1 = 26; // relais pin for sprinkler 1 -const int PIN_SPRINKLER2 = 25; // relais pin for sprinkler 2 -const int PIN_SPRINKLER3 = 33; // relais pin for sprinkler 3 -const int PIN_SPRINKLER4 = 32; // relais pin for sprinkler 4 +const int PIN_LED = 2; // esp32 dev board integrated LED +const int PIN_GARAGE_BUTTON = 4; // button to open/close the garage door +const int PIN_GATE_BUTTON = 16; // button to open/close the gate +const int PIN_MOWER_ACTIVE = 12; // only contact mower if pin is LOW +const int PIN_GARAGE = 22; // relais pin for garage door +const int PIN_GATE = 23; // relais pin for gate +const int PIN_DOOR_OPEN = 18; // sensor pin for garage door in position 'open' +const int PIN_DOOR_CLOSED = 19; // sensor pin for garage door in position 'closed' +const int PIN_SENSOR_GATE= 21; // sensor pin for gate (HIGH is open) +const int PIN_SPRINKLER1 = 26; // relais pin for sprinkler 1 +const int PIN_SPRINKLER2 = 25; // relais pin for sprinkler 2 +const int PIN_SPRINKLER3 = 33; // relais pin for sprinkler 3 +const int PIN_SPRINKLER4 = 32; // relais pin for sprinkler 4 +const int PIN_MOISTURE_ANALOG = 14; // moisture sensor analog value // XXX currently blocked by WiFi +const int PIN_MOISTURE_DIGITAL = 27; // moisture sensor digital value AsyncWebServer server(80); const char* PARAM_MESSAGE = "message"; @@ -111,7 +113,9 @@ void pinStatus(AsyncWebServerRequest *request) { "PIN_SPRINKLER1 (" + String(PIN_SPRINKLER1) + ") = " + String(digitalRead(PIN_SPRINKLER1)) + "\n" + "PIN_SPRINKLER2 (" + String(PIN_SPRINKLER2) + ") = " + String(digitalRead(PIN_SPRINKLER2)) + "\n" + "PIN_SPRINKLER3 (" + String(PIN_SPRINKLER3) + ") = " + String(digitalRead(PIN_SPRINKLER3)) + "\n" + - "PIN_SPRINKLER4 (" + String(PIN_SPRINKLER4) + ") = " + String(digitalRead(PIN_SPRINKLER4)) + "\n\n" + + "PIN_SPRINKLER4 (" + String(PIN_SPRINKLER4) + ") = " + String(digitalRead(PIN_SPRINKLER4)) + "\n" + + "PIN_MOISTURE_ANALOG (" + String(PIN_MOISTURE_ANALOG) + ") = " + String(analogRead(PIN_MOISTURE_ANALOG)) + "\n" + + "PIN_MOISTURE_DIGITAL (" + String(PIN_MOISTURE_DIGITAL) + ") = " + String(digitalRead(PIN_MOISTURE_DIGITAL)) + "\n\n" + "sprinklerDuration = " + String(sprinklerDuration) + "\n" + "sprinklerStartMillis = " + String(sprinklerStartMillis) + "\n\n" + "/ \n" + @@ -212,7 +216,7 @@ void triggerGate() { digitalWrite(PIN_LED, LOW); // DEAKTIVIEREN FÜR WINTERPAUSE: - if (digitalRead(PIN_MOWER_ACTIVE)) { + if (digitalRead(PIN_MOWER_ACTIVE) == LOW) { if (toSafety) { mowerSafety(); } @@ -243,18 +247,19 @@ void setup() { btStop(); // turn off bluetooth pinMode(PIN_LED, OUTPUT); - pinMode(PIN_GARAGE_BUTTON, INPUT); - pinMode(PIN_GATE_BUTTON, INPUT); - pinMode(PIN_MOWER_ACTIVE, INPUT); + pinMode(PIN_GARAGE_BUTTON, INPUT_PULLUP); + pinMode(PIN_GATE_BUTTON, INPUT_PULLUP); + pinMode(PIN_MOWER_ACTIVE, INPUT_PULLUP); pinMode(PIN_GARAGE, OUTPUT); pinMode(PIN_GATE, OUTPUT); - pinMode(PIN_DOOR_OPEN, INPUT); - pinMode(PIN_DOOR_CLOSED, INPUT); - pinMode(PIN_SENSOR_GATE, INPUT); + pinMode(PIN_DOOR_OPEN, INPUT_PULLUP); + pinMode(PIN_DOOR_CLOSED, INPUT_PULLUP); + pinMode(PIN_SENSOR_GATE, INPUT_PULLUP); pinMode(PIN_SPRINKLER1, OUTPUT); pinMode(PIN_SPRINKLER2, OUTPUT); pinMode(PIN_SPRINKLER3, OUTPUT); pinMode(PIN_SPRINKLER4, OUTPUT); + pinMode(PIN_MOISTURE_DIGITAL, INPUT); digitalWrite(PIN_GARAGE, HIGH); digitalWrite(PIN_GATE, HIGH); @@ -263,6 +268,7 @@ void setup() { digitalWrite(PIN_SPRINKLER3, HIGH); digitalWrite(PIN_SPRINKLER4, HIGH); + digitalWrite(PIN_LED, HIGH); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); @@ -271,6 +277,7 @@ void setup() { Serial.println(WiFi.localIP()); WiFi.setAutoReconnect(true); WiFi.persistent(true); + digitalWrite(PIN_LED, LOW); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ pinStatus(request); @@ -341,21 +348,22 @@ void setup() { request->send(200, "application/json", "{ \"action\": \"stop\" }"); } else if (action.equals("start") and (request->hasParam("device")) and (request->hasParam("duration"))) { + stopSprinklers(); // only run one sprinkler at a time String device = request->getParam("device")->value(); String duration = request->getParam("duration")->value(); sprinklerDuration = duration.toInt(); sprinklerStartMillis = millis(); if (device.equals("1")) { - digitalWrite(PIN_SPRINKLER1, HIGH); + digitalWrite(PIN_SPRINKLER1, LOW); } else if (device.equals("2")) { - digitalWrite(PIN_SPRINKLER2, HIGH); + digitalWrite(PIN_SPRINKLER2, LOW); } else if (device.equals("3")) { - digitalWrite(PIN_SPRINKLER3, HIGH); + digitalWrite(PIN_SPRINKLER3, LOW); } else if (device.equals("4")) { - digitalWrite(PIN_SPRINKLER4, HIGH); + digitalWrite(PIN_SPRINKLER4, LOW); } else { sprinklerDuration = 0; @@ -386,8 +394,8 @@ void setup() { unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 80; -int lastValGarage = LOW; -int lastValGate = LOW; +int lastValGarage = HIGH; +int lastValGate = HIGH; void loop() { @@ -397,15 +405,14 @@ void loop() { int newVal = digitalRead(PIN_GARAGE_BUTTON); if (newVal != lastValGarage) { - Serial.print("Garage Button: "); + Serial.println("Garage Button"); lastDebounceTime = millis(); while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GARAGE_BUTTON) != lastValGarage) { delay(1); } if (digitalRead(PIN_GARAGE_BUTTON) != lastValGarage) { - Serial.println("REAL"); lastValGarage = newVal; - if (newVal == HIGH) { + if (newVal == LOW) { triggerGarage(); } } @@ -416,15 +423,14 @@ void loop() { newVal = digitalRead(PIN_GATE_BUTTON); if (newVal != lastValGate) { - Serial.print("Gate Button: "); + Serial.print("Gate Button"); lastDebounceTime = millis(); while (((millis() - lastDebounceTime) < debounceDelay) and digitalRead(PIN_GATE_BUTTON) != lastValGate) { delay(1); } if (digitalRead(PIN_GATE_BUTTON) != lastValGate) { - Serial.println("REAL"); lastValGate = newVal; - if (newVal == HIGH) { + if (newVal == LOW) { triggerGate(); } }