HiveMQ Cloud, part 4 - Sending sensor data from Raspberry Pi Pico W to HiveMQ Cloud

Exactly one year ago, in December 2021, I published three articles of MQTT messaging with Raspberry Pi, Raspberry Pi Pico and HiveMQ Cloud. On June 30th of 2022, Raspberry Pi released a new product, that is the subject of this post: the Pico W. Yes, a new version of the original Pico, but with Wi-Fi. The new board is for sale for 6$, compared to the 4$ of the original Pico.

In this post, we will rework the third project of the posts from December last year, where data was sent to HiveMQ Cloud using a Pico, separate Wi-Fi module and distance sensor. We can simplify that project now by using the new Pico W, removing the need for that separate Wi-Fi module.

The sources of this project and the previous three parts are available on GitHub.

About HiveMQ Cloud

HiveMQ Cloud is an online MQTT-compatible service which is totally free up to 100 devices! Even for the most enthusiastic maker, that’s a lot of microcontrollers or computers!

On the HiveMQ Blog an article by Kudzai Manditereza was published that also describes how to use the Pico W.

About the Raspberry Pi Pico W

The new Pico W has exactly the same form-factor as the original Pico. There is a minor change in the wiring as the connection of the on-board LED has changed. You should be able to swap to the new version in most existing projects, without any problems.


Breadboard setup

Take a breadboard and some wires to create this small test setup. Compared to the previous post with the Pico and an additional Adafruit AirLift Wi-Fi, the setup is now a lot simpler.

The only component to be connected is the distance sensor.

Distance sensor

Only 4 wires are needed, for simplicity connected to the same pins as used in the previous post, but of course you are free to use other ones, as long as you use the correct numbers in your code.

Pico HC-SR04

MicroPython project

In the previous project with the Pico, CircuitPython was used to code the program. Unfortunately, when I started with this project to connect Pico W to HiveMQ Cloud, CircuitPython was not yet available with support for the WiFi module of the Pico W. That ticket on GitHub now seems to be closed and resolved, so I leave it up to you to try out. In this post we will be using MicroPython.

“MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments.”.

To install MicroPython on the Pico W, follow this step-by-step provided by Raspberry Pi.

Setting up a secure connection - as required by HiveMQ Cloud - turned out to be an additional challenge. In the end, as most of the times, it was just a matter of finding the right library and settings. Luckily I got the support of a few members of the HiveMQ forum as you can see in this thread.

Warning! If you are using an Apple computer with macOS Ventura, there could be a problem to connect to the Pico. See this blog post for more info.

Install Thonny IDE on Mac

I tried several IDEs to program the Pico W, but found out that Thonny is the best option. It is available for Windows, Mac, and Linux. On macOS you can easily install with brew install --cask thonny. Once installed, make sure to open “View > Files”, and select your board “MicroPython (RP2040)” in the lower right corner of the screen as you can see in the screenshot.

Install the libraries

Our application will use two libraries to minimize the code we need to write ourselves.

The first one gives us an MQTTClient. Copy the code from the file you can find on GitHub in the MicroPython project, create a new file in Thonny and save it to the Pico W in the directory /lib/umqtt/ as file

Another approach to install a library, provided by MicroPython, is using the terminal in Thonny and run these two commands:

>>> import upip
>>> upip.install('umqtt.simple')
Installing to: /lib/
Warning: SSL certificate is not validated
Installing umqtt.simple 1.3.4 from

The second library helps us with the distance measurements with the HC-SR04 sensor. Again, copy this code from GitHub from a project by rsc1975 (Roberto), create a new file in Thonny and save it to the Pico W in the directory /lib/hcsr04/ as file


Create a file in the root of the Pico W called with the following content (this is the same file as used in the previous post with the Pico).

secrets = {
   'ssid' : 'WIFI_NETWORK_NAME',
   'password' : 'WIFI_PASSWORD',
   'timezone' : 'Europe/Brussels',
   'mqtt_username' : 'HIVEMQ_USERNAME',
   'mqtt_key' : 'HIVEMQ_PASSWORD',
   'broker' : '',
   'port' : 8883

Project code

The final part is the actual application code. Create one extra file in the root of the Pico W called with the following content:

import time
import secrets
import network
import ntptime
import ussl

from machine import Pin
from umqtt.simple import MQTTClient
from hcsr04.hcsr04 import HCSR04
from time import sleep

# Blink the onboard LED to show startup
led = Pin("LED", Pin.OUT)

# Load the WiFi and HiveMQ Cloud credentials from
    from secrets import secrets
except ImportError:
    print("Error, secrets could not be read")

# Connect to WiFi
# Based on
print('Connecting to AP: ' + secrets["ssid"])
wlan = network.WLAN(network.STA_IF)
wlan.connect(secrets["ssid"], secrets["password"])

# Wait for WiFi connection or failure
connected = False
attempt = 0
while not connected and attempt < 10:
    attempt += 1
    if wlan.status() < 0 or wlan.status() >= 3:
        connected = True
    if not connected:
        print("Connection attempt failed: " + str(attempt))
        print("Connected on attempt: " + str(attempt))
if not connected or wlan.ifconfig()[0] == "":
    # Blink LED to show there is a WiFi problem
    print("Bad WiFi connection: " + wlan.ifconfig()[0])
    while True:
        # Endless loop as we don't have a WiFi connection

# As we end up here, we now we have a WiFi connection
print("WiFi status: " + str(wlan.ifconfig()))

# To validate certificates, a valid time is required
# NTP is used to get the correct time
print('Connecting to NTP') = ""
print('Current time: ' + str(time.localtime()))

# Load the certificate for secure connection to HiveMQ Cloud
print('Loading CA Certificate')
with open("/certs/hivemq-com-chain_2_only.der", 'rb') as f:
    cacert =
print('Obtained CA Certificate')

# Connect to HiveMQ Cloud
# Based on
print("Connecting to " + secrets["broker"] + " as user " + secrets["mqtt_username"])

# Use sslparams as defined below for a secure connection
# sslparams = {'server_side': False,
#             'key': None,
#             'cert': None,
#             'cert_reqs': ussl.CERT_REQUIRED,
#             'cadata': cacert,
#             'server_hostname': secrets["broker"]}

# When using the sslparams below, a connection can be made to HiveMQ Cloud, but it's not secure
sslparams = {'server_hostname': secrets["broker"]}

mqtt_client = MQTTClient(client_id="picow",
print('Connected to MQTT Broker: ' + secrets["broker"])

# Send a test message to HiveMQ
mqtt_client.publish('test', 'HelloWorld')

# Continuously measure the distance and send the value to HiveMQ
# Based on
hcsr04 = HCSR04(trigger_pin=17, echo_pin=16, echo_timeout_us=10000)
print("Starting the distance measurement")
killed = False
while not killed:
    # Measure distance
    distance = 0
        distance = hcsr04.distance_cm()
    except Exception as e:
        print("Distance measurement failure\n", e)
    # Send to HiveMQ Cloud
        json = "{\"value\": " + str(distance) + "}"
        print("\tMessage for queue: " + json)
        mqtt_client.publish("picow/distance", json)
    except Exception as e:
        print("\tMQTT publish Failed, retrying\n", e)
        killed = True

    # Sleep a second

When executing the above code with the “Run current script” button in Thonny, you should get the following output that shows the device connected to Wi-Fi and started sending data.

Connecting to AP: ***
Connected on attempt: 1
WiFi status: ('', '', '', '')
Connecting to NTP
Current time: (2022, 9, 29, 16, 2, 20, 3, 272)
Loading CA Certificate
Obtained CA Certificate
Connecting to *** as user picow-user
Connected to MQTT Broker: ***
Starting the distance measurement
	Message for queue: {"value": 108.6254}
	Message for queue: {"value": 107.732}
	Message for queue: {"value": 14.10653}
	Message for queue: {"value": 16.95876}


HiveMQ Cloud requires a TLS (secure) connection which is a bit challenging for this kind of small boards. But still, a connection can be set up easily and from then on the possibilities are endless, even for a small board of 6$.