无法在Arduino Uno R3中使用PubSubClient.h接收订阅的消息

时间:2016-11-15 10:44:19

标签: arduino android-wifi esp8266 arduino-esp8266

#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设备和liberary用于连接和发送/接收数据WiFiEsp.h,WiFiEspClient.h,WiFiEspUdp.h,SoftwareSerial.h,PubSubClient.h  client.subscribe( “主题”);返回1  还实现了回调函数但无法调用。

所以任何人都可以帮助我为什么我没有在Arduino中订阅主题消息?

我已关注https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/#comment-111773

1 个答案:

答案 0 :(得分:0)

您正在使用的库在回调中存在错误,因此您最好使用我喜欢的其他库 https://github.com/vshymanskyy/TinyGSM 这个库包括SIM800(A,C,L,H,808)的几乎所有变体,SIM900(A,D,908,968)的变体,ESP8266(安装在arduino上),以太网屏蔽等。它对我有用,同样的问题我差错了将近1-2周,但后者无论通讯方式如何(GSM,以太网,WiFi)都能收到所有订阅的消息