One of the main things that I want to accomplish as I build out my farm is to collect and analyze data so that I'm able to better understand what's happening and how to improve my operation.
There are a number of different pre-built and turn-key solutions to accomplish this but, most of the time, those solutions are like black boxes that you don't have control over and your data is stored on someone else's servers.
To get around this, I've decided to utilize Raspberry Pis to build my own devices that can easily be repaired or customized while allowing me to own all of my data.
A Simple Thermometer
I believe the easiest way to get started with a project like this is to start with a simple thermometer that will allow me to keep track of temperature, humidity, and barometric pressure in a germination room. To build the prototype, I purchased:
- 1 - Raspberry Pi Pico W ($7.00)
- 1 - BME280 Temp/Humidity/Pressure Sensor ($15.00)
- 1 - USB Cable ($3.00)
In my case, the device will be indoors near a power outlet, but you could also swap out the USB cable for solar to eliminate the need for a power outlet:
- 1 - Solar Charger ($15.00)
- 1 - 6V 2W Solar Panel ($21.00)
- 1 - 6600 mAh Battery ($25.00)
- 1 - DC Jack Adapter ($2.00)
Setting up the Circuit
The first thing we need to do (after soldering headers onto the boards) is set up our circuit so the BME280 can talk to our Pico. In this case, we only need to utilize 4 pins on each device.
Writing the Code
Now that everything's hooked up, we can write the code to read sensor values from the BME280.
Install The Firmware
In order to utilize the pico, we first need to download some firmware and plug the pico into a computer while holding the boot-select. Once the pico's plugged in, we can drop the firmware on via file explorer and it will reboot automatically.
Add The Libraries
At this point, we're starting with a blank slate. To make things easier, we'll be utilizing one of the libraries from adafruit to take care of some of the low-level logic needed to utilize the BME280.
First, download the CircuitPython libraries from the official site. The files can be extracted, and then we'll copy the adafruit_bme280 directory and adafruit_requests file to a lib directory on the pico
Next, we can install an IDE called Thonny, which will allow us to connect to the pico and write some code for it.
At this point, we'll also create the following files:
- thp_sensor.py - The code that controls the sensor and contains the core logic
- secrets.py - A file for keeping secrets, such as the password to connect to wireless
Add the logic
First, we'll start with the sensor logic.
Here, we're creating a sensor class that contains the core logic for interacting with the BME280 sensor. Later, we'll add the logic for generating telemetry that we can record elsewhere.
Next, we're going to add our wireless SSID and Password to our secrets file.
Finally, we can utilize our secrets and the ThpSensor in code.py, which is the main file that gets executed when powering on a CircuitPython device.
Make it useful
Currently, we only have a device that powers on, connects to the wireless, and captures the current temperature, humidity, and pressure. We need to make this more useful by continually capturing the data and sending it to a server where we can process and store the information.
We'll start with adding logic to the sensor that will generate some telemetry objects for us.
Now we can update code.py to utilize our new generate_telemetry function and post the results to an API that saves everything to a database
Now, as long as the device remains powered on, it will send the temperature and humidity to our API every hour.
What are the Benefits?
There are a few reasons capturing information such as the temperature and humidity could be beneficial to farm operations.
Record Keeping & Reporting
Record keeping is an obvious reason for capturing information like this. Once we have the information in a database, not only can we visualize what's happening over time, but we can begin utilizing that information to gain insights on how environmental factors are effecting yields and crop health.
Capturing telemetry from the farm can also allow logic to run that alerts us when something is wrong. For example, if this is running in a greenhouse that has fans that are meant to keep the temperature at 80 degrees, we can then send a text or email when the temperature varies too much - say 75 or 85 degrees.
Similar to alerting, we could also connect multiple devices together, allowing the automation of various aspects of our operations. If the thermometer tells us the greenhouse is 90 degrees, then we can tell a fan to turn on and/or vents to open to help regulate the temperature.
Another scenario would be if soil becomes too dry or too moist, we can tell the irrigation system to turn on or off to rectify the condition.
Do you have ideas on how capturing farm data could help improve your operation? I'd love to hear them in the comments!