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