🚀 ready for testing
This commit is contained in:
20
README.md
20
README.md
@@ -4,13 +4,13 @@
|
|||||||
[https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/](https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/)
|
[https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/](https://randomnerdtutorials.com/esp32-mfrc522-rfid-reader-arduino/)
|
||||||
|
|
||||||
|
|
||||||
| MFRC522 RFID Reader | ESP32 | Description | Color |
|
| MFRC522 RFID Reader | ESP32 | Description |
|
||||||
|---------------------|---------------|-------------|--------|
|
|---------------------|---------------|-------------|
|
||||||
| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input | blue |
|
| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input |
|
||||||
| SCK | GPIO 18 | SPI clock | brown |
|
| SCK | GPIO 18 | SPI clock |
|
||||||
| MOSI | GPIO 23 | SPI data input | grey |
|
| MOSI | GPIO 23 | SPI data input |
|
||||||
| MISO | GPIO 19 | SPI master-in-slave-out, I2C serial clock, or UART serial output | white |
|
| 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 | - |
|
| IRQ | Don't connect | Interrupt pin; signals the microcontroller when an RFID tag is nearby |
|
||||||
| GND | GND | Ground connection | black |
|
| 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 | yellow |
|
| 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) | orange |
|
| 3.3V | 3.3V | Power supply (2.5-3.3V) |
|
||||||
@@ -29,7 +29,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 = "http://192.168.50.87:8111";
|
||||||
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}/
|
||||||
|
|
||||||
|
|||||||
28
src/main.cpp
28
src/main.cpp
@@ -33,18 +33,23 @@ void setup() {
|
|||||||
while (!Serial); // Wait for the serial port to connect
|
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
|
// Connect to the Wi-Fi network
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
{
|
{
|
||||||
Serial.println(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
{
|
{
|
||||||
Serial.println("WiFi connected");
|
Serial.println(" WiFi connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize NTP client
|
// Initialize NTP client
|
||||||
@@ -65,6 +70,9 @@ void setup() {
|
|||||||
// Initialize the MFRC522 board
|
// Initialize the MFRC522 board
|
||||||
mfrc522.PCD_Init();
|
mfrc522.PCD_Init();
|
||||||
|
|
||||||
|
// Check if the reader is ready
|
||||||
|
|
||||||
|
|
||||||
// loop until the API is ready
|
// loop until the API is ready
|
||||||
while (!checkAPI()) {
|
while (!checkAPI()) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
@@ -76,9 +84,10 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Check the health of the system
|
// Check the health of the system and update the time
|
||||||
if (millis() - lastCheck > healthCheckInterval) {
|
if (millis() - lastCheck > healthCheckInterval) {
|
||||||
healthCheck();
|
healthCheck();
|
||||||
|
timeClient.update();
|
||||||
lastCheck = millis();
|
lastCheck = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,10 +102,15 @@ void loop() {
|
|||||||
return; // Exit the loop if no card could be read
|
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
|
// Read and print the UID of the card
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
try {
|
try {
|
||||||
Serial.println(timeClient.getFormattedTime() + " - " + getCardUID(mfrc522));
|
Serial.println(String(epochTIme) + " - " + getCardUID(mfrc522));
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
Serial.println("Error reading the card UID: " + String(e.what()));
|
Serial.println("Error reading the card UID: " + String(e.what()));
|
||||||
}
|
}
|
||||||
@@ -105,6 +119,12 @@ void loop() {
|
|||||||
// Perform additional actions when a card is read
|
// Perform additional actions when a card is read
|
||||||
beepAndBlink(1000, 1);
|
beepAndBlink(1000, 1);
|
||||||
|
|
||||||
|
const bool ok = writeToAPI(cardUID, epochTIme);
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
beepAndBlink(40, 2);
|
||||||
|
}
|
||||||
|
|
||||||
// Delay to avoid reading the same card multiple times
|
// Delay to avoid reading the same card multiple times
|
||||||
delay(afterReadDelay);
|
delay(afterReadDelay);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,17 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <HTTPClient.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()
|
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()
|
bool checkAPI()
|
||||||
{
|
{
|
||||||
// Send the HTTP GET request
|
// Send the HTTP GET request
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
char url[256]; // Make sure this is large enough for your URL
|
char url[256];
|
||||||
snprintf(url, sizeof(url), "%s%s", apiHost, healthCheckUrl);
|
snprintf(url, sizeof(url), "%s/%s", apiHost, healthCheckUrl);
|
||||||
Serial.println(url);
|
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
int httpResponseCode = http.GET();
|
int httpResponseCode = http.GET();
|
||||||
bool success = (httpResponseCode == 200);
|
bool success = (httpResponseCode == 200);
|
||||||
@@ -106,7 +131,7 @@ void healthCheck()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the health check failed, beep and blink the LED
|
// If the health check failed, beep and blink the LED
|
||||||
beepAndBlink(10, 50);
|
beepAndBlink(40, 20);
|
||||||
ledOff();
|
ledOff();
|
||||||
// reset the board
|
// reset the board
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
|||||||
@@ -4,13 +4,15 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <MFRC522v2.h>
|
#include <MFRC522v2.h>
|
||||||
|
|
||||||
|
String getBoardID();
|
||||||
void ledOn();
|
void ledOn();
|
||||||
void ledOff();
|
void ledOff();
|
||||||
void buzzerOn();
|
void buzzerOn();
|
||||||
void buzzerOff();
|
void buzzerOff();
|
||||||
String getCardUID(const MFRC522 &mfrc522);
|
String getCardUID(const MFRC522& mfrc522);
|
||||||
void beepAndBlink(int duration = 1000, int count = 1);
|
void beepAndBlink(int duration = 1000, int count = 1);
|
||||||
bool checkAPI();
|
bool checkAPI();
|
||||||
|
bool writeToAPI(const String& cardID, unsigned long currentTime);
|
||||||
void healthCheck();
|
void healthCheck();
|
||||||
|
|
||||||
#endif // UTILS_H
|
#endif // UTILS_H
|
||||||
Reference in New Issue
Block a user