无法在 Arduino Uno R3 中使用 PubSubClient.h 接收订阅的消息 [英] Not able to Receive subscribed Message with PubSubClient.h in Arduino Uno R3

查看:65
本文介绍了无法在 Arduino Uno R3 中使用 PubSubClient.h 接收订阅的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 中接收有关订阅主题的数据.但是所有其他设备(如 MongoDB)都能够从 Raspberry Pi 接收数据.我使用 Arduino Uno R3、ESP8266-01 设备和库来连接和发送/接收数据 WiFiEsp.h、WiFiEspClient.h、WiFiEspUdp.h、SoftwareSerial.h、PubSubClient.hclient.subscribe("主题");返回 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://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp-01/#comment-111773

推荐答案

您使用的库在回调中存在错误,因此您最好使用其他库,我的偏好是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆