MQTT

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Alpoy
Posts: 4
Joined: Wed Aug 16, 2017 8:41 pm

MQTT

Post by Alpoy » Wed Aug 16, 2017 9:19 pm

Hi,

I can't seem to get MQTT working on my ESP32, I'm using one of the example codes from the uPyCraft util (mqtt.py) but it seems the mqtt module isn't available ...?
This is the line that throws the error:
"from umqtt.simple import MQTTClient"

Error output:
ImportError: no module named 'umqtt'


I've flashed the latest FW from this site (esp32-20170816-v1.9.1-399-g40ae6724.bin) with esptool.

What I am I doing wrong? :?

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: MQTT

Post by torwag » Wed Aug 16, 2017 9:24 pm

The mqtt library does not come with the firmware. You have to install them first using e. g. upip or copy them manually to your board. Sorry for my brevity but I am on the road right now.
Hope it helps a bit.

Alpoy
Posts: 4
Joined: Wed Aug 16, 2017 8:41 pm

Re: MQTT

Post by Alpoy » Wed Aug 16, 2017 9:32 pm

Ah, thank you for the quick answer! I'll try this :)

Alpoy
Posts: 4
Joined: Wed Aug 16, 2017 8:41 pm

Re: MQTT

Post by Alpoy » Thu Aug 17, 2017 6:44 am

Thanks again, everything worked after I uploaded the "umqtt" dir with the simple.py file in, in hindsight it's kind of obvious ;)

chale
Posts: 2
Joined: Fri Aug 11, 2017 2:40 pm

Re: MQTT

Post by chale » Thu Aug 17, 2017 6:47 pm

If it's OK to tag on to this thread, I am looking for the basics of MQQT on ESP32 with micropython. I don't have an MQQT issue yet except for my own ignorance of the process for inclusion in the binaries.
I have a functional ESP32 devkitC, built micropython for ESP32 and flashed a new main.py to execute on reboots. I'm running windows 10, but developing on VirtualBox(Ubuntu 16.04 LTS) - so we're functional. Now it's time to move fwd with functionality like MQQT.

If there is some basic reading I need to do first, please point me to it. I'm a little lost when trying to apply general micropy topics to micropy/esp32 - don't quite understand what can "just be added to my build." It seems like all the general comments on tools in the uPy area may not apply to the esp32-specific area? (again, just trying to understand the intended relationships between them so that I think about problems correctly).

To whit, some basic questions:
1) Do I need to move to daily (or non-major) uPy/esp32 builds to get this MQQT functionality into my esp32 directories and then installed in my flash?
2) Are uasyncio and mqqt included in esp32 builds? I'm struggling to figure out basic layout of modules. I did learn to put my own modules in the esp32/modules directory and then rebuild/flash. Should I go ahead and do this with uasyncio and mqqt?
3) What is the proper way to fetch these modules and place them into my esp32 build? would I go to my linux prompt and install them using github?

I figure I will learn the basic build-flash workflow before graduating to upip (unless directed to a better approach).

Thanks for helping a noob along.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: MQTT

Post by torwag » Thu Aug 17, 2017 7:14 pm

Hi, there is no need to recompile and reflash after doing it initially unlike :
1. one want to update micropython and the precompiled modules coming with it
2. want to add modules to the image, so called frozen bytecode, to have a customised version of mp or for memory safeing reasons.
Micropython comes with a little file system pretty much like a usb memory stick but much smaller, created in the memory of the chip. This can be used to store modules beside of self written files e. g. boot.py and main.py. Simply copy them over as you would copy a file to somewhere else. There are tools to do this.

Actually it is just one of the major advantages of micropython that you do not have to recompile and reflash after every single change.

Furthermore, as soon as you have micropython running, all the stuff you find for other platforms could in principle work on the esp32. However,
1. ESP32 is still experimental and rather new, some stuff might not be yet included or is buggy.
That was the reason I mentioned the esp8266 port which is much more stable and mature. As both ports are very similar in many aspects, one can assume that if something works under the esp8266 it should eventually work under the esp32, however for now it might take a while to implement all what is needed and to fix bugs. Devs are happy for every helping hand.
2. Sure nobody expects something to work what is physically not available. If one chip has a high speed ADC, but another chip has not, nobody can add this (in software).

As for MQTT (one Q two T) it is a lean protocol to send and receive data between different units. It requires however a server, called broker., I guess looking for MQTT and broker will give you a good idea...

Enjoy

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

Re: MQTT

Post by pythoncoder » Fri Aug 18, 2017 5:49 am

@chale This thread viewtopic.php?f=16&t=2239 provides a basic guide to MQTT which you may find useful.

But I'd start out by learning the basic workflow with MicroPython targets. Write a simple "hello world" program, copy it to the filesystem on the ESP32, enter the REPL and run it. To do this you'll need a tool. I use Dave Hylands' rshell https://github.com/dhylands/rshell - using this the ESP32 filesystem may be found under /pyboard so you can copy your program there, type repl and run it.

Then learn how to install a library (e.g. umqtt) https://github.com/micropython/micropython-lib.

As @torwag says, there is no need to recompile. This will only arise if you start writing large programs.
Peter Hinch
Index to my micropython libraries.

Alpoy
Posts: 4
Joined: Wed Aug 16, 2017 8:41 pm

Re: MQTT

Post by Alpoy » Fri Aug 18, 2017 10:01 am

Chale, I uploaded the mqtt and asyncio module to the esp32, and that was it.

Note: I had to compile the unix version of the upython on a *nix box I have, to use the upip command to find the uasyncio module and then upload it to the ESP32.
(https://github.com/micropython/micropython). I'm sure there is a simpler way to get the module but atleast it worked ;-)

The folder structure of my ESP32 was then:
myscript.py
umqtt/simple.py
uasyncio/__init__.py
uasyncio/core.py

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

Re: MQTT

Post by pythoncoder » Sat Aug 19, 2017 4:36 am

@Alpoy It should be possible to run upip on the target device but a quick test confirms that this doesn't yet work on the ESP32. So your approach is the best way to install a library.
Peter Hinch
Index to my micropython libraries.

Post Reply