/* * Sources: * https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-button/ * */ #include #include #include #include const char* ssid = "LeWe"; const char* password = "5275274365464843"; 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 = 5; // button to open/close the gate const int PIN_MOWER_ACTIVE = 15; // only contact mower if pin is HIGH const int PIN_GARAGE = 18; // relais pin for garage door const int PIN_GATE = 19; // relais pin for gate const int PIN_DOOR_OPEN = 21; // sensor pin for garage door in position 'open' const int PIN_DOOR_CLOSED = 22; // sensor pin for garage door in position 'closed' const int PIN_SENSOR_GATE= 23; // sensor pin for gate (HIGH is open) const int PIN_SPRINKLER1 = 25; // relais pin for sprinkler 1 const int PIN_SPRINKLER2 = 26; // relais pin for sprinkler 2 const int PIN_SPRINKLER3 = 27; // relais pin for sprinkler 3 const int PIN_SPRINKLER4 = 14; // relais pin for sprinkler 4 AsyncWebServer server(80); const char* PARAM_MESSAGE = "message"; WiFiClient webClient; HTTPClient http; int sprinklerDuration = 0; unsigned long sprinklerStartMillis; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } void pinStatus(AsyncWebServerRequest *request) { String status = "esp32garage\n\nPIN_LED (" + String(PIN_LED) + ") = " + String(digitalRead(PIN_LED)) + "\n" + "PIN_GARAGE_BUTTON (" + String(PIN_GARAGE_BUTTON) + ") = " + String(digitalRead(PIN_GARAGE_BUTTON)) + "\n" + "PIN_GATE_BUTTON (" + String(PIN_GATE_BUTTON) + ") = " + String(digitalRead(PIN_GATE_BUTTON)) + "\n" + "PIN_MOWER_ACTIVE (" + String(PIN_MOWER_ACTIVE) + ") = " + String(digitalRead(PIN_MOWER_ACTIVE)) + "\n" + "PIN_GARAGE (" + String(PIN_GARAGE) + ") = " + String(digitalRead(PIN_GARAGE)) + "\n" + "PIN_GATE (" + String(PIN_GATE) + ") = " + String(digitalRead(PIN_GATE)) + "\n" + "PIN_DOOR_OPEN (" + String(PIN_DOOR_OPEN) + ") = " + String(digitalRead(PIN_DOOR_OPEN)) + "\n" + "PIN_DOOR_CLOSED (" + String(PIN_DOOR_CLOSED) + ") = " + String(digitalRead(PIN_DOOR_CLOSED)) + "\n" + "PIN_SENSOR_GATE (" + String(PIN_SENSOR_GATE) + ") = " + String(digitalRead(PIN_SENSOR_GATE)) + "\n" + "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" + "/ \n" + "/garage[?action=(open|close)] \n" + "/gate[?action=(open|close)] \n" + "/sprinkler[?action=(stop|(start&device={int}&duration={int}))]"; request->send(200, "text/plain", status); } void mowerSafety() { Serial.println("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(); String statusStr = payload.substring(payload.indexOf("\"status\": {\"status\": ") + 21); statusStr = statusStr.substring(0, statusStr.indexOf(",")); Serial.println(statusStr); if (statusStr.equals("2") or statusStr.equals("3") or statusStr.equals("5") or statusStr.equals("17")) { Serial.println("stop mower"); if (http.begin(webClient, "http://192.168.178.54/json?cmd=stop&user=lewe&pass=nFdcX3sMD115WAap")) { int httpCode = http.GET(); if (httpCode < 0) { Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.println("Could not send HTTP request (stop) to mower"); } } else { Serial.println("no status to be stopped"); } } } else { Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.println("Could not send HTTP request to mower"); } } void mowerAuto() { Serial.println("mowerAuto()"); if (http.begin(webClient, "http://192.168.178.54/json?cmd=mode&mode=auto&user=lewe&pass=nFdcX3sMD115WAap")) { int httpCode = http.GET(); if (httpCode < 0) { Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.println("Could not send HTTP request (auto mode) to mower"); } } void triggerGarage() { Serial.println("Trigger garage door"); digitalWrite(PIN_GARAGE, HIGH); digitalWrite(PIN_LED, HIGH); delay(400); digitalWrite(PIN_GARAGE, LOW); digitalWrite(PIN_LED, LOW); } void triggerGate() { Serial.println("Trigger gate"); bool toSafety = false; if (digitalRead(PIN_SENSOR_GATE)) { toSafety = true; } digitalWrite(PIN_GATE, HIGH); digitalWrite(PIN_LED, HIGH); delay(400); digitalWrite(PIN_GATE, LOW); digitalWrite(PIN_LED, LOW); // DEAKTIVIEREN FÜR WINTERPAUSE: if (digitalRead(PIN_MOWER_ACTIVE)) { if (toSafety) { mowerSafety(); } else { mowerAuto(); } } } void stopSprinklers() { Serial.println("Stopping sprinklers"); digitalWrite(PIN_SPRINKLER1, LOW); digitalWrite(PIN_SPRINKLER2, LOW); digitalWrite(PIN_SPRINKLER3, LOW); digitalWrite(PIN_SPRINKLER4, LOW); sprinklerDuration = 0; } void setup() { // start serial output Serial.begin(115200); Serial.println(); 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, OUTPUT); pinMode(PIN_GATE, OUTPUT); pinMode(PIN_DOOR_OPEN, INPUT); pinMode(PIN_DOOR_CLOSED, INPUT); pinMode(PIN_SENSOR_GATE, INPUT); pinMode(PIN_SPRINKLER1, OUTPUT); pinMode(PIN_SPRINKLER2, OUTPUT); pinMode(PIN_SPRINKLER3, OUTPUT); pinMode(PIN_SPRINKLER4, OUTPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println(WiFi.localIP()); WiFi.setAutoReconnect(true); WiFi.persistent(true); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ pinStatus(request); }); 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\" }"); triggerGarage(); } 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 { 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.on("/sprinkler", HTTP_GET, [] (AsyncWebServerRequest *request) { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); if (action.equals("stop")) { stopSprinklers(); request->send(200, "application/json", "{ \"action\": \"stop\" }"); } else if (action.equals("start") and (request->hasParam("device")) and (request->hasParam("duration"))) { String device = request->getParam("device")->value(); String duration = request->getParam("duration")->value(); sprinklerDuration = duration.toInt(); sprinklerStartMillis = millis(); if (device == 1) { digitalWrite(PIN_SPRINKLER1, HIGH); } else if (device == 2) { digitalWrite(PIN_SPRINKLER2, HIGH); } else if (device == 3) { digitalWrite(PIN_SPRINKLER3, HIGH); } else if (device == 4) { digitalWrite(PIN_SPRINKLER4, HIGH); } else { sprinklerDuration = 0; } // HIER GEHT'S WEITER 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\" }"); } } }); // timer server.onNotFound(notFound); server.begin(); Serial.println("Setup completed"); } unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 80; int lastVal = LOW; void loop() { if ((sprinklerDuration > 0) and ((millis() - sprinklerStartMillis) > sprinklerDuration)) { stopSprinklers(); } 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) { triggerGarage(); } } } 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) { triggerGate(); } } } }