Now that I have a Circuit Python library for the ATM90e26, I need an mqtt library. I didn’t find one within Circuit Python, but I did find Micropython’s umqtt.simple library. How hard can it be to use the umqtt.simple on the Huzzah to send mqtt messages from a Circuit Python App?
The goal of this post is to document the steps I took to send an mqtt message from a Feather Huzzah to a Raspberry Pi.
- umqtt_simple.py – modifications I made to the simple mqtt micropython library.
- mqttTest.py – test app I ran on the Feather.
Get umqtt.simple Library Up-n-Running
The umqtt.simple library is located here. I copied the contents to my local folder as umqtt_simple.py on my Mac for editing with Atom.
MQTT address and port
The DNS name of my Raspberry Pi is ‘raspberrypi.home’ …I found this by looking at the IP addresses in Angry IP.
I had installed the Mosquitto broker on the Raspberry Pi. To better understand traffic, I started mosquitto in the verbose mode.
- Stop the currently running instance of Mosquitto: sudo killall mosquito
- Restart in verbose mode: mosquito -v
1509813288: Opening ipv4 listen socket on port 1883.
1509813288: Opening ipv6 listen socket on port 1883.
ooh! Looky looky…port 1883!…I guess this means mqtt connections come in as SSL….
but OOPS….the code in simple.py
Isn’t right…so I replaced it with:
if port == 0: ####!!! BUG IN SIMPLE.PY
port = 1883
port = 8883
Connecting Huzzah to WAN
I seemed to only have to do this once…I would think I should try connecting every time I start an mqtt conversation…I thought this python code was a “nice and robust” way to include connecting to the wifi.
Anywho, I assume you know how to connect to a wireless network with micropython/circuitpython…
I started up the mqtt client on the Raspberry Pi:
$ mosquitto_sub -t test/test1 -d
from umqtt_simple import MQTTClient
c = MQTTClient(‘mqtt_client’,’raspberrypi.home’,ssl=True)
Fix What Didn’t Work
UPDATE (11/5/2017): uncommented…instead, access umqtt_simple with:
c = MQTTClient(‘mqtt_client’,’raspberrypi.home’,1883)
(Include the port number in the call. I don’t understand if the Raspberry Pi is using 1883 and NOT assuming SSL?…)
I also had to comment out these lines from umqtt_simple.py:
# if self.ssl:
# import ussl
# self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
Client mosqsub/1328-raspberryp received PUBLISH (d0, q0, r0, m0, ‘test/test1’, … (5 bytes))
Log lines from mosquitto -v:
1509818043: Received PUBLISH from mqtt_client (d0, q0, r0, m0, ‘test/test1’, … (5 bytes))
1509818043: Sending PUBLISH to mosqsub/1328-raspberryp (d0, q0, r0, m0, ‘test/test1’, … (5 bytes))
Seems to be working. Thanks for reading this far….please find many things to smile about.