WOKWI ESP32 模擬 READ RFID UID (AsyncMQTT_ESP32)
https://wokwi.com/projects/420235174679393281
WOKWI程式
rfid-rc522.chip.json程式
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMQTT_ESP32.h>
//#include <MFRC522.h>
#include <Arduino.h>
//=========================
// RFID-RC522 wire pin
// SDA = 5 SCK =18
// MOSI=23 MISO=19
// RST = 4
// GND , VCC
//=========================
#define LEDPIN 27
#define BuzzerPIN 14
//#define MQTT_HOST "broker.mqtt-dashboard.com"
//#define MQTT_HOST "broker.hivemq.com"
#define MQTT_HOST "test.mosquitto.org"
//#define MQTT_HOST "broker.mqttgo.io"
#define MQTT_PORT 1883
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
const char *SubTopic1 = "alex9ufo/esp32/led";
const char *PubTopic2 = "alex9ufo/esp32/uid";
//const char *PubTopic3 = "alex9ufo/esp32/buzzer";
//================================================================
//================================================================
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
//================================================================
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
//================================================================
void WiFiEvent(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi ready");
break;
case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("WiFi STA starting");
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("WiFi STA connected");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
Serial.println("WiFi lost IP");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#endif
default:
break;
}
}
//================================================================
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub1 = mqttClient.subscribe(SubTopic1 , 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub1);
//uint16_t packetIdSub2 = mqttClient.subscribe(SubTopic2, 2);
//Serial.print("Subscribing at QoS 2, packetId: ");
//Serial.println(packetIdSub2);
}
//================================================================
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
//================================================================
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
//================================================================
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
//================================================================
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
//================================================================
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
String messageTemp;
for (int i = 0; i < len; i++) {
Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}
if (strcmp(topic, SubTopic1) == 0) {
// If the relay is on turn it off (and vice-versa)
Serial.println();
Serial.println(messageTemp);
if (messageTemp == "on") {
digitalWrite(LEDPIN,HIGH);
Serial.println("LED ON");
}
if (messageTemp == "off") {
digitalWrite(LEDPIN,LOW);
Serial.println("LED OFF");
}
}
}
//================================================================
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Hello, ESP32!");
pinMode(LEDPIN, OUTPUT);
pinMode(BuzzerPIN, OUTPUT);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage); //callback
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
connectToWifi();
}
//================================================================
void loop() {
if (Serial.available()) {
Serial.println("Enter MFRC522 UID (format: XX XX XX XX):");
String input = Serial.readStringUntil('\n');
input.trim(); // Remove any leading or trailing whitespace
if (isValidFormat(input)) {
Serial.println("Valid format.");
String temp1=input;
uint16_t packetIdPub1 = mqttClient.publish(PubTopic2, 1, true, temp1.c_str());
Serial.printf("RFID UID Number Published", PubTopic2, packetIdPub1);
digitalWrite(BuzzerPIN, HIGH);
tone(BuzzerPIN, 750);
delay(1000);
digitalWrite(BuzzerPIN, LOW);
noTone(BuzzerPIN);
} else {
Serial.println("Invalid format. Enter Enter MFRC522 UID (format: XX XX XX XX):");
}
}
}
//================================================================
bool isValidFormat(String input) {
// Check if the input matches the format "XX XX XX XX"
if (input.length() == 11 && input.charAt(2) == ' ' && input.charAt(5) == ' ' && input.charAt(8) == ' ') {
for (int i = 0; i < input.length(); i++) {
if (i != 2 && i != 5 && i != 8) {
if (!isDigit(input.charAt(i))) {
return false;
}
}
}
return true;
}
return false;
}
//================================================================
rfid-rc522.chip.c程式
#include "wokwi-api.h"
#include <stdint.h>
#include <stdio.h>
#include <unistd.h> // For sleep function
// Function to simulate SPI communication
void spi_transfer(uint8_t *data_out, uint8_t *data_in, uint8_t len) {
// Simulate SPI transfer
printf("MFRC522: Sending data: ");
for (int i = 0; i < len; i++) {
printf("0x%02X ", data_out[i]);
}
printf("\n");
// Simulate receiving data
for (int i = 0; i < len; i++) {
data_in[i] = 0xAB; // Dummy data
}
printf("MFRC522: Received data: ");
for (int i = 0; i < len; i++) {
printf("0x%02X ", data_in[i]);
}
printf("\n");
}
// Function to send UID data to Arduino
void send_uid_to_arduino() {
// Loop for sending 10 sets of UID data
for (int i = 0; i < 10; i++) {
// Simulate UID data
uint8_t uid_data[4] = {0x12, 0x34, 0x56, 0x78};
// Print UID data
printf("UID %d: ", i + 1);
for (int j = 0; j < 4; j++) {
printf("%02X ", uid_data[j]);
}
printf("\n");
// Send UID data to Arduino via SPI
spi_transfer(uid_data, NULL, 4);
// Wait for 1 second (for demonstration, reduce to 1 second)
sleep(1);
}
}
// Function to initialize the chip and SPI pins
void chip_init() {
// Initialize MFRC522
// Reset MFRC522
// Additional initialization steps can be added here
//mfrc522_write(0x0A, 0x0F); // Command register address and reset command
}
int main() {
// Initialize chip
chip_init();
// Send UID data to Arduino
// Send UID data to Arduino every 10 seconds
while (1) {
send_uid_to_arduino();
// Wait for 10 seconds
sleep(10);
}
return 0;
}
{
"name": "RFID-RC522",
"author": "arducoding",
"pins": [
"SDA",
"SCK",
"MOSI",
"MISO",
"RST",
"GND",
"VCC",
"",
"",
"",
"",
"",
""
],
"controls": []
}