Last active
March 18, 2023 02:19
-
-
Save wutu/387bfeb40890d33659044629ffb216dd to your computer and use it in GitHub Desktop.
Read RFID tags and publish just the UID to an MQTT server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RC522 - read RFID tags and publish just the UID to an MQTT server | |
#include <SPI.h> | |
#include <MFRC522.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
#include <Wire.h> | |
#define LED 7 | |
#define BUTTON 2 | |
#define CLIENTID "ArduinoMEGA" | |
#define SS_PIN 53//Arduino Mega, set 10 for Arduino Uno/Nano | |
#define RST_PIN 5 | |
MFRC522 mfrc522(SS_PIN, RST_PIN);// Create MFRC522 instance. | |
char message_buff[100]; | |
// Update these with values suitable for your network. | |
byte mac[] = { 0xDE, 0xED, 0xAA, 0xFE, 0xFE, 0xED }; | |
//byte ip[] = { 10, 0, 0, 177 }; //Static IP | |
byte server[] = { 10, 0, 0, 10 }; // MQTT Broker | |
const int buttonPinOn = 2; // the number of the pushbutton pin | |
// variables will change: | |
int buttonOnState = 0; // variable for reading the pushbutton status | |
// Callback function header | |
void callback(char* topic, byte* payload, unsigned int length); | |
EthernetClient ethClient; | |
PubSubClient client(server, 1883, callback, ethClient); | |
// Callback function | |
void callback(char* topic, byte* payload, unsigned int length) { | |
int i = 0; | |
Serial.println("Message arrived:topic: " + String(topic)); | |
Serial.println("Length: " + String(length, DEC)); | |
// create character buffer with ending null terminator (string) | |
for (i = 0; i < length; i++) { | |
message_buff[i] = payload[i]; | |
} | |
message_buff[i] = '\0'; | |
String msgString = String(message_buff); | |
Serial.println("Payload: " + msgString); | |
if (msgString.equals("LED on")) { | |
digitalWrite(LED, HIGH); | |
} | |
else if (msgString.equals("LED off")) { | |
digitalWrite(LED, LOW); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(9600);// Initialize serial communications with the PC | |
SPI.begin();// Init SPI bus | |
mfrc522.PCD_Init();// Init MFRC522 card | |
Serial.println("Initialiased RFID reader"); | |
Wire.begin(); | |
Ethernet.begin(mac); //for dynamic IP | |
// Ethernet.begin(mac, ip); //for static IP | |
Serial.print("Ethernet is at "); | |
Serial.println(Ethernet.localIP()); | |
//Serial.println(tmpBuf); | |
pinMode(LED, OUTPUT); | |
if (client.connect(CLIENTID)) { | |
client.publish("mh/gf/tech/lab/plc/status", "Ready"); | |
client.subscribe("inTopic"); | |
} | |
} | |
void loop() | |
{ | |
client.loop(); | |
buttonOnState = digitalRead(buttonPinOn); | |
if (buttonOnState == HIGH) { | |
// turn LED on: | |
digitalWrite(LED, HIGH); | |
Serial.println("Turn LED on and send MQTT message."); | |
client.publish("mh/gf/tech/lab/plc/button/state", "1"); | |
delay(500); | |
digitalWrite(LED, LOW); | |
} | |
// Look for new cards | |
if ( ! mfrc522.PICC_IsNewCardPresent()) { | |
return; | |
} | |
// Select one of the cards | |
if ( ! mfrc522.PICC_ReadCardSerial())return; | |
Serial.print("Card UID:");//Dump UID | |
String rfidUid = ""; | |
for (byte i = 0; i < mfrc522.uid.size; i++) { | |
rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); | |
rfidUid += String(mfrc522.uid.uidByte[i], HEX); | |
} | |
Serial.println(rfidUid); | |
Serial.println(""); | |
rfidUid.toCharArray(message_buff, rfidUid.length() + 1); | |
client.publish("mh/gf/tech/lab/plc/rfid/uid", message_buff); | |
mfrc522.PICC_HaltA(); // Halt PICC | |
mfrc522.PCD_StopCrypto1();// Stop encryption on PCD | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pubsub version? u have used