🎨 pridany config na restart zariadenia v pripade hcyby, dokumentacia
This commit is contained in:
@@ -9,10 +9,13 @@
|
|||||||
constexpr bool DEBUG = true;
|
constexpr bool DEBUG = true;
|
||||||
|
|
||||||
// check heath interval
|
// 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
|
// Enable or disable the buzzer
|
||||||
constexpr bool buzzerEnabled = false;
|
constexpr bool buzzerEnabled = true;
|
||||||
|
|
||||||
// Enable or disable the LED
|
// Enable or disable the LED
|
||||||
constexpr bool ledEnabled = true;
|
constexpr bool ledEnabled = true;
|
||||||
@@ -29,7 +32,7 @@ constexpr auto ssid = "ElaZereVsetko2g";
|
|||||||
constexpr auto password = "CoJeElaToJeEla";
|
constexpr auto password = "CoJeElaToJeEla";
|
||||||
|
|
||||||
// API config
|
// 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 healthCheckUrl = "api/timer/healthcheck/"; // GET method
|
||||||
constexpr auto writeTimerUrl = "api/timer/write"; // POST method, need aditional url params /{timerID}/{chipUID}/{unixtime}.{milisec}/
|
constexpr auto writeTimerUrl = "api/timer/write"; // POST method, need aditional url params /{timerID}/{chipUID}/{unixtime}.{milisec}/
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,12 @@
|
|||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the unique board ID.
|
||||||
|
* it is generated from the ESP32 MAC address.
|
||||||
|
*
|
||||||
|
* @return String The unique board ID.
|
||||||
|
*/
|
||||||
String getBoardID()
|
String getBoardID()
|
||||||
{
|
{
|
||||||
String boardID = "";
|
String boardID = "";
|
||||||
@@ -14,6 +19,10 @@ String getBoardID()
|
|||||||
return boardID;
|
return boardID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Turn on the LED.
|
||||||
|
* only if the LED is enabled.
|
||||||
|
*/
|
||||||
void ledOn()
|
void ledOn()
|
||||||
{
|
{
|
||||||
if (ledEnabled)
|
if (ledEnabled)
|
||||||
@@ -22,6 +31,10 @@ void ledOn()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Turn off the LED.
|
||||||
|
* only if the LED is enabled.
|
||||||
|
*/
|
||||||
void ledOff()
|
void ledOff()
|
||||||
{
|
{
|
||||||
if (ledEnabled)
|
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()
|
void buzzerOn()
|
||||||
{
|
{
|
||||||
if (buzzerEnabled)
|
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()
|
void buzzerOff()
|
||||||
{
|
{
|
||||||
if (buzzerEnabled)
|
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 getCardUID(const MFRC522& mfrc522)
|
||||||
{
|
{
|
||||||
String uidString = "";
|
String uidString = "";
|
||||||
@@ -57,6 +86,12 @@ String getCardUID(const MFRC522& mfrc522)
|
|||||||
return uidString;
|
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)
|
void beepAndBlink(const int duration, const int count)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < count; i++)
|
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)
|
bool writeToAPI(const String& cardID, const unsigned long currentTime)
|
||||||
{
|
{
|
||||||
|
// clalculate the microseconds
|
||||||
const unsigned long microseconds = micros() % 1000000;
|
const unsigned long microseconds = micros() % 1000000;
|
||||||
|
|
||||||
char url[256];
|
char url[256];
|
||||||
@@ -89,17 +132,25 @@ bool writeToAPI(const String& cardID, const unsigned long currentTime)
|
|||||||
microseconds
|
microseconds
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
Serial.println(url);
|
Serial.println(url);
|
||||||
|
}
|
||||||
|
|
||||||
// Send the HTTP GET request
|
// Send the HTTP GET request
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
int httpResponseCode = http.GET();
|
const int httpResponseCode = http.GET();
|
||||||
http.end();
|
http.end();
|
||||||
|
|
||||||
return httpResponseCode == 200;
|
return httpResponseCode == 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check the health of the API.
|
||||||
|
*
|
||||||
|
* @return bool True if the API is healthy, false otherwise.
|
||||||
|
*/
|
||||||
bool checkAPI()
|
bool checkAPI()
|
||||||
{
|
{
|
||||||
// Send the HTTP GET request
|
// Send the HTTP GET request
|
||||||
@@ -107,8 +158,8 @@ bool checkAPI()
|
|||||||
char url[256];
|
char url[256];
|
||||||
snprintf(url, sizeof(url), "%s/%s", apiHost, healthCheckUrl);
|
snprintf(url, sizeof(url), "%s/%s", apiHost, healthCheckUrl);
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
int httpResponseCode = http.GET();
|
const int httpResponseCode = http.GET();
|
||||||
bool success = (httpResponseCode == 200);
|
const bool success = (httpResponseCode == 200);
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
{
|
{
|
||||||
Serial.println(success ? "API check: OK" : "API check: Failed");
|
Serial.println(success ? "API check: OK" : "API check: Failed");
|
||||||
@@ -117,6 +168,9 @@ bool checkAPI()
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Perform a health check of the system.
|
||||||
|
*/
|
||||||
void healthCheck()
|
void healthCheck()
|
||||||
{
|
{
|
||||||
bool check = false;
|
bool check = false;
|
||||||
@@ -125,14 +179,20 @@ void healthCheck()
|
|||||||
check = checkAPI();
|
check = checkAPI();
|
||||||
|
|
||||||
|
|
||||||
|
// If the health check was successful return
|
||||||
if (check)
|
if (check)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle the failure
|
||||||
|
if (resetOnFail)
|
||||||
|
{
|
||||||
// If the health check failed, beep and blink the LED
|
// If the health check failed, beep and blink the LED
|
||||||
beepAndBlink(40, 20);
|
beepAndBlink(40, 20);
|
||||||
ledOff();
|
ledOff();
|
||||||
// reset the board
|
// reset the board
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user