PubSubClient不显示使用回调接收的消息()

时间:2017-11-10 16:49:25

标签: mqtt publish-subscribe nodemcu arduino-esp8266

每当调用subscribe()时,我都会遇到nodemcu接收消息的问题。 publish()在发送邮件时工作正常。经纪人是蚊子。我甚至使用过callback()但没有回复。这是代码。它实际上读取卡片标签,将其发送到代理,并根据发送的卡片标签,linux上的另一个客户端发送回我使用subscribe()方法接收的消息,但我无法检索该消息。任何帮助。

#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_topic2 = "Data2";
const char* mqtt_topic3 = "Data3";


//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(mqtt_topic2,c);
  client.subscribe(mqtt_topic3);
  memset(&c[0], 0, sizeof(c)); 
  Serial.println("Exited from loop 1"); 
  data ="";
  Serial.println(w);
  if((w=="b")||(w=="c"))
  { w="";
  client.connect(clientID);
  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) {
 w=""; 
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i=0;i<length;i++) {
  char receivedChar = (char)payload[i];
  w+=receivedChar;
  Serial.print(w);
 }
 Serial.println();}

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

1 个答案:

答案 0 :(得分:0)

这应该有效: -

#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_topic2 = "Data2";
const char* mqtt_topic3 = "Data3";


//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(mqtt_topic2,c);
  client.subscribe(mqtt_topic3);
  memset(&c[0], 0, sizeof(c)); 
  Serial.println("Exited from loop 1"); 
  data ="";
  Serial.println(w);
  if((w=="b")||(w=="c"))
  { w="";
  client.connect(clientID);
  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) {
 w=""; 
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
 }
 Serial.println();
 }

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