🚀 ready for testing

This commit is contained in:
2025-03-13 21:16:37 +01:00
parent 3195e3975d
commit 1d148063c9
5 changed files with 75 additions and 28 deletions

View File

@@ -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 |
| 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) |

View File

@@ -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}/

View File

@@ -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);
}

View File

@@ -2,16 +2,17 @@
#include "config.h"
#include <HTTPClient.h>
// 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();

View File

@@ -4,13 +4,15 @@
#include <Arduino.h>
#include <MFRC522v2.h>
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