0

I'm trying to use NodeMCU on my ESP8266 (NodeMCU 0.9) but the board keep crashing and I don't understand why.

I tested the mqtt broker using command line and it works perfectly, so I can suppose that the problem is not the server.

I was following this guide but it doesn't seem to work with me.

Here's the code I wrote:

Previously:

#include <PubSubClient.h>
#define MQTT_SERVER    "1.2.3.4"
#define MQTT_PORT      1883
#define MQTT_USER     "user"
#define MQTT_PSW      "psw"
...
WiFiClient client;

Then:

PubSubClient clientMqtt(MQTT_SERVER, MQTT_PORT, this->client);
//Try mqtt connection
while (!clientMqtt.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (clientMqtt.connect("ESP8266Client", MQTT_USER, MQTT_PSW)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(clientMqtt.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
}

//Try send message
clientMqtt.publish("bin", "test", true);

The code works correcty until clientMqtt.connect("ESP8266Client", MQTT_USER, MQTT_PSW) where at the first/second attempt crashes.

This is the output:

Attempting MQTT connection...
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...

Soft WDT reset

ctx: cont 
sp: 3ffefa90 end: 3ffefe30 offset: 01b0

>>>stack>>>
3ffefc40:  3ffe897d 40202c5d 3ffefcc4 40202dab  
3ffefc50:  3ffe89b7 3ffefcc0 3ffefcc4 40203cdc  
3ffefc60:  514d0400 00045454 3ffe89a7 40204cdc  
3ffefc70:  3ffe89c0 3ffe89a9 00000000 3ffe88fa  
3ffefc80:  3ffeeb70 3ffefdb0 3ffeedd8 3ffe88fa  
3

The Soft WDT reset means that the software watchdog timer reset. Depending on the version of Arduino core to the ESP8266 using delay() can cause this to happen, though current versions handle this better, and using delay() does not cause problems. Make sure you are up-to-date with the Board Manager definitions for ESP8266-based projects.

This page has lots of information on the watchdog timers and especially notes how to use a tool called ESP8266/ESP32 Exception Stack Trace Decoder to determine exactly what line in your code caused the reset to happen.

This page notes that the PubSubClient library can cause problems with the watchdog feeding and gives examples of some alternative yield() and delay() functions you can include and call instead of the built-in versions, which help alleviate resets when using PubSub on ESP8266.

The software watchdog timeout is on the order of 3 seconds, so you can also try reducing your delay(5000) to something shorter, like delay(1000) while debugging, to rule out the 3-second WDT reset due to the delay() call.

Your output also indicates status -2 to the state() function. The documentation for PubSubClient notes that this means "the network connection failed". So one other thing to check is that your device actually connects to the WiFi network and that the MQTT server is listening on the IP address and port you specify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.