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.

Equipment

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.

Pico W with BME280, connected via a breadboard and jumpers

A rather dusty Pico W with BME280 attached

A Pico W with DHT-22 soldered directly to the board

A Pico W with DHT-22 soldered directly to the board

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.

[SSID]
[PASSWORD]

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. cd to the directory in the Terminal, then code .)
  • 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.

You’re done

And that’s it for now!

JSON data returned via the /data route. Pressure is null because it is not supported by this sensor - the dht22

JSON data returned via the /data route. Pressure is null because it is not supported by this sensor - the dht22

What you actually do with this data is another matter… this solution doesn’t do any logging. I cover that in a later post.