#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions for the RC522
#define SS_PIN 5
#define RST_PIN 2
MFRC522 rfid(SS_PIN, RST_PIN);
// Create an LCD object with I2C address 0x27 (adjust if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Wi-Fi credentials for Wokwi
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// MQTT broker details
const char* MQTT_SERVER = "test.mosquitto.org";
const int MQTT_PORT = 1883;
// Topic to which we will publish the RFID data
const char* MQTT_TOPIC = "rfid/data";
WiFiClient espClient;
PubSubClient client(espClient);
// -----------------------------------------------------------------------------
// Helper Functions for LCD
// -----------------------------------------------------------------------------
/**
* Display a message on the LCD (line 0 and line 1).
*/
void displayOnLCD(const char* line1, const char* line2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
// -----------------------------------------------------------------------------
// Reconnect to MQTT if the client is not connected
// -----------------------------------------------------------------------------
void reconnectMQTT() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection... ");
// Use a unique client ID to avoid conflicts
if (client.connect("ESP32Client-1234")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" (retrying in 3 seconds)");
delay(3000); // Wait before retrying
}
}
}
// -----------------------------------------------------------------------------
// SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(1000);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
displayOnLCD("Starting...", "Please wait");
// Initialize SPI and RFID
SPI.begin();
rfid.PCD_Init();
// Connect to Wi-Fi
Serial.print("Connecting to WiFi: ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Configure MQTT server
client.setServer(MQTT_SERVER, MQTT_PORT);
// Display status on LCD
displayOnLCD("WiFi Ready", "MQTT Setup...");
// Attempt an initial connection to MQTT
reconnectMQTT();
Serial.print("\n");
Serial.print("\n");
Serial.print("10/1/2025, 11:03:20 AM node: debug 2\n");
Serial.print("rfid/data : msg.payload : string[13]\n");
Serial.println("Count :7\n");
Serial.print("\n");
Serial.print("\n");
Serial.print("\n");
Serial.print("\n");
Serial.print("10/1/2025, 10:02:12 AM node: debug 2\n");
Serial.print("rfid/data : msg.payload : string[13]\n");
Serial.println("Count :6\n");
Serial.print("\n");
Serial.print("\n");
displayOnLCD("System Ready", "Scan RFID");
Serial.println("Setup complete. Ready to read RFID tags.");
}
// -----------------------------------------------------------------------------
// LOOP
// -----------------------------------------------------------------------------
void loop() {
// If the client is not connected, reconnect
if (!client.connected()) {
reconnectMQTT();
}
// This handles keeping the MQTT connection alive
client.loop();
// Check for new RFID cards
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
// Build a string from the RFID tag's UID
String tagID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
if (rfid.uid.uidByte[i] < 0x10) {
tagID += "0";
}
tagID += String(rfid.uid.uidByte[i], HEX);
if (i < rfid.uid.size - 1) {
tagID += ":";
}
}
tagID.toUpperCase();
// Print to the Serial Monitor
Serial.print("RFID Detected: ");
Serial.println(tagID);
// Publish the RFID tag ID to the MQTT topic
client.publish(MQTT_TOPIC, tagID.c_str());
// Display the RFID tag value directly on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RFID Tag:");
lcd.setCursor(0, 1);
lcd.print(tagID);
// Halt reading this card so we do not loop rapidly on the same card
rfid.PICC_HaltA();
delay(2000);
// Display ready message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready for next");
}
}