简体   繁体   中英

Arduino Not Receiving MQTT Subscribed Messages Over ESP8266

I have an MQTT server (Mosquitto) running on a RPi3. I also have a sensor array on my bench that publishes readings to MQTT every 5 seconds. All of this is working great.

Tonight, I wired up an ESP8266-01 to an Arduino Mega using one of the Mega's extra hardware serial ports (rx1, tx1). I'm using two libraries in my sketch: (1) WiFiEsp and (2) PubSubClient. I've tried both O'Leary's PubSubClient and Imroy's branch off O'Leary's PubSubClient. I get the same result with each.

When I power up my Mega with it's ESP8266 shield, it connects to WiFi and the MQTT broker fine. However, its not receiving any messages. It also drops the connection to the MQTT broker every minute or so. It immediately reconnects. The blue transmit LED on the ESP8266 is cycling extremely fast. Way too fast for the message traffic on my MQTT server.

I'm using proper level shifting between the devices. When I put a scope on the Mega's rx1, I see a continuous trace of data that only stops upon disconnect.

In a similar post here , ankur posted just yesterday that there is a bug in the callback. I think he's probably referring to the PubSubClient library. PubSubClient is a very popular library that a lot of people are using. Can anyone comment further on the nature of the bug that ankur alluded to? I'll post my code below.

If anyone else runs into this issue, I also tested another variant of this configuration. I put my WiFi client code and my MQTT client code directly on the ESP8266's micro and connected the ESP8266 to an Arduino Uno via SoftwareSerial at 19200 baud. The connection to my MQTT server is rock solid and all messages are coming through flawlessly.

My code on the Mega follows:

#include <WiFiEsp.h>
#include <PubSubClient.h>

const char *ssid = "********";
const char *pass = "********";
int status = WL_IDLE_STATUS;

#define BUFFER_SIZE 100

IPAddress server(10, 0, 1, 6);

// initialize WiFi client
WiFiEspClient espClient;

// connect to mqtt server
PubSubClient client(espClient);

void callback(const MQTT::Publish& pub) {
    if (pub.has_stream()) {
        uint8_t buf[BUFFER_SIZE];
        int read;
        while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) {
            Serial.write(buf, read);
        }
        pub.payload_stream()->stop();
        Serial.println("");
    } else
        Serial.println(pub.payload_string());
}

void setup() {
    
    pinMode(19, INPUT_PULLUP);  // pull rx1 high

    Serial.begin(9600);
    Serial1.begin(9600);

    WiFi.init(&Serial1);

    // check for the presence of the shield
    if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue
        while (true);
    }

    // attempt to connect to WiFi network
    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network
        status = WiFi.begin(ssid, pass);
    }
    Serial.println("You're connected to the network");
}

void loop() {
    if (!client.connected()) {
        reconnect();
    }
    client.loop();
}

void reconnect() {
    if (!client.connected()) {
        client.set_server(server, 1883);
        client.connect("AVR101");
        client.subscribe("ishnalaIOT");
        client.set_callback(callback);
    }
}

You need to use loop() method of the class PubSubClient.

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