在Arduino Uno R3中无法通过PubSubClient.h接收订阅的消息 [英] Not able to Receive subscribed Message with PubSubClient.h in Arduino Uno R3
问题描述
#include "SPI.h"
#include "WiFiEsp.h"
#include <WiFiEspClient.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>
#include <WiFiEspUdp.h>
float temp=0;
int tempPin = 0;
int isClientConnected = 0;
char data[80];
char ssid[] = "SSID"; // your network SSID (name)
char pass[] = "PASSWORD"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio’s status
char deviceName = "ArduinoClient1";
IPAddress server(xxx,xxx,xxx,xxx); //MQTT server IP
IPAddress ip(192,168,43,200);
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++) {
Serial.print((char)payload[i]);
}
Serial.println("-");
}
// Emulate Serial1 on pins 6/7 if not present
WiFiEspClient espClient;
PubSubClient client(espClient);
SoftwareSerial Serial1(6,7); // RX, TX
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
WiFi.init(&Serial1);
WiFi.config(ip);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
while ( status != WL_CONNECTED) {
Serial.print("Attemptingonnect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
Serial.print("WiFius : ");
Serial.println(status);
}
//connect to MQTT server
client.setServer(server, 1883);
client.setCallback(callback);
isClientConnected = client.connect(deviceName);
Serial.println("+++++++");
Serial.print("isClientConnected;
Serial.println(isClientConnected);
Serial.print("client.state");
Serial.println(client.state());
if (isClientConnected) {
Serial.println("Connected…..");
client.publish("status","Welcome to ISG");
client.subscribe("isg/demoPublish/rpi/ardTempWarn");
//Not able to recieve for this subscribed topic on Arduino Uno Only if I
//print it returns 1
}
}
void loop() {
temp = (5.0 * analogRead(tempPin) * 100.0) / 1024;
Serial.print(" temp : " );
Serial.println(temp);
Serial.print("client.connected);
Serial.println(client.connected());
if (!client.connected()) {
reconnect();
}
client.publish("isg/demoPublish/ard1/tempLM35",String(temp).c_str());
// able to receive data at other
// clients like RPI,Web using Mosquitto broker
client.loop();
delay(5000);
}
void reconnect() {
Serial.println("Device is trying to connect to server ");
while (!client.connected()) {
if (client.connect(deviceName)) {
} else {
delay(5000);
}
}
}
我正在使用Arduino Uno R3和ESP8266-01作为wifi连接器. 我必须读取温度数据并发送到Mosquitto,MongoDB和Raspberry Pi,并接收有关特定条件的数据,因为我已经在Arduino中订阅了一个主题. 我能够从Arduino接收所有其他客户端的数据,但无法在Arduino的Subscribed主题上接收数据.但是所有其他设备(例如MongoDB)都能够从Raspberry Pi接收数据. 我已经使用了Arduino Uno R3,ESP8266-01设备和库来连接和发送/接收数据WiFiEsp.h,WiFiEspClient.h,WiFiEspUdp.h,SoftwareSerial.h,PubSubClient.h client.subscribe("topic");返回1 还实现了回调功能,但无法获得呼叫.
I am using Arduino Uno R3 and ESP8266-01 as wifi connector. I have to read temperature data and send to Mosquitto ,MongoDB and Raspberry Pi and receive a data on specific condition for that i have subscribed a topic in Arduino. I am able to receive data from Arduino to all other clients but I am not able to receive data on Subscribed topic in Arduino. But all other deviced like MongoDB able to receive data from Raspberry Pi. I have used Arduino Uno R3, ESP8266-01 devices and liberary for to connect and send/receive data WiFiEsp.h, WiFiEspClient.h, WiFiEspUdp.h, SoftwareSerial.h, PubSubClient.h client.subscribe("topic"); returns 1 Also callback function implemented but not able to get call.
那么任何人都可以帮助我为什么我没有在Arduino中获得订阅的主题消息吗?
So can any one help me why I am not getting subscribed topic message in Arduino?
我已关注推荐答案
您正在使用的库在回调中存在错误,因此最好使用其他库,我的偏爱是
https://github.com/vshymanskyy/TinyGSM
该库几乎包含SIM800(A,C,L,H,808)的所有变体,SIM900(A,D,908,968),ESP8266(安装在arduino上),以太网屏蔽等的变体.困扰了我大约1-2个星期,但无论通信方式(GSM,以太网,WiFi)如何,后者都能接收所有订阅的消息 The library that you are using has bug in callback hence it would be better that you use other library my preference would be
https://github.com/vshymanskyy/TinyGSM
this library include almost all variants of SIM800(A,C,L,H,808), variants of SIM900(A,D,908,968), ESP8266 (mounted on arduino), Ethernet shield etc. It worked for me, as same issue bugged me for almost 1-2 weeks but latter was able to receive all subscribed message irrespective of mode of communication(GSM,Ethernet,WiFi) 这篇关于在Arduino Uno R3中无法通过PubSubClient.h接收订阅的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!