Minimal MQTT with Micropython



I have been meaning to play around with MQTT for some time now, and finally decided to take the plunge one evening last week. I had three cheap home temperature and humidity sensors, and was bothered that they often didn’t agree. Surprisingly, while the analog one had a calibration adjustment in the back, I have no idea how to calibrate the two digital ones. I took this as a sign that it was time to learn MQTT and be able to install my own, accurate sensors. Of course, I began by ordering the cheapest sensors I could find, but I can always upgrade later on.
Three Cheap Sensors While we have written quite a bit about MQTT in Hackaday, I had to go all the way back to 2016 to find this introductory four-part series by Elliot Williams. Five years is a long time in the tech world, but I decided to give it a try anyway.
Building a Broker
The first article worked perfectly, although instead of a Raspberry Pi, I used an old desktop that my wife was about to throw away. After wiping out Windows, doubling the RAM, and installing Debian, I had a new lab machine up and running. I installed the mosquitto packages from the standard repositories, and used them without issues to follow along with this article (I briefly tested on an Ubuntu and Mac machine, too). Installation is as easy as:

Debian and Ubuntu

sudo apt install mosquitto
sudo apt install mosquitto-clients

MacOS

brew install mosquitto

Networked Nodes
The trouble began with the second article . Elliot used an ESP-8266 module and NodeMCU. I had been wanting to give NodeMCU a try, so I plunged ahead. While I didn’t have any 8266 modules on-hand, I did have an ESP32 DevKitC module. In the past I brought up these and similar modules running GRBL, Micropython, and bare metal — “How difficult could this be?”, I thought to myself.
Well, in fact it proved to be quite difficult. I proceeded to build NodeMCU custom images online, was able to make the esptool.py talk to and program my board. But try as I might, and I tried for hours, I could not get my board to boot up without errors. The board programmed and hashed correctly, but always gave errors on boot. I read many similar reports from users online, so at least I am not alone in my problem in my frustration.
I reached out to a couple of professional programmers, and the advice I got was to move on. They suggested that NodeMCU is outdated and weren’t surprised I was having difficulties. I am confident that I could have eventually made NodeMCU to work — evidence points to a hardware bootup issue (I/O pin or setting), the online image was being loaded at the incorrect address, or it was just plain wrong. But after so many wasted hours, I wanted to experiment with MQTT, not become a NodeMCU expert.
Having used Micropython before, and seeing there was an MQTT module I could just import, I decided to take this approach....

Top