From 452346367f337666c853ffa238f8cf9d63ea9693 Mon Sep 17 00:00:00 2001 From: Roman Hajnala Date: Fri, 14 Mar 2025 14:28:17 +0100 Subject: [PATCH] :art: pridany config na restart zariadenia v pripade hcyby, dokumentacia --- src/config.h | 9 ++++-- src/utils.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/config.h b/src/config.h index d4caf43..d520c82 100644 --- a/src/config.h +++ b/src/config.h @@ -9,10 +9,13 @@ constexpr bool DEBUG = true; // check heath interval -constexpr int healthCheckInterval = 10000; +constexpr int healthCheckInterval = 30000; + +// Enable or disable the reset of the ESP32 when the API is not available or other fatal errors occur +constexpr bool resetOnFail = true; // Enable or disable the buzzer -constexpr bool buzzerEnabled = false; +constexpr bool buzzerEnabled = true; // Enable or disable the LED constexpr bool ledEnabled = true; @@ -29,7 +32,7 @@ constexpr auto ssid = "ElaZereVsetko2g"; constexpr auto password = "CoJeElaToJeEla"; // API config -constexpr auto apiHost = "http://192.168.50.87:8111"; +constexpr auto apiHost = "https://kalskeborec.cz"; constexpr auto healthCheckUrl = "api/timer/healthcheck/"; // GET method constexpr auto writeTimerUrl = "api/timer/write"; // POST method, need aditional url params /{timerID}/{chipUID}/{unixtime}.{milisec}/ diff --git a/src/utils.cpp b/src/utils.cpp index 088a458..f9426bf 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -3,7 +3,12 @@ #include - +/** + * @brief Get the unique board ID. + * it is generated from the ESP32 MAC address. + * + * @return String The unique board ID. + */ String getBoardID() { String boardID = ""; @@ -14,6 +19,10 @@ String getBoardID() return boardID; } +/** + * @brief Turn on the LED. + * only if the LED is enabled. + */ void ledOn() { if (ledEnabled) @@ -22,6 +31,10 @@ void ledOn() } } +/** + * @brief Turn off the LED. + * only if the LED is enabled. + */ void ledOff() { if (ledEnabled) @@ -30,6 +43,11 @@ void ledOff() } } +/** + * @brief Turn on the buzzer. + * only if the buzzer is enabled. + * there is used active buzzer so the buzzer is turned on by setting the pin to HIGH. + */ void buzzerOn() { if (buzzerEnabled) @@ -38,6 +56,11 @@ void buzzerOn() } } +/** + * @brief Turn off the buzzer. + * only if the buzzer is enabled. + * there is used active buzzer so the buzzer is turned on by setting the pin to HIGH. + */ void buzzerOff() { if (buzzerEnabled) @@ -46,6 +69,12 @@ void buzzerOff() } } +/** + * @brief Get the UID of an RFID card. + * + * @param mfrc522 Reference to the MFRC522 instance. + * @return String The UID of the card. + */ String getCardUID(const MFRC522& mfrc522) { String uidString = ""; @@ -57,6 +86,12 @@ String getCardUID(const MFRC522& mfrc522) return uidString; } +/** + * @brief Beep the buzzer and blink the LED. + * + * @param duration Duration of each beep/blink in milliseconds. + * @param count Number of beeps/blinks. + */ void beepAndBlink(const int duration, const int count) { for (int i = 0; i < count; i++) @@ -75,8 +110,16 @@ void beepAndBlink(const int duration, const int count) } } +/** + * @brief Write the card ID and current time to the API. + * + * @param cardID The UID of the card. + * @param currentTime The current time in Unix epoch format. + * @return bool True if the write was successful, false otherwise. + */ bool writeToAPI(const String& cardID, const unsigned long currentTime) { + // clalculate the microseconds const unsigned long microseconds = micros() % 1000000; char url[256]; @@ -89,17 +132,25 @@ bool writeToAPI(const String& cardID, const unsigned long currentTime) microseconds ); - Serial.println(url); + if (DEBUG) + { + Serial.println(url); + } // Send the HTTP GET request HTTPClient http; http.begin(url); - int httpResponseCode = http.GET(); + const int httpResponseCode = http.GET(); http.end(); return httpResponseCode == 200; } +/** + * @brief Check the health of the API. + * + * @return bool True if the API is healthy, false otherwise. + */ bool checkAPI() { // Send the HTTP GET request @@ -107,8 +158,8 @@ bool checkAPI() char url[256]; snprintf(url, sizeof(url), "%s/%s", apiHost, healthCheckUrl); http.begin(url); - int httpResponseCode = http.GET(); - bool success = (httpResponseCode == 200); + const int httpResponseCode = http.GET(); + const bool success = (httpResponseCode == 200); if (DEBUG) { Serial.println(success ? "API check: OK" : "API check: Failed"); @@ -117,6 +168,9 @@ bool checkAPI() return success; } +/** + * @brief Perform a health check of the system. + */ void healthCheck() { bool check = false; @@ -125,14 +179,20 @@ void healthCheck() check = checkAPI(); + // If the health check was successful return if (check) { return; } - // If the health check failed, beep and blink the LED - beepAndBlink(40, 20); - ledOff(); - // reset the board - ESP.restart(); + // handle the failure + if (resetOnFail) + { + // If the health check failed, beep and blink the LED + beepAndBlink(40, 20); + ledOff(); + // reset the board + ESP.restart(); + } + }