feat: implemented simple logic to detect cards and send information to master via I2C

This commit is contained in:
SWETRAK
2026-07-03 23:50:58 +02:00
parent 3eb119f6a2
commit a3cfa44d82
8 changed files with 138 additions and 9 deletions
+8 -1
View File
@@ -10,4 +10,11 @@ Addresses:
- Slave 2 -> `0x09` - Slave 2 -> `0x09`
- Slave 3 -> `0x0A` - Slave 3 -> `0x0A`
- Slave 4 -> `0x0B` - Slave 4 -> `0x0B`
- Slave 5 -> `0x0C` - Slave 5 -> `0x0C`
# LED config
Led Type: WS2812B
Pin: 5
Count: 29
Color Order: GRB
+3
View File
@@ -38,6 +38,9 @@ void setup()
void loop() void loop()
{ {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
// TODO: Implement the main loop logic for I2C communication with slave devices
// TODO: Implement the logic to control the LED strip based on the received data from slave devices
// TODO: Send informations to PC via Serial communication, needs to be implemented based on the previous code snippets.
} }
void testLedStrip() void testLedStrip()
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+112 -3
View File
@@ -1,13 +1,64 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <MFRC522.h>
#define SDA_PIN 21 #define SDA_PIN 21
#define SCL_PIN 22 #define SCL_PIN 22
#define SLAVE_ADDRESS 0x08 #define SLAVE_ADDRESS 0x08
// Card reader pins
#define READER_COUNT 6
#define RST_PIN 9
#define SS_ONE_PIN 10
#define SS_TWO_PIN 5
#define SS_THREE_PIN 4
#define SS_FOUR_PIN 2
#define SS_FIVE_PIN 15
#define SS_SIX_PIN 13
// Card status definitions
#define CARD_NOT_DETECTED 0
#define CARD_OK 1
#define CARD_WRONG 2
// Slave select pins for each reader
const byte SS_PINS[READER_COUNT] = {SS_ONE_PIN, SS_TWO_PIN, SS_THREE_PIN, SS_FOUR_PIN, SS_FIVE_PIN, SS_SIX_PIN};
// I2C addresses for each reader
// TODO: Update the addresses to specific setup
const byte READER_ADDRESSES[READER_COUNT] = {
0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
// List of proper cards uuid's
const byte CORRECT_UIDS[READER_COUNT][4] = {
{0xDE, 0xAD, 0xBE, 0xEF}, // Reader 1
{0xCA, 0xFE, 0xBA, 0xBE}, // Reader 2
{0xFA, 0xCE, 0xB0, 0x0C}, // Reader 3
{0xBA, 0xAD, 0xF0, 0x0D}, // Reader 4
{0xFE, 0xED, 0xFA, 0xCE}, // Reader 5
{0xC0, 0xFF, 0xEE, 0x00} // Reader 6
};
// Reader statuses: 0 - not detected, 1 - okey, 2 - wrong card
volatile byte readerStatus[READER_COUNT] = {
CARD_NOT_DETECTED,
CARD_NOT_DETECTED,
CARD_NOT_DETECTED,
CARD_NOT_DETECTED,
CARD_NOT_DETECTED,
CARD_NOT_DETECTED};
MFRC522 mfrc522[READER_COUNT]; // Create MFRC522 instances for each reader
void requestEvent(); void requestEvent();
void initializeReaders();
bool compareUID(byte *readUID, const byte *correctUID, byte size);
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -15,15 +66,73 @@ void setup()
Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins Wire.begin(SDA_PIN, SCL_PIN, SLAVE_ADDRESS); // Initialize I2C with specified SDA and SCL pins
Wire.onRequest(requestEvent); Wire.onRequest(requestEvent);
initializeReaders();
} }
void loop() void loop()
{ {
for (byte i = 0; i < READER_COUNT; i++)
{
if (!mfrc522[i].PICC_IsNewCardPresent() || !mfrc522[i].PICC_ReadCardSerial())
{
MFRC522::StatusCode buffer_status;
byte buffer_atqa[2];
byte buffer_size = sizeof(buffer_atqa);
buffer_status = mfrc522[i].PICC_WakeupA(buffer_atqa, &buffer_size);
if (buffer_status != MFRC522::STATUS_OK)
{
readerStatus[i] = CARD_NOT_DETECTED;
}
continue;
}
if (compareUID(mfrc522[i].uid.uidByte, CORRECT_UIDS[i], mfrc522[i].uid.size))
{
readerStatus[i] = CARD_OK;
}
else
{
readerStatus[i] = CARD_WRONG;
}
mfrc522[i].PICC_HaltA();
mfrc522[i].PCD_StopCrypto1();
}
delay(50);
}
void initializeReaders()
{
for (byte i = 0; i < READER_COUNT; i++)
{
mfrc522[i].PCD_Init(SS_PINS[i], RST_PIN);
}
}
bool compareUID(byte *readUID, const byte *correctUID, byte size)
{
for (byte i = 0; i < size; i++)
{
if (readUID[i] != correctUID[i])
{
return false;
}
}
return true;
} }
void requestEvent() void requestEvent()
{ {
// TODO: Send data to the master when requested byte outputData[READER_COUNT];
Serial.println("Master requested data from Slave.");
Wire.write("Hello from Slave!"); for (byte i = 0; i < READER_COUNT; i++)
{
outputData[i] = READER_ADDRESSES[i] + readerStatus[i];
}
Wire.write(outputData, READER_COUNT);
} }
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12
+3 -1
View File
@@ -13,4 +13,6 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522@^1.4.12