🎨 vycistene + separatne subory pre config a metody + healtcheck
This commit is contained in:
38
src/config.h
Normal file
38
src/config.h
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by Roman Hajnala on 13/03/2025.
|
||||
//
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
// Enable or disable debug mode
|
||||
constexpr bool DEBUG = true;
|
||||
|
||||
// check heath interval
|
||||
constexpr int healthCheckInterval = 10000;
|
||||
|
||||
// Enable or disable the buzzer
|
||||
constexpr bool buzzerEnabled = false;
|
||||
|
||||
// Enable or disable the LED
|
||||
constexpr bool ledEnabled = true;
|
||||
|
||||
// Delay after reading a card to avoid reading the same card multiple times
|
||||
constexpr int afterReadDelay = 100;
|
||||
|
||||
// Define the pins for the buzzer and the LED
|
||||
constexpr int buzzerPin = 2;
|
||||
constexpr int ledPin = 4;
|
||||
|
||||
// Wi-Fi credentials
|
||||
constexpr auto ssid = "ElaZereVsetko2g";
|
||||
constexpr auto password = "CoJeElaToJeEla";
|
||||
|
||||
// API config
|
||||
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}/
|
||||
|
||||
|
||||
|
||||
#endif //CONFIG_H
|
||||
139
src/main.cpp
139
src/main.cpp
@@ -1,97 +1,110 @@
|
||||
#include <Arduino.h>
|
||||
// this is simple RFID card reader code
|
||||
// it will read the card and print the UID of the card
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <MFRC522v2.h>
|
||||
#include <MFRC522DriverSPI.h>
|
||||
#include <MFRC522DriverPinSimple.h>
|
||||
#include <MFRC522Debug.h>
|
||||
#include <WiFi.h>
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include "utils.h"
|
||||
#include "config.h"
|
||||
|
||||
constexpr bool DEBUG = true;
|
||||
|
||||
// Define the pins for the buzzer and the LED
|
||||
constexpr int buzzerPin = 2;
|
||||
constexpr int ledPin = 4;
|
||||
|
||||
// Create an instance of the MFRC522 driver
|
||||
MFRC522DriverPinSimple ss_pin(5);
|
||||
MFRC522DriverSPI driver{ss_pin};
|
||||
MFRC522 mfrc522{driver};
|
||||
MFRC522DriverPinSimple ss_pin(5); // Create digital pin driver
|
||||
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
|
||||
MFRC522 mfrc522{driver}; // Create MFRC522 instance
|
||||
|
||||
// define the functions
|
||||
String getCardUID(MFRC522 &mfrc522);
|
||||
void beepAndBlink(int duration, int count);
|
||||
// NTP client
|
||||
WiFiUDP ntpUDP;
|
||||
// Europe/Prague timezone and update every 60 seconds
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000);
|
||||
|
||||
// last check time
|
||||
unsigned long lastCheck = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
// Initialize the serial port for debugging
|
||||
if (DEBUG) {
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
while (!Serial); // Wait for the serial port to connect
|
||||
}
|
||||
|
||||
// Init the pins
|
||||
// Connect to the Wi-Fi network
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
if (DEBUG)
|
||||
{
|
||||
Serial.println(".");
|
||||
}
|
||||
}
|
||||
if (DEBUG)
|
||||
{
|
||||
Serial.println("WiFi connected");
|
||||
}
|
||||
|
||||
// Initialize NTP client
|
||||
timeClient.begin();
|
||||
timeClient.update();
|
||||
|
||||
// Print the current time
|
||||
if (DEBUG)
|
||||
{
|
||||
Serial.println("Current time: " + timeClient.getFormattedTime());
|
||||
}
|
||||
|
||||
|
||||
// Initialize the pins
|
||||
pinMode(ledPin, OUTPUT);
|
||||
pinMode(buzzerPin, OUTPUT);
|
||||
|
||||
// Init the MFRC522 board
|
||||
// Initialize the MFRC522 board
|
||||
mfrc522.PCD_Init();
|
||||
|
||||
// loop until the API is ready
|
||||
while (!checkAPI()) {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// ready
|
||||
// turn on the LED and beep the buzzer
|
||||
// Indicate that the system is ready by turning on the LED and beeping the buzzer
|
||||
digitalWrite(ledPin, HIGH);
|
||||
beepAndBlink(100, 5);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
||||
// Check the health of the system
|
||||
if (millis() - lastCheck > healthCheckInterval) {
|
||||
healthCheck();
|
||||
lastCheck = millis();
|
||||
}
|
||||
|
||||
|
||||
// Check if a new card is present on the sensor/reader
|
||||
if (!mfrc522.PICC_IsNewCardPresent()) {
|
||||
return;
|
||||
return; // Exit the loop if no new card is present
|
||||
}
|
||||
|
||||
// Select one of the cards.
|
||||
// Select one of the cards
|
||||
if (!mfrc522.PICC_ReadCardSerial()) {
|
||||
return;
|
||||
return; // Exit the loop if no card could be read
|
||||
}
|
||||
|
||||
|
||||
// Read and print the UID
|
||||
Serial.println("Card UID:" + getCardUID(mfrc522));
|
||||
|
||||
// Additional actions when a card is read
|
||||
beepAndBlink(1000, 1);
|
||||
|
||||
// delay for a while to avoid reading the same card multiple times
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
|
||||
// Helper functions
|
||||
|
||||
// return the UID of the card as a string
|
||||
String getCardUID(const MFRC522 &mfrc522) {
|
||||
String uidString = "";
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
uidString += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "";
|
||||
uidString += String(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
return uidString;
|
||||
}
|
||||
|
||||
// beep and blink the LED for a number of times with a duration
|
||||
void beepAndBlink(const int duration=1000, const int count=1) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
digitalWrite(ledPin, LOW); // Turn on the red LED
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
delay(duration); // Wait for a second
|
||||
digitalWrite(ledPin, HIGH); // Turn off the red LED
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
|
||||
// wait for a while before the next beep only if it's not the last beep
|
||||
if (i < count - 1)
|
||||
{
|
||||
delay(duration);
|
||||
// Read and print the UID of the card
|
||||
if (DEBUG) {
|
||||
try {
|
||||
Serial.println(timeClient.getFormattedTime() + " - " + getCardUID(mfrc522));
|
||||
} catch (const std::exception &e) {
|
||||
Serial.println("Error reading the card UID: " + String(e.what()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Perform additional actions when a card is read
|
||||
beepAndBlink(1000, 1);
|
||||
|
||||
// Delay to avoid reading the same card multiple times
|
||||
delay(afterReadDelay);
|
||||
}
|
||||
|
||||
113
src/utils.cpp
Normal file
113
src/utils.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "utils.h"
|
||||
#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;
|
||||
|
||||
|
||||
void ledOn()
|
||||
{
|
||||
if (ledEnabled)
|
||||
{
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
void ledOff()
|
||||
{
|
||||
if (ledEnabled)
|
||||
{
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void buzzerOn()
|
||||
{
|
||||
if (buzzerEnabled)
|
||||
{
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
void buzzerOff()
|
||||
{
|
||||
if (buzzerEnabled)
|
||||
{
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
String getCardUID(const MFRC522& mfrc522)
|
||||
{
|
||||
String uidString = "";
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++)
|
||||
{
|
||||
uidString += mfrc522.uid.uidByte[i] < 0x10 ? "" : "";
|
||||
uidString += String(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
return uidString;
|
||||
}
|
||||
|
||||
void beepAndBlink(const int duration, const int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
ledOff(); // Turn off the LED
|
||||
buzzerOn(); // Turn on the buzzer
|
||||
delay(duration); // Wait for the specified duration
|
||||
ledOn(); // Turn on the LED
|
||||
buzzerOff(); // Turn off the buzzer
|
||||
|
||||
// Wait for a while before the next beep only if it's not the last beep
|
||||
if (i < count - 1)
|
||||
{
|
||||
delay(duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
http.begin(url);
|
||||
int httpResponseCode = http.GET();
|
||||
bool success = (httpResponseCode == 200);
|
||||
if (DEBUG)
|
||||
{
|
||||
Serial.println(success ? "API check: OK" : "API check: Failed");
|
||||
}
|
||||
http.end();
|
||||
return success;
|
||||
}
|
||||
|
||||
void healthCheck()
|
||||
{
|
||||
bool check = false;
|
||||
|
||||
// Check the API
|
||||
check = checkAPI();
|
||||
|
||||
|
||||
if (check)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If the health check failed, beep and blink the LED
|
||||
beepAndBlink(10, 50);
|
||||
ledOff();
|
||||
// reset the board
|
||||
ESP.restart();
|
||||
}
|
||||
16
src/utils.h
Normal file
16
src/utils.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <MFRC522v2.h>
|
||||
|
||||
void ledOn();
|
||||
void ledOff();
|
||||
void buzzerOn();
|
||||
void buzzerOff();
|
||||
String getCardUID(const MFRC522 &mfrc522);
|
||||
void beepAndBlink(int duration = 1000, int count = 1);
|
||||
bool checkAPI();
|
||||
void healthCheck();
|
||||
|
||||
#endif // UTILS_H
|
||||
Reference in New Issue
Block a user