【问题标题】:Not able to Receive subscribed Message with PubSubClient.h in Arduino Uno R3无法在 Arduino Uno R3 中使用 PubSubClient.h 接收订阅的消息
【发布时间】:2017-03-29 05:09:17
【问题描述】:
#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.h client.subscribe("主题");返回 1 还实现了回调函数,但无法调用。

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

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

【问题讨论】:

  • 这到底是什么意思? “我能够从 Arduino 向所有其他客户接收数据,但我无法在 Arduino 中接收订阅主题的数据。”另外,请将您的代码与您的问题一起发布,以便您更轻松地获得帮助。
  • 我见过的所有 esp8266 的 mqtt 客户端都需要 esp 上的代码,而不仅仅是 AT 命令
  • 嗨,我已将温度传感器与 Arduino 连接,它会延迟 5 秒读取温度并发布到 Mosquitto 代理,Mosquitto 代理将该温度数据发送到订阅温度的 MySQL、Raspberry Pi 和 Web 客户端数据。如果温度升高到 60 以上,我必须对 Raspberry Pi 保持警惕。这一切正常。
  • 我必须从 Raspberry Pi 控制 Arduino,因此我必须使用 Mosquitto 代理将消息从 RPI 发布到 Arduino。所以我订阅了一个主题,例如“receivedRPIData”。现在我可以在 MySQL 客户端和 Web 上接收“receivedRPIData”的数据。但无法在 Arduino 中接收消息。我已经订阅了“receivedRPIData”主题,如果我在串行监视器上打印它会返回 1 client.subscribe("receivedRPIData")
  • 那么我没有得到什么问题。正如我使用此链接中的代码sonyarouje.com/2016/03/15/…

标签: arduino android-wifi esp8266 arduino-esp8266


【解决方案1】:

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

【讨论】:

  • 他指的是 PubSubClient 库,而 TinyGSM 依赖于 knolearry 的 PubSubClient。 GitHub 上还有 lmroy 的其他版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多