From 1d148063c954edfa462289ff910c669ba9f53a2d Mon Sep 17 00:00:00 2001 From: Roman Hajnala Date: Thu, 13 Mar 2025 21:16:37 +0100 Subject: [PATCH] :rocket: ready for testing --- README.md | 20 ++++++++++---------- src/config.h | 2 +- src/main.cpp | 28 ++++++++++++++++++++++++---- src/utils.cpp | 49 +++++++++++++++++++++++++++++++++++++------------ src/utils.h | 4 +++- 5 files changed, 75 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 52a3343..4fc7718 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ [https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/](https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/) -| MFRC522 RFID Reader | ESP32 | Description | Color | -|---------------------|---------------|-------------|--------| -| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input | blue | -| SCK | GPIO 18 | SPI clock | brown | -| MOSI | GPIO 23 | SPI data input | grey | -| MISO | GPIO 19 | SPI master-in-slave-out, I2C serial clock, or UART serial output | white | -| IRQ | Don't connect | Interrupt pin; signals the microcontroller when an RFID tag is nearby | - | -| GND | GND | Ground connection | black | -| RST | GPIO 21 | LOW signal to put the module in power-down mode; send a HIGH signal to reset the module | yellow | -| 3.3V | 3.3V | Power supply (2.5-3.3V) | orange | \ No newline at end of file +| MFRC522 RFID Reader | ESP32 | Description | +|---------------------|---------------|-------------| +| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input | +| SCK | GPIO 18 | SPI clock | +| MOSI | GPIO 23 | SPI data input | +| MISO | GPIO 19 | SPI master-in-slave-out, I2C serial clock, or UART serial output | +| IRQ | Don't connect | Interrupt pin; signals the microcontroller when an RFID tag is nearby | +| GND | GND | Ground connection | +| RST | GPIO 21 | LOW signal to put the module in power-down mode; send a HIGH signal to reset the module | +| 3.3V | 3.3V | Power supply (2.5-3.3V) | \ No newline at end of file diff --git a/src/config.h b/src/config.h index 576a594..d4caf43 100644 --- a/src/config.h +++ b/src/config.h @@ -29,7 +29,7 @@ constexpr auto ssid = "ElaZereVsetko2g"; constexpr auto password = "CoJeElaToJeEla"; // API config -constexpr auto apiHost = "http://192.168.50.87:8111/"; +constexpr auto apiHost = "http://192.168.50.87:8111"; 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/main.cpp b/src/main.cpp index 9513a87..7b06a6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,18 +33,23 @@ void setup() { while (!Serial); // Wait for the serial port to connect } + // print the board ID + if (DEBUG) { + Serial.println("Board ID: " + getBoardID()); + } + // Connect to the Wi-Fi network WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); if (DEBUG) { - Serial.println("."); + Serial.print("."); } } if (DEBUG) { - Serial.println("WiFi connected"); + Serial.println(" WiFi connected"); } // Initialize NTP client @@ -65,6 +70,9 @@ void setup() { // Initialize the MFRC522 board mfrc522.PCD_Init(); + // Check if the reader is ready + + // loop until the API is ready while (!checkAPI()) { delay(1000); @@ -76,9 +84,10 @@ void setup() { } void loop() { - // Check the health of the system + // Check the health of the system and update the time if (millis() - lastCheck > healthCheckInterval) { healthCheck(); + timeClient.update(); lastCheck = millis(); } @@ -93,10 +102,15 @@ void loop() { return; // Exit the loop if no card could be read } + + // get cardUID and current time + String cardUID = getCardUID(mfrc522); + const unsigned long epochTIme = timeClient.getEpochTime(); + // Read and print the UID of the card if (DEBUG) { try { - Serial.println(timeClient.getFormattedTime() + " - " + getCardUID(mfrc522)); + Serial.println(String(epochTIme) + " - " + getCardUID(mfrc522)); } catch (const std::exception &e) { Serial.println("Error reading the card UID: " + String(e.what())); } @@ -105,6 +119,12 @@ void loop() { // Perform additional actions when a card is read beepAndBlink(1000, 1); + const bool ok = writeToAPI(cardUID, epochTIme); + if (ok) + { + beepAndBlink(40, 2); + } + // Delay to avoid reading the same card multiple times delay(afterReadDelay); } diff --git a/src/utils.cpp b/src/utils.cpp index 29f9860..088a458 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,16 +2,17 @@ #include "config.h" #include -// Enable or disable the buzzer -extern const bool buzzerEnabled; -// Enable or disable the LED -extern const bool ledEnabled; - -// Define the pins for the buzzer and the LED -extern const int buzzerPin; -extern const int ledPin; +String getBoardID() +{ + String boardID = ""; + for (int i = 0; i < 6; i++) + { + boardID += String(ESP.getEfuseMac() >> (40 - i * 8) & 0xff, 16); + } + return boardID; +} void ledOn() { @@ -74,13 +75,37 @@ void beepAndBlink(const int duration, const int count) } } +bool writeToAPI(const String& cardID, const unsigned long currentTime) +{ + const unsigned long microseconds = micros() % 1000000; + + char url[256]; + snprintf(url, sizeof(url), "%s/%s/%s/%s/%d.%d/", + apiHost, + writeTimerUrl, + getBoardID().c_str(), + cardID.c_str(), + currentTime - 3600, // Subtract one hour from the current time + microseconds + ); + + Serial.println(url); + + // Send the HTTP GET request + HTTPClient http; + http.begin(url); + int httpResponseCode = http.GET(); + http.end(); + + return httpResponseCode == 200; +} + bool checkAPI() { // Send the HTTP GET request HTTPClient http; - char url[256]; // Make sure this is large enough for your URL - snprintf(url, sizeof(url), "%s%s", apiHost, healthCheckUrl); - Serial.println(url); + char url[256]; + snprintf(url, sizeof(url), "%s/%s", apiHost, healthCheckUrl); http.begin(url); int httpResponseCode = http.GET(); bool success = (httpResponseCode == 200); @@ -106,7 +131,7 @@ void healthCheck() } // If the health check failed, beep and blink the LED - beepAndBlink(10, 50); + beepAndBlink(40, 20); ledOff(); // reset the board ESP.restart(); diff --git a/src/utils.h b/src/utils.h index 22714aa..7dccffe 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,13 +4,15 @@ #include #include +String getBoardID(); void ledOn(); void ledOff(); void buzzerOn(); void buzzerOff(); -String getCardUID(const MFRC522 &mfrc522); +String getCardUID(const MFRC522& mfrc522); void beepAndBlink(int duration = 1000, int count = 1); bool checkAPI(); +bool writeToAPI(const String& cardID, unsigned long currentTime); void healthCheck(); #endif // UTILS_H \ No newline at end of file