Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266 first call to loop times out #184

Open
tomkcook opened this issue Jul 24, 2016 · 6 comments
Open

ESP8266 first call to loop times out #184

tomkcook opened this issue Jul 24, 2016 · 6 comments

Comments

@tomkcook
Copy link

I'm setting up PubSubClient on an ESP8266. I'm using WiFiClientSecure as the client class to connect using SSL.

Connection to the MQTT server works, but 9 times out of 10 the first call to PubSubClient::loop causes a watchdog timeout and reboot.

Looking further into it, it seems that the problem is in PubSubClient::readPacket(uint8_t*). The first loop in that function reads the length of the following payload but the value of length calculated appears to be more or less random - frequently in the tens of thousands. The second loop reads eight bytes and then blocks waiting for data from the socket, and the value of MQTT_SOCKET_TIMEOUT is long enough that the watchdog times out.

The MQTT server is mosquitto running on Ubuntu.

@tomkcook
Copy link
Author

Looking a bit further, it seems the first byte of the packet (with the packet type etc) is also essentially random. Does this mean that data from the previous packet is being left over?

@tomkcook
Copy link
Author

The content of the first packet is always:

20 02 00 00

This looks to me like a correct 'connection acknowledgement' packet. Here's a selection of second packets I receive:

ac 5c 53 1a 27 90 03 00 03 00

45 c0 33 44 54 90 03 00 03 00 

14 d1 3c 92 59 90 03 00 03 00 

9b 09 89 99 12 90 03 00 03 00 

ad b7 b8 fa 28 90 03 00 03 00 

The last five bytes of each of these looks like a valid SUBACK packet. Any idea what the first five bytes of each are???

@tomkcook
Copy link
Author

Another data point: This only happens if I subscribe to more than one topic. As in:

mqtt.subscribe("/topic/a");
mqtt.subscribe("/topic/b");

If I comment out the second subscribe, everything goes fine.

I note that the protocol has support for multiple subscriptions in one packet; if I get a bit of time, I'll add support for it.

@Unchapped
Copy link

I'm having similar issues with the ESP8266, my code works fine when compiled on an Arduino Yun, so it seems that something specific is causing problems with the wdt reset during the first call to loop().

subscribing to only one topic seems to forestall the issue a few cycles, but it still occurs at some point. I'm happy to help dig in and find the issue.

@Unchapped
Copy link

Unchapped commented Aug 20, 2016

Looking here:
https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/using-the-arduino-addon

seems we just need to add a call to yield() in any blocking loops to feed the wdt in the functions you describe, I'll give that a try and see how it goes.

@nootropicdesign
Copy link

I was having the same problem with the first call to loop() taking a long time, but only if there were multiple subscriptions. The solution is to call loop() after each call to subscribe().

mqtt.subscribe("/topic/a");
mqtt.loop();
mqtt.subscribe("/topic/b");
mqtt.loop();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants