How to Send and Receive Data Using Raspberry Pi Pico W and MQTT (Updated)

How to Send and Receive Data Using Raspberry Pi Pico W and MQTT
(Image credit: Tom's Hardware)

Update 10/24 03:32 PT

We have updated this how to as originally we installed umqtt.simple via the upip package manager. It seems that at this time the package cannot be installed in this manner. We have updated the installation so that we use Thonny instead. 

Latest Videos FromTom's Hardware
Les Pounder
Associate Editor

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • GKVicto
    For the last subscribe loop, aren't you just subscribing over and over? Don't you only want to do that once? Shouldn't you be calling client.wait_msg() instead?
    Reply
  • FrankDelporte
    Hi, I'm following this guide trying to connect to HiveMQ Cloud. But to be able to do this, in MQTTClient(...) the username, password, and SSL are required. Were you able to achieve this, or did you connect to a local HiveMQ where this is not required?
    Reply
  • rnlg
    Looks like you need to use mip instead of upip for package management in MicroPython now.
    Reply
  • markjoe
    GKVicto said:
    For the last subscribe loop, aren't you just subscribing over and over? Don't you only want to do that once? Shouldn't you be calling client.wait_msg() instead?

    I agree, the subscription before the loop and then wait_msg or check_msg. The only thing then is to ping the broker once in a while (if you set a keep alive interval)
    Reply
  • fasani
    Hi there,

    I'm facing the following problem using a Pico W with firmware v1.19.1 (2022-06-18) .uf2 (latest stable version).
    Eeach time I try to install the module, I'm facing a error message (AssertionError) that seems to be related to the fact that I can't download the package.

    ...
    AssertionError
    ----------------------------------------
    WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
    ----------------------------------------
    Exception occurred during processing of request from ('127.0.0.1', 50406)
    Traceback (most recent call last):
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
    AssertionError
    ----------------------------------------
    ERROR: Could not install packages due to an OSError: HTTPConnectionPool(host='127.0.0.1', port=36628): Max retries exceeded with url: /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/ (Caused by ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')))

    Error Command '' returned non-zero exit status 1.
    I've tried to install related packages (simple2, robust, robust2) but I'm still facing the same problem...

    Does anybody has an idea on how to deal with this problem ?

    Regards,
    David
    Reply
  • markjoe
    fasani said:
    Hi there,

    I'm facing the following problem using a Pico W with firmware v1.19.1 (2022-06-18) .uf2 (latest stable version).
    Eeach time I try to install the module, I'm facing a error message (AssertionError) that seems to be related to the fact that I can't download the package.

    ...
    AssertionError
    ----------------------------------------
    WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
    ----------------------------------------
    Exception occurred during processing of request from ('127.0.0.1', 50406)
    Traceback (most recent call last):
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
    File "C:\Users\david\AppData\Local\Programs\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
    AssertionError
    ----------------------------------------
    ERROR: Could not install packages due to an OSError: HTTPConnectionPool(host='127.0.0.1', port=36628): Max retries exceeded with url: /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/ (Caused by ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')))

    Error Command '' returned non-zero exit status 1.
    I've tried to install related packages (simple2, robust, robust2) but I'm still facing the same problem...

    Does anybody has an idea on how to deal with this problem ?

    Regards,
    David

    umqtt.simple is literally one file. Download and save on the pico and you are good to go.
    Reply
  • KenzW
    I tried to install umqtt.simple with an ubuntu laptop and with a raspberry pi 4, for use with Thonny.
    Both attempts failed for different reasons.
    The following steps worked for me:
    Visit https://pypi.org/project/micropython-umqtt.simple/#filesDownload micropython-umqtt.simple-1.3.4.tar.gz
    Extract simple.py from micropython-umqtt.simple-1.3.4/umqtt in the tar.gz file
    Copy simple.py to your Thonny project, and save it to your pico w board
    Use the following import in your mqtt-pub.py and mqtt-sub.py files: from simple import MQTTClientI hope this might be useful to other readers.
    Reply
  • FrankDelporte
    I also got it working and described it here:
    https://webtechie.be/post/2022-12-07-sending-sensor-data-from-raspberry-pi-pico-w-to-hivemq-cloud/
    Reply