clear start app

This commit is contained in:
2025-03-12 10:49:52 +01:00
parent db4b933981
commit d0bfca560a
4 changed files with 213 additions and 119 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 |
|---------------------|-------|-------------|
| 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) |
| 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 |

View File

@@ -14,6 +14,3 @@ board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
arduino-libraries/NTPClient
miguelbalboa/MFRC522

147
src/backup.txt Normal file
View File

@@ -0,0 +1,147 @@
#include <Arduino.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
constexpr bool DEBUG = true;
// pins
constexpr int redLedPin = 2;
constexpr int greenLedPin = 4;
constexpr int cardLedPin = 12;
constexpr int buzzerPin = 13;
// wifi
auto ssid = "ElaZereVsetko2g";
auto password = "CoJeElaToJeEla";
// API
const String apiUrl = "http://192.168.50.87:8111/api/timer/write";
const String timerId = "branka1";
// NTP client
WiFiUDP ntpUDP;
// Europe/Prague timezone and update every 60 seconds
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000);
// vykradnuty kod netusim co to robi ale robi to
MFRC522DriverPinSimple ss_pin(5); // Create digital pin driver
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
MFRC522 mfrc522{driver}; // Create MFRC522 instance
void debugPrint(String const& msg, const bool newLine = true) {
if (DEBUG) {
if (newLine) {
Serial.println(msg);
} else {
Serial.print(msg);
}
}
}
void setup() {
Serial.begin(115200);
// cakaj na serial port
while (!Serial) {
delay(10);
}
// init pins
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(cardLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// pripoj sa na wifi
debugPrint("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
debugPrint(".", false);
}
if (DEBUG) {
debugPrint(" connected!");
}
// Turn off the red LED and turn on the green LED
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
// Initialize NTP client
timeClient.begin();
timeClient.update();
// Print the current time
debugPrint("Current time: " + timeClient.getFormattedTime());
mfrc522.PCD_Init();
debugPrint("Cakam na kartu...");
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards.
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Get the current time in Unix time with microseconds
timeClient.update();
const unsigned long currentTime = timeClient.getEpochTime();
const unsigned long microseconds = micros() % 1000000;
// Format the time with microseconds
char timeWithMicroseconds[30];
snprintf(timeWithMicroseconds, sizeof(timeWithMicroseconds), "%lu.%06lu", currentTime, microseconds);
// Format the UID
String uidString = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
uidString += String(mfrc522.uid.uidByte[i], HEX);
}
debugPrint("send: " + uidString);
// Create the URL
char url[256]; // Make sure this is large enough for your URL
snprintf(url, sizeof(url), "%s/%s/%s/%s/",
apiUrl.c_str(),
timerId.c_str(),
uidString.c_str(),
timeWithMicroseconds);
debugPrint(url);
// Send the HTTP GET request
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
debugPrint("HTTP Response code: " + String(httpResponseCode));
} else {
debugPrint("Error on HTTP request");
}
http.end();
// Turn on the card LED and buzzer
digitalWrite(cardLedPin, HIGH);
tone(buzzerPin, 2000); // Generate 2000Hz tone
delay(500); // Beep and light for 500 milliseconds
digitalWrite(cardLedPin, LOW);
noTone(buzzerPin); // Stop the tone
delay(2000);
}

View File

@@ -1,92 +1,49 @@
#include <Arduino.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
constexpr bool DEBUG = true;
// Define the pins for the buzzer and the LED
constexpr int buzzerPin = 2;
constexpr int ledPin = 4;
// pins
constexpr int redLedPin = 2;
constexpr int greenLedPin = 4;
constexpr int cardLedPin = 12;
constexpr int buzzerPin = 13;
// Create an instance of the MFRC522 driver
MFRC522DriverPinSimple ss_pin(5);
MFRC522DriverSPI driver{ss_pin};
MFRC522 mfrc522{driver};
// wifi
auto ssid = "ElaZereVsetko2g";
auto password = "CoJeElaToJeEla";
// API
const String apiUrl = "http://192.168.50.87:8111/api/timer/write";
const String timerId = "branka1";
// NTP client
WiFiUDP ntpUDP;
// Europe/Prague timezone and update every 60 seconds
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000);
// vykradnuty kod netusim co to robi ale robi to
MFRC522DriverPinSimple ss_pin(5); // Create digital pin driver
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
MFRC522 mfrc522{driver}; // Create MFRC522 instance
void debugPrint(String const& msg, const bool newLine = true) {
if (DEBUG) {
if (newLine) {
Serial.println(msg);
} else {
Serial.print(msg);
}
}
}
// define the functions
String getCardUID(MFRC522 &mfrc522);
void beepAndBlink(int duration, int count);
void setup() {
Serial.begin(115200);
// cakaj na serial port
while (!Serial) {
delay(10);
if (DEBUG) {
Serial.begin(115200);
while (!Serial);
}
// init pins
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(cardLedPin, OUTPUT);
// Init the pins
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// pripoj sa na wifi
debugPrint("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
debugPrint(".", false);
}
if (DEBUG) {
debugPrint(" connected!");
}
// Turn off the red LED and turn on the green LED
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
// Initialize NTP client
timeClient.begin();
timeClient.update();
// Print the current time
debugPrint("Current time: " + timeClient.getFormattedTime());
// Init the MFRC522 board
mfrc522.PCD_Init();
debugPrint("Cakam na kartu...");
// ready
// turn on the LED and beep 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.
if (!mfrc522.PICC_IsNewCardPresent()) {
@@ -98,50 +55,43 @@ void loop() {
return;
}
// Get the current time in Unix time with microseconds
timeClient.update();
const unsigned long currentTime = timeClient.getEpochTime();
const unsigned long microseconds = micros() % 1000000;
// Format the time with microseconds
char timeWithMicroseconds[30];
snprintf(timeWithMicroseconds, sizeof(timeWithMicroseconds), "%lu.%06lu", currentTime, microseconds);
// Read and print the UID
Serial.println("Card UID:" + getCardUID(mfrc522));
// Format the UID
// 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;
}
debugPrint("send: " + uidString);
// Create the URL
char url[256]; // Make sure this is large enough for your URL
snprintf(url, sizeof(url), "%s/%s/%s/%s/",
apiUrl.c_str(),
timerId.c_str(),
uidString.c_str(),
timeWithMicroseconds);
// 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);
debugPrint(url);
// Send the HTTP GET request
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
debugPrint("HTTP Response code: " + String(httpResponseCode));
} else {
debugPrint("Error on HTTP request");
// wait for a while before the next beep only if it's not the last beep
if (i < count - 1)
{
delay(duration);
}
}
http.end();
// Turn on the card LED and buzzer
digitalWrite(cardLedPin, HIGH);
tone(buzzerPin, 2000); // Generate 2000Hz tone
delay(500); // Beep and light for 500 milliseconds
digitalWrite(cardLedPin, LOW);
noTone(buzzerPin); // Stop the tone
delay(2000);
}