简体   繁体   English

连接到MQTT代理时,NodeMCU(ESP8266)异常28

[英]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. 我正在使用NodeMCU(ESP8266)作为WiFi客户端连接到计算机上运行的MQTT代理。 Using this setup in Windows using WSL (Ubuntu), the MQTT broker seems to work perfectly fine. 在Windows中使用WSL(Ubuntu)使用此设置,MQTT代理似乎运行得很好。 The ESP8266, however, throws an error in the Serial console immediately when it attempts connect to the MQTT server. 但是,ESP8266在尝试连接到MQTT服务器时会立即在串行控制台中引发错误。 WiFi connects without incident. WiFi连接无任何意外。

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")) { 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 Arduino的ESP8266WiFi.h

MQTT.h by Joel Gaehwiler Joel Gaehwiler的MQTT.h

Many thanks in advance! 提前谢谢了! I've been at this for 6+ hours and I'm dead. 我在这里待了6个多小时,我已经死了。

You're missing some setup on the MQTT client. 您缺少MQTT客户端上的某些设置。 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. 不幸的是,这个MQTT库不够聪明,以至于您没有设置服务器名称,而在未完全设置的情况下调用connect()时会崩溃。

You need a call to the begin() method before you call connect() . 在调用connect()之前,需要先调用begin()方法。

Try rewriting your connect() function like this: 尝试像这样重写connect()函数:

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. 如果需要指定默认端口号(1883)以外的端口号,则可以在begin()方法中在服务器域名之后指定一个整数端口号。

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. 正如约翰·罗马基指出的那样,我错过了我的脚本告诉我ESP8266 其中券商行。 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. 但是, 我还需要禁用Windows防火墙,因为我正在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! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM