0
subscribe()
이 호출 될 때마다 nodemcu가 메시지를받는 데 문제가 있습니다. publish()
메시지를 보내는 동안 잘 작동합니다. 브로커는 모기입니다. 심지어 callback()
을 사용했지만 응답이 없습니다. 여기에 코드가 있습니다. 실제로 카드 태그를 읽고 중개인에게 보냈고 보내진 카드 태그를 기반으로 리눅스의 다른 클라이언트가 subscribe()
메소드를 사용하여받은 메시지를 다시 전송하지만 메시지를 검색 할 수는 없습니다. 도움이 필요해.PubSubClient가 Callback()을 사용하여받은 메시지를 표시하지 않음
#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;
}
가 INT (I = 0; I <길이; 내가 ++) { CHAR receivedChar = (숯), 페이로드 [I]; } 콜백 내에서 바이트 배열을 파싱하기 때문에 –
덕분에 을 사용해야합니다. 지금 작동 중입니다. 코드를 조금 변경해야했습니다. –