knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

MQTT timeout when also using MFRC522 rfid reader #982

Closed andrewl closed 1 year ago

andrewl commented 1 year ago

I'm running a Wemos D1 mini with an rfid reader, using the MFRC522 library https://github.com/mdxs/MFRC522.

Running the Basic ESP8266 MQTT example works absolutely perfectly. I can also add the MFRC522 library (ie add the header files) and again, it works just fine.

However, I consistently get disconnections simply by adding the MFRC522 library and initiating it by adding the following lines to the setup() function:

` Serial.println("Calling SPI.begin()"); SPI.begin(); // Initiate SPI bus

delay(10); Serial.println("Calling PCD_Init()"); mfrc522.PCD_Init(); // Initiate MFRC522`

( Just initiating the SPI bus (calling SPI.begin()) doesn't lead to the error, it only occurs when the mfrc522.PDC_Init() is called during setup).

The same thing happens if I use the entirely refactored MFRC522v2 library (https://github.com/OSSLibraries/Arduino_MFRC522v2)

The disconnection errors look like this in the serial monitor, and whilst in some instances it reconnects after a few attempts, sometimes it steadfastly refuses to reconnect at all. Again, if I don't call mfrc522.PCD_Init(); it works just fine.

15:10:24.993 -> Publish message: hello world #4 15:10:44.990 -> Publish message: hello world #5 15:10:55.288 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 15:11:05.447 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 15:11:15.641 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 15:11:25.828 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 15:11:36.019 -> Attempting MQTT connection...connected 15:11:45.146 -> Publish message: hello world #6

I'm going to try these two RFID libraries with a different MQTT library and compare results.

KrappRamiro commented 1 year ago

try using this reconnect function i got from #526

void reconnect()
{
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print("Attempting MQTT reconnection...");
        // Attempt to connect
        if (client.connect(THINGNAME, AWS_IOT_PUBLISH_TOPIC, 0, true, "OFFLINE")) {
            Serial.println("connected");
            // Once connected, publish an announcement...
            client.publish(AWS_IOT_PUBLISH_TOPIC, "ONLINE");
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}

Put it in the start of the void loop, and see if it works

andrewl commented 1 year ago

Thanks @KrappRamiro - still the same though. I've also tried another MQTT library, Adafruits (https://github.com/adafruit/Adafruit_MQTT_Library), and that too exhibits similar behaviour and ultimately results in a s/w watchdog reset. I've got a few next steps

andrewl commented 1 year ago

I'm now thinking this is a hardware issue. I had the same issue when trying to make http requests instead of mqtt requests, and the problem goes away entirely if I move everything onto a breadboard. Closing this issue for now.