clear start app
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 |
|
| MFRC522 RFID Reader | ESP32 | Description | Color |
|
||||||
|---------------------|-------|-------------|
|
|---------------------|---------------|-------------|--------|
|
||||||
| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input |
|
| SDA | GPIO 5 | SPI signal input, I2C data line, or UART data input | blue |
|
||||||
| SCK | GPIO 18 | SPI clock |
|
| SCK | GPIO 18 | SPI clock | brown |
|
||||||
| MOSI | GPIO 23 | SPI data input |
|
| MOSI | GPIO 23 | SPI data input | grey |
|
||||||
| MISO | GPIO 19 | SPI master-in-slave-out, I2C serial clock, or UART serial output |
|
| 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 |
|
| IRQ | Don't connect | Interrupt pin; signals the microcontroller when an RFID tag is nearby | - |
|
||||||
| GND | GND | Ground connection |
|
| 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 |
|
| 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) |
|
| 3.3V | 3.3V | Power supply (2.5-3.3V) | orange |
|
||||||
@@ -14,6 +14,3 @@ board = esp32dev
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
lib_deps =
|
|
||||||
arduino-libraries/NTPClient
|
|
||||||
miguelbalboa/MFRC522
|
|
||||||
147
src/backup.txt
Normal file
147
src/backup.txt
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
162
src/main.cpp
162
src/main.cpp
@@ -1,92 +1,49 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Wire.h>
|
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <MFRC522v2.h>
|
#include <MFRC522v2.h>
|
||||||
#include <MFRC522DriverSPI.h>
|
#include <MFRC522DriverSPI.h>
|
||||||
#include <MFRC522DriverPinSimple.h>
|
#include <MFRC522DriverPinSimple.h>
|
||||||
#include <MFRC522Debug.h>
|
#include <MFRC522Debug.h>
|
||||||
#include <WiFi.h>
|
|
||||||
#include <NTPClient.h>
|
|
||||||
#include <WiFiUdp.h>
|
|
||||||
|
|
||||||
constexpr bool DEBUG = true;
|
constexpr bool DEBUG = true;
|
||||||
|
|
||||||
|
// Define the pins for the buzzer and the LED
|
||||||
|
constexpr int buzzerPin = 2;
|
||||||
|
constexpr int ledPin = 4;
|
||||||
|
|
||||||
// pins
|
// Create an instance of the MFRC522 driver
|
||||||
constexpr int redLedPin = 2;
|
MFRC522DriverPinSimple ss_pin(5);
|
||||||
constexpr int greenLedPin = 4;
|
MFRC522DriverSPI driver{ss_pin};
|
||||||
constexpr int cardLedPin = 12;
|
MFRC522 mfrc522{driver};
|
||||||
constexpr int buzzerPin = 13;
|
|
||||||
|
|
||||||
// wifi
|
// define the functions
|
||||||
auto ssid = "ElaZereVsetko2g";
|
String getCardUID(MFRC522 &mfrc522);
|
||||||
auto password = "CoJeElaToJeEla";
|
void beepAndBlink(int duration, int count);
|
||||||
|
|
||||||
// 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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
|
||||||
// cakaj na serial port
|
if (DEBUG) {
|
||||||
while (!Serial) {
|
Serial.begin(115200);
|
||||||
delay(10);
|
while (!Serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
// init pins
|
// Init the pins
|
||||||
pinMode(redLedPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
pinMode(greenLedPin, OUTPUT);
|
|
||||||
pinMode(cardLedPin, OUTPUT);
|
|
||||||
pinMode(buzzerPin, OUTPUT);
|
pinMode(buzzerPin, OUTPUT);
|
||||||
|
|
||||||
// pripoj sa na wifi
|
// Init the MFRC522 board
|
||||||
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();
|
mfrc522.PCD_Init();
|
||||||
|
|
||||||
debugPrint("Cakam na kartu...");
|
|
||||||
|
// ready
|
||||||
|
// turn on the LED and beep the buzzer
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
beepAndBlink(100, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
||||||
if (!mfrc522.PICC_IsNewCardPresent()) {
|
if (!mfrc522.PICC_IsNewCardPresent()) {
|
||||||
@@ -98,50 +55,43 @@ void loop() {
|
|||||||
return;
|
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
|
// Read and print the UID
|
||||||
char timeWithMicroseconds[30];
|
Serial.println("Card UID:" + getCardUID(mfrc522));
|
||||||
snprintf(timeWithMicroseconds, sizeof(timeWithMicroseconds), "%lu.%06lu", currentTime, microseconds);
|
|
||||||
|
|
||||||
// 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 = "";
|
String uidString = "";
|
||||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||||
|
uidString += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "";
|
||||||
uidString += String(mfrc522.uid.uidByte[i], HEX);
|
uidString += String(mfrc522.uid.uidByte[i], HEX);
|
||||||
}
|
}
|
||||||
|
return uidString;
|
||||||
|
}
|
||||||
|
|
||||||
debugPrint("send: " + uidString);
|
// beep and blink the LED for a number of times with a duration
|
||||||
// Create the URL
|
void beepAndBlink(const int duration=1000, const int count=1) {
|
||||||
char url[256]; // Make sure this is large enough for your URL
|
for (int i = 0; i < count; i++) {
|
||||||
snprintf(url, sizeof(url), "%s/%s/%s/%s/",
|
digitalWrite(ledPin, LOW); // Turn on the red LED
|
||||||
apiUrl.c_str(),
|
digitalWrite(buzzerPin, HIGH);
|
||||||
timerId.c_str(),
|
delay(duration); // Wait for a second
|
||||||
uidString.c_str(),
|
digitalWrite(ledPin, HIGH); // Turn off the red LED
|
||||||
timeWithMicroseconds);
|
digitalWrite(buzzerPin, LOW);
|
||||||
|
|
||||||
debugPrint(url);
|
// wait for a while before the next beep only if it's not the last beep
|
||||||
// Send the HTTP GET request
|
if (i < count - 1)
|
||||||
HTTPClient http;
|
{
|
||||||
http.begin(url);
|
delay(duration);
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user