PubSubClient With nodemcu

47 views
Skip to first unread message

Mitul Tyagi

unread,
Nov 10, 2017, 11:32:35 AM11/10/17
to MQTT
Hello, 
I am facing a problem in receiving the message by nodemcu whenever subscribe() is called. publish() is working fine while sending the message. Broker is mosquitto. I have even used callback() but no response. Here is the code. It actually reads the card sends it to broker and based on the card tag sent another client on linux sends back a message which I am receiving using subscribe() command but I am not able to retrieve the message. Any help

#include<Esp.h>
#define SS_PIN 4  //D2
#define RST_PIN 5 //D1
#include<SPI.h>
#include<MFRC522.h>
#include<ESP8266WiFi.h>
#include<PubSubClient.h>
#include <Wire.h>
#include <Adafruit_INA219.h>

int y=HIGH;
int z=HIGH;
String p;
String w;
String l;
String data = "";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const char* ssid = "Nahi Milega Password";
const char* wifi_password = "qazwsxedc789000123";
const char* mqtt_server = "192.168.1.103";
const char* mqtt_topic = "Data";
//const char* mqtt_username = "RFID";
//const char* mqtt_password = "datarfid";
// The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg).
const char* clientID = "NodeMCU";
WiFiClient wifiClient;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
int statuss = 0;
int out = 0;
void callback(char* topic, byte* payload, unsigned int length);
bool initcard();
PubSubClient client(wifiClient); // 1883 is the listener port for the Broker

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() 
{
  Serial.begin(9600);// Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  ESP.wdtEnable(4);
  client.setServer(mqtt_server,1883);
  client.setCallback(callback);
  ESP.wdtDisable();
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  pinMode(0,INPUT);
  pinMode(16,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(10,OUTPUT);
  mfrc522.PCD_Init();// Initiate MFRC522
    WiFi.begin(ssid, wifi_password);

  // Wait until the connection has been confirmed before continuing
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 if (client.connect(clientID)) {
    Serial.println("Connected to MQTT Broker!");
  }
  else {
    Serial.println("Connection to MQTT Broker failed...");
  }
}
void loop() 
{
    client.loop();
  // Look for new cards
  digitalWrite(3,LOW);
  digitalWrite(16,LOW);
  digitalWrite(15,LOW);
  digitalWrite(10,HIGH);
  while(!initcard())ESP.wdtFeed();
  //Show UID on serial monitor
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  data+="{";
  data+="\'";
  data+="ID";
  data+="\'";  
  data+=":";
  data+="\'";
  data+=content;
  data+="\'";
  data+="}";
  Serial.println(data);
  char c[25];
  for (int q=0;q<25;q++)
  c[q]=data[q];
  client.connect("First");
  client.publish("Data2",c);
  w = "z";
  ESP.wdtDisable();
  while(w=="z")
  {
    client.subscribe("Data3");
    if (w!="z")
    { 
      client.disconnect();
      break;
      }
      
   }
  ESP.wdtEnable(4); 
  memset(&c[0], 0, sizeof(c)); 
  Serial.println("Exited from loop 1"); 
  data ="";
  if(w=="0")
  { 
    client.connect(clientID);
    w="z";
  Serial.println("Reached in loop2");  
  y=digitalRead(2);
  z=digitalRead(0);
  Serial.println();
  digitalWrite(10,LOW);
while((y==HIGH) && (z==HIGH))
{  
   y=digitalRead(2);
   z=digitalRead(0);
   digitalWrite(3,HIGH);
   ESP.wdtFeed();
   if((y==LOW)||(z==LOW))
   {
   if (y==LOW) 
   {p="Home";
   digitalWrite(16,HIGH);}
   else{
   p="Market";
   digitalWrite(15,HIGH);}
  data+="{";
  data+="\'";
  data+="ID";
  data+="\'";  
  data+=":";
  data+="\'";
  data+=content;
  data+="\'";
  data+=",";
  data+="\'";
  data+="Purpose";
  data+="\'";
  data+=":";
  data+="\'";
  data+=p;
  data+="\'";
  data+="}";
  Serial.println(data);
   char c[100];
   for (int q=0;q<data.length();q++)
   c[q]=data[q];
  digitalWrite(3,LOW);
  if (client.publish(mqtt_topic,c)) {
      Serial.println("Button pushed and message sent!");
    }
     else {
      Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
      client.connect(clientID);
      delay(1000); // This delay ensures that client.publish doesn't clash with the client.connect call
      client.publish(mqtt_topic,c);
    }
  data="";
  memset(&c[0], 0, sizeof(c));
  delay(1000);
   break;
   }
  }
 }
}

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i=0;i<length;i++) {
  char receivedChar = (char)payload[i];
  Serial.print(receivedChar);
 }
 Serial.println();}

bool initcard()
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    false;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return false;
  }
  return true;
  }


Reply all
Reply to author
Forward
0 new messages