Distributed weather monitoring in gardens

Garden Tech

© Lead Image © Lorelyn Medina, 123RF.com

© Lead Image © Lorelyn Medina, 123RF.com

Article from Issue 239/2020

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.

Figure 1: Custom sensor setup.

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.

Figure 2: Schematic [6].
Figure 3: PCB assembly in KiCad 3D viewer.

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.

Figure 4: Microcontroller pins configured in CubeMX.

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 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.

Figure 5: Raspberry Pi and Gateway PCB in a weatherproof box.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • LoRa Long Range Radio

    WiFi is convenient for devices that are in the same house. If you want to extend the distance, give LoRa a call.

  • Prometheus

    Legacy monitoring solutions are fine for small-to-medium-sized networks, but complex environments benefit from a different approach. Prometheus is an interesting alternative to classic tools like Nagios.

  • Automated Irrigation

    An automated watering system comprising a Raspberry Pi Zero W, an analog-to-digital converter, and an inexpensive irrigation kit can help keep your potted plants from dying of thirst.

  • Electronic Weighing

    Create your own weighing device with easily available components and open source software.

  • WiFi Thermo-Hygrometer

    A WiFi sensor monitors indoor humidity and temperature and a Node-RED dashboard reports the results, helping you to maintain a pleasant environment.

comments powered by Disqus
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.

Learn More