Hi, my project is about the RFID it will detected by the MQTT node named RFID Collection. But My RFID is run with NodeMCU and has been coded as below using Arduino and it works well as displayed in the COM Serial, the RFID is detected but not in the Node Red. I want the RFID to be detected and change the switch shown in the dashboard .
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <MFRC522.h>
#include <SPI.h>
#include "NewPing.h" // include NewPing library
#define SS_PIN 4 //D2
#define RST_PIN 5 //D1
// Lamp - LED - GPIO 0 = D4 on ESP-12E NodeMCU board
const int ledPin = 0;
const char* ssid = "AndroidAP";
const char* password = "lalalo1234";
const char* mqtt_server = "192.168.43.114";// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
// Create MFRC522 instance.
MFRC522 mfrc522(SS_PIN, RST_PIN);
int statuss = 0;
int out = 0;
// setup mqtt client
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// If a message is received on the topic room/lamp, you check if the message is either on or off. Turns the lamp GPIO according to the message
if(topic=="home/levelsensor"){
Serial.print("Measuring level ");
if (messageTemp == "on"){
digitalWrite(ledPin, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(ledPin, LOW);
Serial.print("Off");
}
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("room/lamp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
// pinMode(ledPin, OUTPUT);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
// dht.begin();
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.println();
Serial.print(" UID tag :");
String content= "";
byte letter;
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));
}
content.toUpperCase();
Serial.println();
if (content.substring(1) == "2A 44 4F B3") //change UID of the card that you want to give access
{
client.publish("rfid/collection","true");
Serial.println(" Access Granted ");
Serial.println(" Welcome Mr.Circuit ");
delay(1000);
Serial.println(" Have FUN ");
Serial.println();
statuss = 1;
}
else {
Serial.println(" Access Denied ");
delay(3000);
}
}
Maybe something is wrong with the my Client Publish . Any suggestion?
Below is my node red code :
[{"id":"86cdf9d1.c16778","type":"mqtt in","z":"71d2c1a.f8a4f4","name":"RFID Collection","topic":"rfid/collection","qos":"0","datatype":"auto","broker":"4c17587e.e718a8","nl":false,"rap":true,"rh":0,"x":240,"y":180,"wires":[["3cfc3d34.110cc2"]]},{"id":"4c17587e.e718a8","type":"mqtt-broker","name":"MQTT Connected","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]