ds_k7
ds_k7

Reputation: 23

NodeMCU (ESP8266) Exception 28 when connecting to MQTT broker

I am using a NodeMCU (ESP8266) as a WiFi client to connect to an MQTT broker run on my computer. Using this setup in Windows using WSL (Ubuntu), the MQTT broker seems to work perfectly fine. The ESP8266, however, throws an error in the Serial console immediately when it attempts connect to the MQTT server. WiFi connects without incident.

Please find below a working example of my code:

#include <ESP8266WiFi.h>
#include <MQTT.h>

const char ssid[] = "MyWiFiNetwork";
const char pass[] = "MyWiFiPassword";

WiFiClient net;
MQTTClient client;


void connect() {
  Serial.print("Connecting to broker...");

  while (!client.connect("arduino")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected to broker!");
}


void setup() {
  Serial.begin(9600);

  WiFi.begin(ssid, pass);
  Serial.print("Attempting to connect to ");
  Serial.println(ssid);

  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  if(WiFi.status() == WL_CONNECTED) {
    Serial.print("\nWiFi connected to ");
    Serial.println(WiFi.SSID());
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway: ");
    Serial.println(WiFi.gatewayIP());
    Serial.println("");
  }

  connect();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Running this code produces the following exception the moment it reaches

while(!client.connect("arduino", "try", "try")) {

Exception (28):
epc1=0x40203051 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd80 end: 3fffffc0 offset: 01a0
3fffff20:  3ffee6e4 00000003 0000000b 3ffee764  
3fffff30:  3ffe85a2 00000000 3ffee6e4 402043ec  
3fffff40:  3ffe85a2 3ffee658 3ffee6e4 40204665  
3fffff50:  3ffe884f 3ffee658 3fffff90 40204665  
3fffff60:  3ffe8851 3ffee658 3ffee6e4 40204690  
3fffff70:  dc2ba8c0 00ffffff 3ffee6e4 3ffee764  
3fffff80:  3ffe85a2 3ffee658 3ffee6e4 402031bb  
3fffff90:  40205188 412ba8c0 00000000 feefeffe  
3fffffa0:  3fffdad0 00000000 3ffee734 40204ca4  
3fffffb0:  feefeffe feefeffe 3ffe8508 40100801  
<<<stack<<<

From other forum posts on here, I figured out how to decode the stack exception to produce this:

0x402043ec: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\Josh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/HardwareSerial.h line 175
0x40204665: Print::write(char const*) at C:\Users\Josh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/Print.h line 60
0x40204665: Print::write(char const*) at C:\Users\Josh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/Print.h line 60
0x40204690: Print::println() at C:\Users\Josh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\Print.cpp line 178
0x402031bb: setup() at C:\Users\Josh\Desktop\ssid_scan/ssid_scan.ino line 52
0x40204ca4: loop_wrapper() at C:\Users\Josh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp line 122

But at this point I'm hitting a wall. I don't know what to make of it, and I've tried investigating the files shown in the exception to no avail. Anyone know what this means or what the error is even saying?

The libraries I am using are:

ESP8266WiFi.h by Arduino

MQTT.h by Joel Gaehwiler

Many thanks in advance! I've been at this for 6+ hours and I'm dead.

Upvotes: 2

Views: 9204

Answers (2)

ds_k7
ds_k7

Reputation: 23

The solution to my problem was actually two-fold:

As John Romkey pointed out, I was missing the line in my script that tells my ESP8266 where the broker is. I needed the following:

  client.begin("IP_ADDRESS_OF_BROKER", net);

However, I also needed to disable Windows Firewall since I'm running this on WSL. Go figure.

Hopefully someone else who stays up late and misses a small detail won't waste multiple hours trying to figure it out. Thank you!

Upvotes: 0

romkey
romkey

Reputation: 7054

You're missing some setup on the MQTT client. Unfortunately, this MQTT library isn't clever enough to notice you didn't set a server name and crashes when you call connect() without it being fully set up.

You need a call to the begin() method before you call connect().

Try rewriting your connect() function like this:

void connect() {
  Serial.print("Connecting to broker...");

  client.begin("MQTT-SERVER-HOSTNAME", net);

  while (!client.connect("arduino")) {

If you need to specify a port number other than the default (1883) you can specify an integer port number after the server's domain name in the begin() method.

Upvotes: 2

Related Questions