MQTT on Esp8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jarekd
Posts: 13
Joined: Mon Aug 21, 2017 3:54 pm

MQTT on Esp8266

Post by jarekd » Mon Jan 29, 2018 9:28 pm

Hey :) I wanted to play a little with MQTT and Adafruit.IO, as I'm building some sensors for my home. But when I import "umqtt" library it looks empty... (help(umqtt) lists only __name__ and __path__ there) Tried to use library made for CirquitPython (Adafruit version of MicroPython) but lib don't work correctly - throws some errors on standard MicroPython. Is there any other library that I can use?

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: MQTT on Esp8266

Post by cefn » Mon Jan 29, 2018 9:48 pm

You need to choose between umqtt.simple and umqtt.robust, which are modules within umqtt, although maintained as separate subdirectories at micropython-lib.

https://github.com/micropython/micropyt ... qtt.simple
https://github.com/micropython/micropyt ... qtt.robust

If you are in a position to experiment with a more complex build, you could try mqtt_as from...
https://github.com/peterhinch/micropython-mqtt

jarekd
Posts: 13
Joined: Mon Aug 21, 2017 3:54 pm

Re: MQTT on Esp8266

Post by jarekd » Mon Jan 29, 2018 10:58 pm

Thank you : )
Just few minutes before reading your answer found info about mqtt.simple and mqtt.robust.
Anyway, in the meantime, I have created (very simple) communication between my modules and Mac in python using sockets and simple messages - it works, so no need for mqtt - for now at least.

User avatar
chrisvoo
Posts: 2
Joined: Tue Feb 13, 2018 8:16 am
Location: Italy

Re: MQTT on Esp8266

Post by chrisvoo » Tue Feb 13, 2018 11:30 am

Hi, I'm using simple and robust micropython implementation, as posted above on a WiPy3, using Aedes as MQTT broker. I noticed that after around 30 minutes, I'm not able to send messages to my broker anymore.
Since I thought it could be a case of disconnection (even if I haven't seen nor any reconnection message neither any other type of error, both on client side and server side), I tried to play with keepAlive option, specifying a value greater than 0, but I haven't spotted any difference even specifying 3600. To be more precise, I just replaced 0 with 3600 here in the constructor (https://github.com/micropython/micropyt ... ple.py#L10).
This behavior is present in a real application, where my device is at home and the broker is deployed on Amazon, I never saw any problem in local.
I thought that "check_msg" method would be enough to maintain the connection, but at the moment it seems that the only one is to publish a message to the broker.
Do you have any advice?
Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MQTT on Esp8266

Post by pythoncoder » Wed Feb 14, 2018 9:43 am

chrisvoo wrote:
Tue Feb 13, 2018 11:30 am
...I thought that "check_msg" method would be enough to maintain the connection, but at the moment it seems that the only one is to publish a message to the broker...
The official MQTT libraries don't ping the broker*. The broker should be sent a ping or a publication at least once in the keepalive period, otherwise when the keepalive period expires the broker assumes the connection has failed. It then sends the last will and closes the connection.

So, to use the official libraries with a permanently open connection, you need to issue regular publications. Given the inherent imperfection of wireless links I favour sending publications about three times per keepalive period to keep the link open.

However this doesn't explain why your connection is going down after 30 minutes when the keepalive period is an hour. Debugging this might be easier if you used the last will feature: you'd then get positive confirmation that the broker was downing the connection.

* This resilient asynchronous library does ping the broker.
Peter Hinch
Index to my micropython libraries.

User avatar
chrisvoo
Posts: 2
Joined: Tue Feb 13, 2018 8:16 am
Location: Italy

Re: MQTT on Esp8266

Post by chrisvoo » Wed Feb 14, 2018 9:54 am

[quote=pythoncoder post_id=25446 time=1518601410 user_id=265]
However this doesn't explain why your connection is going down after 30 minutes when the keepalive period is an hour. Debugging this might be easier if you used the last will feature: you'd then get positive confirmation that the broker was downing the connection.

* This [url=https://github.com/peterhinch/micropython-mqtt.git]resilient asynchronous[/url] library does ping the broker.
[/quote]

Thanks, I can't explain it too, I'll use the last will feature. It's not clear if there's some settings issue with my broker, I'll try also to investigate more about it.

Post Reply