Looking for some help with mqtt

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ryanjennings
Posts: 4
Joined: Tue Aug 30, 2016 12:29 pm

Looking for some help with mqtt

Post by ryanjennings » Wed Aug 31, 2016 10:16 pm

I am just getting started with micropython and the esp8266 so I am sure this is a pretty low level question, but I am stuck.

I have read over http://forum.micropython.org/viewtopic.php?f=16&t=2155 and I am sure the answer is there somewhere, but I must need more detail.

When I do
from umqtt.simple import MQTTClient
I get ImportError: no module named 'umqtt'

I am running 1.8.3-78

Thanks for the help,
Ryan

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Looking for some help with mqtt

Post by SpotlightKid » Wed Aug 31, 2016 11:19 pm

You need to upload the files of the "umqtt" module to your esp8266. You can find the module files in the micropython-lib Github reposiotory. You can use the Webrepl for uploading or a tool like mpfshell.

For the import command you're using, you need a directory structure like this in your esp8266's flash:

Code: Select all

umqqt/
umqqt/__init__.py
umqqt/simple.py
But if you only want to use the simple API, it might be easier to just copy the "simple.py" file to the root dir as "umqtt_simply.py" and then use

Code: Select all

from umqtt_simple import MQTTClient
Last edited by SpotlightKid on Sat Sep 03, 2016 1:04 pm, edited 1 time in total.

ryanjennings
Posts: 4
Joined: Tue Aug 30, 2016 12:29 pm

Re: Looking for some help with mqtt

Post by ryanjennings » Thu Sep 01, 2016 1:53 am

Thanks for the help. I will give that a try. I thought I had read that mqtt was included after 1.8.2 I was assuming that meant I didn't need to upload it. I had read this before from

[quote]"It was, in the official 1.8.2 release, and intended to be part of future releases.[/quote]

What does "in the release mean"? Just that it has officially been adopted, rather than someone's own library?

Thanks for helping out a new guy.

Ryan

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

Re: Looking for some help with mqtt

Post by pythoncoder » Thu Sep 01, 2016 8:55 am

I gather it's available in 'release' builds and not in nightly builds - a fact which is not self-evident ;) So try esp8266-20160809-v1.8.3.bin
Peter Hinch
Index to my micropython libraries.

binghamd
Posts: 2
Joined: Sat Oct 08, 2016 1:50 pm

Re: Looking for some help with mqtt

Post by binghamd » Sat Oct 08, 2016 2:01 pm

Like Ryan, I an new to the world of micropython and ESP8266.
I have followed Spotlightkid's suggestion and downloaded simple.puy to a ESP8266 mini board as umqtt_simple.py
From REPL and WebREPL(on a windows PC) I can connect the ESP to a Mosquitto server running on a raspberry pi and publish to it successfully, but when I try to do exactly the same in main.py it will not connect with error:-
Traceback (most recent call last):
File "main.py", line 11, in <module>
File "main.py", line 6, in main
File "umqtt_simple.py", line 56, in connect
OSError: [Errno 103] ECONNABORTED
programme is simply:-
from umqtt_simple import MQTTClient

def main(server="192.168.1.19"):
c = MQTTClient("umqtt_client", server)
c.connect()
c.publish(b"testESP", b"hello")
c.disconnect()

if __name__ == "__main__":
main()

Any suggestions would be much appreciated.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Looking for some help with mqtt

Post by SpotlightKid » Sat Oct 08, 2016 8:12 pm

How do you connect to the wifi network? Are you sure the esp is connected when main.py is executed?

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

Re: Looking for some help with mqtt

Post by pythoncoder » Sun Oct 09, 2016 8:58 am

@SpotlightKid is surely right. It takes some time after power up for the ESP8266 to connect - 5 to 10 seconds here. Try executing a loop along these lines before attempting to use the LAN.

Code: Select all

import utime
sta_if = network.WLAN(network.STA_IF)
while not sta_if.isconnected():
    utime.sleep(1)
Peter Hinch
Index to my micropython libraries.

binghamd
Posts: 2
Joined: Sat Oct 08, 2016 1:50 pm

Re: Looking for some help with mqtt

Post by binghamd » Sun Oct 09, 2016 12:32 pm

Thank you pythoncoder, that did the trick.

Post Reply