メニュー
In this guide, I will demonstrate how to create an IoT project with the Raspberry Pi Pico W, utilizing MicroPython and MQTT. By the end of this detailed tutorial, you'll be equipped to set up a system that collects sensor data, sends it to the cloud, and displays the information on a dashboard.
Let's start by examining the Raspberry Pi Pico W microcontroller board in detail. The Pico W belongs to a series of microcontroller boards from Raspberry Pi, which also features the Pi Pico, Pi Pico H, and Pi Pico WH.
The key distinction of the Pi Pico W compared to the other Pico boards is its integrated WiFi capability, indicated by the 'W.' This feature makes it ideal for developing Internet of Things (IoT) applications, as I will showcase in this article.
To create an IoT project with the Raspberry Pi Pico W, the first step is to choose a sensor to measure physical parameters like temperature.
For this project, we will use the BME280, an environmental sensor designed for indoor monitoring of temperature, pressure, and humidity. It features an I2C interface for accessing the data it generates. I2C is a serial communication protocol that utilizes two lines for data exchange: the Serial Clock Line (SCL) and the Serial Data Line (SDA).
Given that our Raspberry Pi Pico W has two I2C interfaces, I will connect the BME280 sensor to one of them.
When you purchase the Raspberry Pi Pico W, you have the option to buy it with the pin header already soldered or to buy the pin header separately and solder it yourself. I opted to get mine without the pins and soldered them onto the microcontroller board that we will be using for this demonstration.
Once I soldered the pins, I positioned my Raspberry Pi Pico W on a breadboard and used jumper wires to connect it to the BME280 sensor.
Now, let's take a look at the circuit diagram to see the specific connections.
The BME280 sensor is connected via the I2C interface, with the Serial Data Line (SDA) and Serial Clock Line (SCL) linked to pins 1 and 2 of the Raspberry Pi Pico W, respectively.
Additionally, the ground pin of the BME280 is connected to pin 38 of the Pico W, which serves as the ground. The VCC of the BME280 sensor is linked to pin 36 of the Raspberry Pi, supplying a 3.3 Volts output.
Although you can program the Raspberry Pi Pico W with languages like C and C++, MicroPython is the most straightforward option. MicroPython fully implements the Python 3 programming language and operates directly on embedded hardware like the Raspberry Pi Pico.
To start programming the Pico W with MicroPython, you'll first need to install MicroPython on the device. This involves connecting the Raspberry Pi Pico W to your computer and dragging a UF2 bootloader file onto it.
Let's walk through the process step-by-step.
First, you'll need a Micro USB cable to connect the Pico W to your computer via USB.
Next, press and hold the BOOTSEL button on the microcontroller board while you connect your Pico W to the USB port of your computer. Once the Pico W is connected, release the BOOTSEL button.
You should see a Mass Storage Device named RPI-RP2 appear on your computer. At this stage, you'll need to obtain the UF2 bootloader file to install MicroPython on your Pico W. You can find this file by visiting the documentation section on the Raspberry Pi website, where you'll find detailed instructions and a link to download the UF2 file.
Instructions for downloading the Raspberry Pi Pico W from the Raspberry Pi documentation:
I will click to download the version of the file for the Raspberry Pi Pico W.
After that, I'll copy the bootloader file into the RPI-RP2 storage that appeared when I connected my Pico W to the computer.
The Pico will automatically recognize the file and reboot, after which it will be ready for programming.
To program the Raspberry Pi Pico W with MicroPython, we need an integrated development environment (IDE) that lets us connect to the Pico W from our computer. The most user-friendly IDE for this purpose is Thonny. You can download Thonny IDE by visiting its website and selecting the version compatible with your computer's operating system.
I'll download Thonny for Windows and proceed with the installation. Once it's installed, I'll open Thonny, then go to the bottom right corner to choose the destination for my program deployment.
With the Raspberry Pi Pico W connected, it should show up in the list. I'll select it.
At this point, you should see the Thonny terminal showing that you are now connected to the Pico W. To verify that our Raspberry Pi Pico is functioning correctly, we can run a simple 'hello world' command by executing.
With my development environment prepared, I can start coding for my IoT project.
Before writing the code, I need to save the program file to the Raspberry Pi Pico W. To do this, I'll navigate to File and click on Save. Then, in the 'Where to save file?' prompt, I'll choose Raspberry Pi Pico.
To ensure that your program runs automatically each time the Raspberry Pi boots up, you need to name your file main.py, which I'll do now.
Now that I have saved my program file on the Pico W, I can start writing the code.
Since I’ll be reading data from the BME280 sensor and publishing it to the cloud using MQTT, I need to install the relevant MicroPython libraries on the Pico. I’ll go to Tools and select Manage Packages.
In the Manage Packages search bar, I'll enter bme280 and click search.
Next, I'll select the MicroPython-bme280 search result and click on install.
After that, to install the MQTT Client library, I'll open the terminal and connect my Raspberry Pi Pico W to WiFi using the following set of commands.
Next, I will install MicroPython upip.
I'll use pip in the terminal to install an MQTT library named micropython-umqtt.simple. However, I first need to install micropython-umqtt.robust, as demonstrated below.
Next, I will move on to the micropython-umqtt.simple library.
It's worth noting that installing the MQTT library can sometimes be tricky, as I have encountered. If you face any issues, here are some troubleshooting tips.
First, ensure you are using the latest MicroPython firmware.
If that doesn't resolve the issue, use MicroPython to navigate to your Pico W file directory and delete a folder named lib.
Now that the necessary library packages for the IoT project are installed, we can move forward.
Let's review the code I've assembled.
First, I import the machine library to access the Raspberry Pi's pins and facilitate I2C communication.
Next, I import the network library to connect to my WiFi, utime for implementing time delays, the bme280 library, and finally the MQTT library.
Then I initialise my WiFi connection details.
Next, I activate my WiFi connectivity on the Raspberry Pi and connect to my WiFi network. After that, I check to see the status of my WiFi connectivity.
Then I initialise my Raspberry Pi’s I2C interface
Next, I define a method to connect to my MQTT broker. I'm using a HiveMQ Cloud MQTT broker that I’ve already set up, and I'll explain how to do that shortly.
First, let's quickly review the connection details. It's important to note that these connection details are specific to a HiveMQ Cloud MQTT broker.
For the server, you'll enter the MQTT broker URL. For the port number, you can use 0, even though my broker operates on port 8883. Next, I include the MQTT client username and password that I configured on my HiveMQ Cloud portal.
The Keep Alive setting is set to 7200, and I've enabled SSL. Finally, under ssl_params, I specify the server hostname of my MQTT broker. Again, these settings apply to a HiveMQ Cloud broker, so if you use a different broker, your settings may vary.
Then I connect to my MQTT Broker.
Before we continue with the code, let me explain how I set up the MQTT broker that my code connects to. Visit the HiveMQ website, navigate to Cloud, and sign up for a free HiveMQ Cloud account, which allows you to connect up to a hundred MQTT clients. You can sign up for HiveMQ Cloud directly here. I'll log into my HiveMQ Cloud portal. Once you sign up, HiveMQ automatically creates the broker cluster for you. You can copy its URL by going to Manage Cluster.
Next, you'll need to set the username and password for connecting to your broker under Access Management.
Next, I've implemented a method for publishing messages to my MQTT broker, which displays 'publish done' when the message is successfully sent.
Finally, I have my main loop set up to execute the core logic. I read the sensor data from the BME280 sensor and assign the values to the temperature, pressure, and humidity variables.
After printing the sensor readings, I use the publish method to send the data as MQTT messages under the following topics:
picow/temperature for temperature data
picow/pressure for pressure data
picow/humidity for humidity data
I then introduce a delay of five seconds before reading and publishing the data again.
Now that my code is ready, I can deploy and run it on my Raspberry Pi Pico W. To do this, I'll go to the Menu and click on Run, then select 'Run current script.
Deploying and executing the code to read sensor data
As illustrated below, my code is now running and publishing the BME280 sensor data to my MQTT broker.
Running and publishing the bme280 sensor data to the MQTT broker