As I work on building my farm, one thing I'm aiming to do is collect and analyze data in order to better understand what's happening and how to improve my operation. I figure building custom devices using raspberry pis are a good way to go about this, because doing so will allow me to begin collecting data as well as automating various tasks.
One simple way to get started on a large scale project like that is to begin with a simple thermometer to track the temperature and humidity in my germination room. To build the thermometer, I purchased:
To get started, we need to build the circuit that allows the Pico to interact with the BME280. We'll be utilizing 4 pins on each device:
BME280
Pico
VIN
3v3
GND
GND
SCK
GP1
SD1
GP0
Install the Firmware
Next, we can install the CircuitPython firmware onto the Pico and utilize some pre-built Adafruit libraries to help us utilize the BME280.
Since we're using a Pico W, we can download the firmware here, and the version 8 libraries here.
To install the firmware, simply drag and drop the uf2 file onto the Pico after connecting it to the computer. Then copy the "adafruit_bme280" directory and "adafruit_requests.mpy" file into the pre-created "lib" directory on the Pico.
We'll also create a secrets.py and thp_sensor.py to hold our code for the project. At the end, the directory structure should look something like this:
💡
You can utilize an IDE like Thonny to connect to the Pico and start coding
Adding the Logic
To keep things separated, all of the actual sensor logic will live in thp_sensor.py while secrets such as the wireless SSID & password will live in secrets.py. Code.py is essentially the "main" file that runs when powering the system on.
Let's start with the sensor logic:
Here, we're creating a sensor class that contains the core logic for interacting with the BME280 and allows us to read the temperature, humidity, pressure, and altitude. Later, we'll add the ability to generate telemetry that can be sent elsewhere.
Now lets update our secrets file:
SSID and PASSWORD will be used to connect to the wireless, while TOKEN is needed to post to the API endpoint that handles our device telemetry
Finally, let's update code.py to test everything so far:
Making it Useful
Currently, our thermometer turns on, connects to the wireless, and captures the current temperature, humidity, and pressure. Now we'll make it more useful by continually capturing that data and sending it to a server where it can be processed and stored.
Let's start with generating the telemetry:
💡
You'll notice that for the timestamp, I'm adding the offset to create a UTC timestamp as opposed to using local time. This will store the UTC value in the database, and the web client adjusts for that when creating Date objects or using dayjs.
Now code.py can be updated to utilize the new ThpSensor functions and post the telemetry to an API that saves it to a database
With these changes, the thermometer will continually send the temperature and humidity to our API once per hour.
Of course, building the API endpoint and actually handling the requests from our thermometer requires additional work. That is outside the scope of this post though, and I plan to go into more detail on that later.