From 971b376bdf8c5c6be4445eaa1e597790159d356d Mon Sep 17 00:00:00 2001 From: Tilman Date: Sun, 21 May 2023 20:54:46 +0200 Subject: [PATCH] garage_esp32.ino v1.0 (untested) with Pushover notifications --- garage/garage_esp32/garage_esp32.ino | 105 ++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 18 deletions(-) diff --git a/garage/garage_esp32/garage_esp32.ino b/garage/garage_esp32/garage_esp32.ino index 791b2d4..8c843d6 100644 --- a/garage/garage_esp32/garage_esp32.ino +++ b/garage/garage_esp32/garage_esp32.ino @@ -1,19 +1,47 @@ /* * Sources: * https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-button/ - * + * https://randomnerdtutorials.com/esp32-pushover-notifications-arduino/ */ - #include #include #include #include - +#include +#include 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" + "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" + "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" + "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" + "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" + "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" + "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" + "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" + "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" + "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" + "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" + "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" + "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" + "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" + "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" + "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" + "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" + "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" + "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" + "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" + "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\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 = 5; // button to open/close the gate @@ -23,7 +51,7 @@ 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_SPRINKLER1 = 16; //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 @@ -34,9 +62,37 @@ const char* PARAM_MESSAGE = "message"; WiFiClient webClient; HTTPClient http; -int sprinklerDuration = 0; +unsigned long sprinklerDuration = 0; unsigned long sprinklerStartMillis; +void pushover(String message) { + StaticJsonDocument<512> notification; + notification["token"] = apiToken; + notification["user"] = userToken; + notification["title"] = "Garage"; //optional + notification["message"] = message; + + // Serialize the JSON object to a string + String jsonStringNotification; + serializeJson(notification, jsonStringNotification); + + WiFiClientSecure client; + client.setCACert(PUSHOVER_ROOT_CA); + + HTTPClient https; + https.begin(client, pushoverApiEndpoint); + https.addHeader("Content-Type", "application/json"); + int httpResponseCode = https.POST(jsonStringNotification); + + if (httpResponseCode > 0) { + String response = https.getString(); + Serial.println("Pushover response: " + String(httpResponseCode) + ", " + String(response)); + } else { + Serial.printf("Pushover HTTP response code: %d\n", httpResponseCode); + } + + https.end(); +} void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); @@ -56,6 +112,8 @@ void pinStatus(AsyncWebServerRequest *request) { "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" + + "sprinklerDuration = " + String(sprinklerDuration) + "\n" + + "sprinklerStartMillis = " + String(sprinklerStartMillis) + "\n\n" + "/ \n" + "/garage[?action=(open|close)] \n" + "/gate[?action=(open|close)] \n" + @@ -79,17 +137,19 @@ void mowerSafety() { Serial.println(statusStr); if (statusStr.equals("2") or statusStr.equals("3") or statusStr.equals("5") or statusStr.equals("17")) { - Serial.println("stop mower"); + 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()); + Serial.printf("Mower (STOP) HTTP Error: ", http.errorToString(httpCode).c_str()); + pushover("Mower (STOP) HTTP Error: " + http.errorToString(httpCode)); } http.end(); } else { - Serial.println("Could not send HTTP request (stop) to mower"); + Serial.println("Could not send HTTP request (STOP) to mower"); + pushover("Could not send HTTP request (STOP) to mower"); } } else { @@ -98,12 +158,14 @@ void mowerSafety() { } } else { - Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); + Serial.printf("Mower (STATUS) HTTP Error: ", http.errorToString(httpCode).c_str()); + pushover("Mower (STATUS) HTTP Error: " + http.errorToString(httpCode)); } http.end(); } else { - Serial.println("Could not send HTTP request to mower"); + Serial.println("Could not send HTTP request (STATUS) to mower"); + pushover("Could not send HTTP request (STATUS) to mower"); } } @@ -114,12 +176,14 @@ void mowerAuto() { int httpCode = http.GET(); if (httpCode < 0) { - Serial.printf("HTTP Error: ", http.errorToString(httpCode).c_str()); + Serial.printf("Mower (AUTO) HTTP Error: ", http.errorToString(httpCode).c_str()); + pushover("Mower (AUTO) HTTP Error: " + http.errorToString(httpCode)); } http.end(); } else { - Serial.println("Could not send HTTP request (auto mode) to mower"); + Serial.println("Could not send HTTP request (AUTO) to mower"); + pushover("Could not send HTTP request (AUTO) to mower"); } } @@ -156,6 +220,9 @@ void triggerGate() { mowerAuto(); } } + else { + Serial.println("Mower set to inactive"); + } } void stopSprinklers() { @@ -236,9 +303,11 @@ 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)) { request->send(200, "application/json", "{ \"action\": \"close\" }"); + triggerGate(); } else { pinStatus(request); @@ -258,6 +327,7 @@ void setup() { String action; if (request->hasParam("action")) { action = request->getParam("action")->value(); + Serial.println("Sprinkler " + action); if (action.equals("stop")) { stopSprinklers(); @@ -268,24 +338,23 @@ void setup() { String duration = request->getParam("duration")->value(); sprinklerDuration = duration.toInt(); sprinklerStartMillis = millis(); - if (device == 1) { + if (device.equals("1")) { digitalWrite(PIN_SPRINKLER1, HIGH); } - else if (device == 2) { + else if (device.equals("2")) { digitalWrite(PIN_SPRINKLER2, HIGH); } - else if (device == 3) { + else if (device.equals("3")) { digitalWrite(PIN_SPRINKLER3, HIGH); } - else if (device == 4) { + else if (device.equals("4")) { digitalWrite(PIN_SPRINKLER4, HIGH); } else { sprinklerDuration = 0; } - // HIER GEHT'S WEITER - request->send(200, "application/json", "{ \"action\": \"close\" }"); + request->send(200, "application/json", "{ \"action\": \"start\" }"); } else { pinStatus(request);