RFID is not detected in Node Red

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":""}]

Have you tested the broker on your raspberry pi can be connected to from a networked computer?

What type and version broker are you running on the raspberry?

its mosquitto version 1.5.7

1624659078: mosquitto version 1.5.7 starting
1624659078: Using default config.
1624659078: Opening ipv4 listen socket on port 1883.
1624659078: Error: Address already in use

This could be problematic.

The port is already in use. Do you have another broker running already?
What does the command
sudo netstat -ltnp | grep -w ':1883'
show? If it says the command is not found then run
sudo apt install net-tools
to install it.

tcp6 0 0 :::1883 :::* LISTEN 502/mosquitto

$ sudo apt install net-tools
E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.

sudo dpkg --configure -a
Setting up apache2 (2.4.38-3+deb10u4) ...

Configuration file '/etc/apache2/apache2.conf'
==> File on system created by you or by a script.
==> File also in package provided by package maintainer.
What would you like to do about it ? Your options are:
Y or I : install the package maintainer's version
N or O : keep your currently-installed version
D : show the differences between the versions
Z : start a shell to examine the situation
The default action is to keep your current version.
*** apache2.conf (Y/I/N/O/D/Z) [default=N] ?

what should I proceed ?

its mosquitto version 1.5.7

ok, so can you access the mosquito broker from an external computer using an MQTT client (e.g. MQTT Explorer). Do you see a topic home/levelsensor ?

Are you publishing a value to home/levelsensor? from somewhere?


Is the IP/username/password set correctly in your c code?

const char* ssid = "AndroidAP";
const char* password = "lalalo1234";
const char* mqtt_server = "192.168.43.114"

Can you look at your DHCP server / router to see if the ESP is connecting to the network?

Do you see "WiFi connected - ESP IP address: xxxxxxxxxx" in the serial console?

Can you ping that IP address?

yes , there is home/levelsensor somewhere else in another node red flow.

All is correct, my serial shows that it connected and my MQTT in node red shows green light to say it is connected. My circuit is functioning but seems not reaching my node-red.

Q1: When you present an RFID card do you see "Access Granted" or "Access Denied" in the serial console?

Q2: Can you see (using something like MQTT explorer) values in the broker for topics...

  • rfid/collection ?
  • home/levelsensor ?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.