In this previous post, I gave an overview of how I collect ambient data using a Raspberry Pi and a couple of Raspberry Pico Ws. Now I’m going to go into more detail on how to set up the Pico to take readings on demand via the network.
If you want to try this at home, you’ll need:
- A Raspberry Pico W with MicroPython installed
- A soldering iron and paraphernalia (or another way of connecting the sensor to the Pico W such as breadboard and jumpers)
- A DHT-22 or BME280 sensor
- A wifi network
Attaching a sensor to your Pico W
The MicroPython code I wrote has support for two sensors: the DHT-22 or the BME280. The DHT-22 measures temperature and humidity, while the BME280 additionally measures air pressure. Currently, my code only supports these sensors and you can only read from one at a time.
There are plenty of guides to wiring up these sensors, so I suggest you consult one of them and then come back here. For example:
Important: You’ll need to install MicroPython as laid out in the guides. For the BME280, you’ll have to install the library, also described in the guide. For the DHT-22, it’s preinstalled when you install MicroPython. Actually accessing the sensors is taken care of by the code below so you can ignore the descriptions in the guides.
Installing the code
Get the code from GitHub
Grab the repository from GitHub. You need to include the
--recursive flag so it pulls in Pimoroni’s Phew server too.
git clone https://github.com/johnheaven/pico-ambient-data-api.git --recursive
Add a wifi.txt file with wifi details
In the root of the project, add a file called
wifi.txt with your SSID on one line, and the password on the next.
The first time you run the software, your Pico will connect to wifi with these details.
Getting the code onto your Pico
Now you’re ready to upload the code to your Pico. I use VS Code for this:
- Open the base directory for your project in VS Code (e.g.
cdto the directory in the Terminal, then
- If you don’t already have it installed, install the Pico-W-Go extension
- On the blue bar at the bottom of the screen, click ‘All commands’ then select ‘Upload project to Pico’
You can then hard-reset your Pico.
Find the IP address and test the API
If everything works (which it never does first time…) then your Pico should restart and connect to the network. (If it doesn’t, you can just disconnect it and reconnect it). After a while, it will show the IP address it’s connected to in the REPL in the window at the bottom of the VS Code workspace.
Enter the IP address in a browser. Now you should see a page where readings from your thermometer should be. There probably won’t be any readings now though, as you’ve not set up your sensor.
Try adding the IP address followed by
/data. After a few seconds, you should get a JSON object. Again, you won’t have data yet because it isn’t setup.
Setting up for first time
Now you’re connected to wifi, you can set up your sensor. In the menu, you can click on ‘Settings’ and change a few settings such as wifi credentials and the sensor you’re using as well as the SDA/SCL pin it’s connected to.
If you change the wifi settings, you’ll have to reset after saving. This should be a one-off process, before you place your sensor wherever you want to collect data.
And that’s it for now!
What you actually do with this data is another matter… this solution doesn’t do any logging. I cover that in a later post.