Distributed weather monitoring in gardens
Garden Tech
Place long-range wireless sensors in a garden and keep track of ambient conditions with gauges and time-based graphs.
As any gardener knows, an understanding of local weather plays an important role in gardening success. Frost can kill off delicate seedlings, too much sun can frazzle shade-loving plants, and too much moisture can suffocate roots. I have relied for a few years on a single temperature sensor outside and an indicator inside for an overview of the conditions outdoors. Although it is a useful tool, it has several drawbacks: The wired sensor is at a single, fixed location, only measures current temperature, and is too close to the house. I wanted a system that provides multiple measurement types at multiple locations, requires no wires trailing around the garden, would provide seasonal data to better plan for planting, and has the capacity for alarms to warn of frost and high temperatures. Ideally, the data would be available from a computer and mobile phone without installing additional software.
In this article, I describe the design and implementation of such a system with only free and open source tools in a Linux environment. With this system, I hope to identify microclimates within my garden and select optimum planting conditions for plants with different needs.
Multiparameter Sensor
The most important requirement for a sensor for this application is that it transmits data wirelessly, which, of course, implies battery power. I considered a number of wireless technologies, including WiFi and Bluetooth, but I rejected these on the basis of power consumption, limited range, and other reasons. I've worked previously with LoRa [1], a radio technology expressly designed for low data rate, low power, and long-range data telemetry. Additionally, the LoRaWAN wide area network protocol works with a network of public gateways and public "landing points" for data produced by sensors. This protocol exists expressly to build networks of Internet of Things (IoT) sensors.
The sensors chosen for this project measure four parameters: temperature, pressure, light level (intensity), and humidity. Additionally, the sensor must return its battery level, so I know when the battery needs to be changed. Many commercial sensors [2] meet these requirements, and it would be perfectly possible to build a sensor array from such devices. However, being a hardware engineer, I decided to design my own sensors (Figure 1) tailored to my own needs.
With easy-to-install sensors, I could move them to different locations around my garden to observe microclimates in places discreet enough not to look out of place. To that end, I chose an enclosure the size of a matchbox. A large 1000mAh lithium cell provides enough power for the sensor for several years of operation, provided the unit sleeps at 10-minute intervals, waking up briefly to take a measurement and send a data packet. Sensors for the required measurements are all available as I2C devices, enabling them to be bussed together and connected to a small microcontroller. Power to the devices is provided with a MOSFET switch [3], so the devices can be powered down between measurements.
By measurement, I have confirmed that the standby consumption of the sensor is less than 50µA. The LoRa radio modem is connected over an SPI bus to the microprocessor, which has a connector for programming and debugging, with connections for an ST-Link programmer and a serial port. Power is supplied by a battery, and the radio antenna is a helical design for compactness. The STMicroelectronics STM32L432KCU6 microcontroller [4] was chosen for the right mix of I/O pins, low power consumption, and small package size.
Sensor Design
For the schematic capture and PCB design work, I used KiCad [5], a free and open source design package that runs on Linux and allows 3D visualization of the layout (Figure 2) before committing to designing a PCB (Figure 3), which is especially helpful in designs with tight mechanical constraints. It is also possible to export STEP files [7] to other CAD/CAM packages, such as FreeCAD (another free and open source package), so the PCB assembly can be incorporated into a larger assembly.
The PCB design incorporates a battery holder for the lithium cell, the helical antenna, the connector for programming, screw holes, and an outline adapted to my chosen enclosure, a Hammond 1551GFLGY [8]. The lid of this enclosure has flanges that allows it to be attached to supporting structures with screws, wire, or cable ties. A small hole directly adjacent to the sensor cluster allows air and light into the enclosure. Care must be taken to ensure the enclosure is mounted vertically to prevent flooding.
Software for the sensor was adapted from an example provided by STMicroelectronics that includes a complete port of LoRaWAN, so the bulk of software work involves incorporating drivers for the I2C sensors and formatting the data into a LoRaWAN packet. The software is compiled with the ARM variant of GCC, so no special compiler is required. STMicroelectronics provides the excellent CubeMX [9] tool as a free download, which allows you to set up the target microcontroller's peripherals and clock, as well as generate a skeleton application and a makefile (Figure 4). For help in getting started, you can find many excellent tutorials for CubeMX on YouTube produced by STMicroelectronics and independent makers.
Once the microcontroller is configured and the code is generated, all you need is your favorite editor and standard Linux tools such as make. CubeMX does a very good job of generating code without deleting your additions, so development with CubeMX can be an incremental process. Programming is achieved over a two-wire interface called ST-Link. USB-based ST-link programmers are available on the Internet for less that $5 (EUR5/£5). The excellent stlink [10] open source package works with these programmers to flash new software onto the board. The complete software project and the CubeMX configuration file is available on my GitHub page [11].
LoRaWAN
LoRaWAN is a wide-area network protocol based on LoRa radio technology [12]. LoRaWAN has gateways that forward LoRaWAN packets over the Internet to a network server, from which the data can be collected and further processed. The gateway is a slightly more complex LoRa radio transceiver, which can transmit and receive on several channels simultaneously. I chose a commercially available gateway produced by IMST [13] that requires a host computer communicating over an SPI bus.
IMST provides an excellent tutorial [14] for building a complete gateway with a Raspberry Pi, which amplifies the step-by-step instructions in this article on construction, software setup, and gateway configuration and includes a complete software application for forwarding uplink and downlink packets.
The Raspberry Pi and Gateway PCB are mounted in a weatherproof box with an external antenna (Figure 5), although the gateway is currently installed in my attic and communicates with all my sensors with no problems. I set up WiFi on the Raspberry Pi, so I don't need to connect a LAN cable. Power comes from an external 5V, 3A plug-top supply.
If a public gateway is already available within range of your sensors, you can register your devices to that gateway and save the expense of setting up your own. At the time of print, more than 12,000 gateways were registered with The Things Network [15] in 150 countries.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.