Simple MQTT Tutorial
-
Hi All,
Figured this code may help those relatively new to Pycom/Micropython/MQTT. It's a simple way to quickly get Mqtt publishing on your WiPy or LoPy board. I used the Hive Mq broker for demo purposes:
http://www.hivemq.com/demos/websocket-client/
-
Make sure the WiPy is connected over serial to Pymakr ( sometimes restarting Pymakr gets things going if it can't detect it).
-
Download the UMQTT Library that is available here: https://pypi.python.org/pypi/micropython-umqtt.simple and open simply.py
The library on the whole is pretty good, but there is a bug on line 57. Make sure to change:
self.settimeout(self.timeout)
To
self.sock.settimeout(self.timeout)
Go ahead and save the Library but rename it so umqtt instead of simple.py
- Open Pymakr to start building your main script. Create a new project (Mqtttest or whatever)
from network import WLAN from umqtt import MQTTClient import machine import time def settimeout(duration): pass wlan = WLAN(mode=WLAN.STA) wlan.antenna(WLAN.EXT_ANT) wlan.connect("yourwifinetwork", auth=(WLAN.WPA2, "wifipassword"), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi\n") client = MQTTClient("joe", "broker.hivemq.com", port=1883) client.settimeout = settimeout client.connect() while True: print("Sending ON") client.publish("/lights", "ON") time.sleep(1) print("Sending OFF") client.publish("/lights", "OFF") time.sleep(1)
After inputting your WIFI and broker details, go ahead and choose a publish topic of your choice, along with a message
-
Almost ready to give it a try! Next connect to your WiPy over WIFI and FTP into the /lib folder. I use Fillezilla for this. Upload our renamed library umqtt to the /lib folder on the board.
-
Make sure the WiPy is connected over serial to Pymakr ( again a restart may help this). Then hit the play button to upload your script to the board . You should see the Wifi connected printed in the console, and then the while loop commence.
Go back to http://www.hivemq.com/demos/websocket-client/ and subscribe to your topic, and you should see the messages start coming in on the broker.
Bit of a dummies guide but hope this helps some people!
-
-
@achide
I do not use pymakr
but if i understand screen correctly you put it into
\flash\lib\connections
but you should into
\flash\lib
-
@livius
Hi, thanks for reply!
It's Pymakr Atom Plugin I use.
I've copy umqtt.py under lib file. Like this :
Then when I run the code, it shows like that :
What should I do ?
-
@achide
Pymakr IDE or Pymakr Atom Plugin?
If First then do not use it.
-
@bucknall yes, I use Pymakr.
-
@achide How are you uploading code to your device? Are you using the Pymakr Plugin?
-
Hi,
I'm new with pycom. Maybe it's a stupid question, I would like to know how to import umqtt library.
I've put it in lib folder of sys.path, but when I import this module, it told me no module named 'umqtt'?
-
Hi @pwest,
It might be work checking out https://youtu.be/3NDPSbr5J14?list=PLS09TA19umCOavPeP_FoMHoCuCzjJUWQk, where I talk about MQTT and how to get set up with it.
Currently, the documentation on MQTT that we have online is out of date and I am re-writing it as we speak. I'll post back once it has been published!
Thanks,
Alex
-
I'm also using the same apparently current 1.3.2 simple.py file, and having the exact issue described by lcgeldenhuys 3 months ago. Any suggestions on how to get past this?
-Phil
-
What version of the micropython-umqtt.simple library are you using? I'm trying the latest 1.3.2. It doesn't have the same bug on line 57 anymore, in fact it doesn't seem to use the same timeout mechanism.
I'm not able to connect successfully to the HiveMQ public broker:
Traceback (most recent call last):
File "main.py", line 10, in <module>
File "/flash/lib/umqtt.py", line 74, in connect
OSError: [Errno 113] EHOSTUNREACHUsing:
client = MQTTClient("MQTT_lcg_Client", "broker.hivemq.com", port=1883)
client.settimeout = settimeout
client.connect()I can pub / sub using the MQTT.fx client on my laptop, and the WiPy is on the same network (I can ping and ftp to WiPy from laptop).
Any ideas?
-
@MrPy Thanks!
Adjusting the timeout period in simple.py is helping to make the setup work consistent.
This might help.
Thanks & Regards,
Satish Kumar. R.
-
@RSK , Pretty sure you have a port issue. 1883 should work. For some reason Hive MQ isn't accepting requests on 1883 currently. I will search for another public test broker..
-
I am trying in Windows, by default http://www.hivemq.com/demos/websocket-client/
give port number 8000.If i try to change in the browser to 1883 then socket connection fails in websocket client.
-
@RSK Have you tried using port 1883 instead of 8000?
-
Sorry the link i provided is incorrect, downloaded from the below link:
https://pypi.python.org/pypi/micropython-umqtt.simpleAnd the changes done to line number 57 "self.sock.settimeout(self.timeout)"
Thanks & Regards,
Satish Kumar. R.
-
@sakis Thanks!
I have downloaded umqtt.simple.py from https://github.com/micropython/micropython-lib and placed it in /flash/simple.py of the LoPy using FTP.
And trying to connect from my LoPy application:
With Hive settings:
But connect fails with error "bytes index out of range":
Please help to resolve.
Thanks & Regards,
Satish Kumar. R.
-
@RSK
umqtt
is not part of Pycom's firmware or standard micropython. It is an external Python library, so to be able to use it you need to upload it to your device.The easiest way to do that is using Pymakr. Another way is using plain FTP (https://docs.pycom.io/pycom_esp32/pycom_esp32/general.html#local-file-system-and-ftp-access).
Please read step 2 again and try to follow the instructions. If you have any problems let us know.
-
MQTTClient is missing in the latest firmware, using the image release on 02/Dec/2016.
Could someone confirm this.
-
@gas It's working now. The used port was wrong. Thxs for your help.
-
@vruizvil Hi
I started a mosquitto on my laptop (vm debian) and it is working with the code as it is i updated it a little to make it configurable via config param file (wifi and mqtt)
what error you are getting ?