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:

    1. Make sure the WiPy is connected over serial to Pymakr ( sometimes restarting Pymakr gets things going if it can't detect it).

    2. Download the UMQTT Library that is available here: and open

    The library on the whole is pretty good, but there is a bug on line 57. Make sure to change:




    Go ahead and save the Library but rename it so umqtt instead of

    1. 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): 
    wlan = WLAN(mode=WLAN.STA)
    wlan.connect("yourwifinetwork", auth=(WLAN.WPA2, "wifipassword"), timeout=5000)
    while not wlan.isconnected(): 
    print("Connected to Wifi\n")
    client = MQTTClient("joe", "", port=1883)
    client.settimeout = settimeout
    while True:
         print("Sending ON")
         client.publish("/lights", "ON")
         print("Sending OFF")
         client.publish("/lights", "OFF")

    After inputting your WIFI and broker details, go ahead and choose a publish topic of your choice, along with a message

    1. 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.

    2. 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.

    alt text

    Go back to and subscribe to your topic, and you should see the messages start coming in on the broker.

    alt text

    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
    but you should into

  • @livius
    Hi, thanks for reply!
    It's Pymakr Atom Plugin I use.
    I've copy 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.

  • administrators

    @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'?

  • administrators

    Hi @pwest,

    It might be work checking out, 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!



  • I'm also using the same apparently current 1.3.2 file, and having the exact issue described by lcgeldenhuys 3 months ago. Any suggestions on how to get past this?


  • 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 "", line 10, in <module>
    File "/flash/lib/", line 74, in connect
    OSError: [Errno 113] EHOSTUNREACH


    client = MQTTClient("MQTT_lcg_Client", "", port=1883)
    client.settimeout = settimeout

    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 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
    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:

    And the changes done to line number 57 "self.sock.settimeout(self.timeout)"

    Thanks & Regards,
    Satish Kumar. R.

  • @sakis Thanks!

    I have downloaded from and placed it in /flash/ 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 (

    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.


  • Pybytes Beta

    @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 ?

Log in to reply

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.