简体   繁体   中英

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. 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.

So can any one help me why I am not getting subscribed topic message in Arduino?

I have follow 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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