简体   繁体   English

无法在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);
        }
    }
}

I am using Arduino Uno R3 and ESP8266-01 as wifi connector. 我使用Arduino Uno R3和ESP8266-01作为wifi连接器。 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. 我必须读取温度数据并发送到Mosquitto,MongoDB和Raspberry Pi并接收有关特定条件的数据,因为我已经在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. 我能够从Arduino接收所有其他客户的数据,但我无法接收Arduino中订阅主题的数据。 But all other deviced like MongoDB able to receive data from Raspberry Pi. 但像MongoDB这样的所有其他设备都能够从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"); 我使用了Arduino Uno R3,ESP8266-01设备和liberary用于连接和发送/接收数据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. 返回1还实现了回调函数但无法调用。

So can any one help me why I am not getting subscribed topic message in Arduino? 所以任何人都可以帮助我为什么我没有在Arduino中订阅主题消息?

I have follow https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/#comment-111773 我关注https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM