#include <WiFi.h> // For WiFi connectivity (ESP32 / Arduino WiFi library)
#include <PubSubClient.h> // For MQTT functions
#include <SPI.h> // For RFID (MFRC522) and possibly LCD
#include <MFRC522.h> // For RFID
#include <Wire.h> // For I2C LCD
#include <LiquidCrystal_I2C.h> // For the 20x4 I2C LCD
// ---------------------
// RFID and LCD Definitions
// ---------------------
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
LiquidCrystal_I2C MyLCD(0x27, 20, 4); // I2C LCD at address 0x27, 20 columns, 4 rows
// ---------------------
// WiFi Settings
// ---------------------
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi Password
// ---------------------
// MQTT Settings
// ---------------------
const char* mqttServer = "192.168.1.100"; // MQTT broker IP or hostname
const int mqttPort = 1883; // MQTT broker port (default: 1883)
WiFiClient wifiClient; // WiFi client for PubSubClient
PubSubClient client(wifiClient); // Wrap the WiFi client in PubSubClient
// ---------------------
// Function Prototypes
// ---------------------
void setupWiFi();
void reconnectMQTT();
// ---------------------
// Arduino Setup
// ---------------------
void setup() {
// Initialize Serial
Serial.begin(9600);
// Initialize LCD
MyLCD.init();
MyLCD.backlight();
// Initialize SPI and RFID
SPI.begin();
mfrc522.PCD_Init();
// Print startup messages to Serial and LCD
Serial.println("RFID Based Passport - WiFi & MQTT");
Serial.println("Scan your card to the RFID reader...");
Serial.println();
MyLCD.setCursor(0, 0);
MyLCD.print("RFID Passport via WiFi");
MyLCD.setCursor(0, 1);
MyLCD.print(" Scan Your Card ");
MyLCD.setCursor(0, 2);
MyLCD.print(" To The RFID Reader");
// Connect to WiFi
setupWiFi();
// Initialize MQTT
client.setServer(mqttServer, mqttPort);
}
// ---------------------
// Arduino Main Loop
// ---------------------
void loop() {
// Ensure we stay connected to MQTT
if (!client.connected()) {
reconnectMQTT();
}
client.loop(); // Process incoming MQTT messages (if subscribed) and keep the connection alive
// Check for a new RFID card
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Read the UID and convert it to a String
Serial.print("UID tag: ");
String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
// Convert the UID string to uppercase
content.toUpperCase();
Serial.print("Message: ");
Serial.println(content);
// Publish the UID to MQTT
client.publish("rfid/uid", content.c_str());
// Check if the UID is the authorized one
if (content.substring(1) == "BD 31 15 2B") {
Serial.println("Authorized access");
client.publish("rfid/access", "Authorized");
} else {
Serial.println("Access denied");
client.publish("rfid/access", "Denied");
}
Serial.println();
// Delay to let the user see the result
delay(3000);
}
// ---------------------
// WiFi Setup
// ---------------------
void setupWiFi() {
// Begin WiFi connection
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.print(WiFi.status());
// Wait until connected
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP
Serial.println();
Serial.println("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
// ---------------------
// MQTT Reconnection
// ---------------------
void reconnectMQTT() {
// Keep trying to connect until successful
while (!client.connected()) {
Serial.print("Attempting MQTT connection... ");
// Attempt to connect (clientID must be unique on the broker)
if (client.connect("ArduinoRFIDClient")) {
Serial.println("connected to MQTT!");
// Optionally, subscribe to topics if needed:
// client.subscribe("some/topic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" -> trying again in 5 seconds");
delay(5000); // Retry after 5 seconds
}
}
}