LoRa Rain Gauge
Rain gauge upgraded to LoRaWAN radio technology
While looking for a rain gauge for my IoT weather station, I came across the TFA Dostmann rain transmitter 30.3161, which is very popular in the DIY community. It is offered as a spare part for a weather station for around 20€. There are different approaches to decode the original radio protocol, but unfortunately they always require a separate receiver, which I don't like.
LoRaWAN is becoming widely popular for energy-saving sensor technology in the Internet of Things. The transmission protocol features low power consumption (batteries often last for years) and long ranges (even in urban areas, 1 km can be covered without any problems).
Gateways in the area forward all received data packets to a central server, which then decodes them and makes them available to the owner of the device for further processing. One example of such a public network stack is The Things Network. I already have a LoRaWAN gateway for this. So why not replace the inner components of the rain gauge? An ideal project for a new LoRa sensor was found.
The operating principle of the rain gauge is simple: a funnel collects the rain on a defined area and directs it onto a rocker with two buckets. A rainfall of 0.45 mm causes the water to tip over the seesaw. The other bucket then fills up, while the liquid in the bucket below drains away. A small permanent magnet is attached to the outer edge in the middle of the rocker. This triggers a reed contact on the main circuit board, where the pulses are counted.
In the inside, the sensor consists of three circuit boards with a thickness of 1.0 mm. One with battery contacts for 2x AA batteries and a button, a main board and a transmitter board with antenna. The mechanical dimensions are quickly taken up, everything is redesigned. The battery board remains similar, the new transmitter board only serves as a holder for the antenna. The new microcontroller and all peripherals are located on the main board. Only the battery contacts are reused, as the old reed contact is bistable and the antenna is designed for a different frequency range.
Circuit description
The heart of the circuit is an ATMega328P (IC1), which is clocked at 8MHz by a ceramic resonator (Q1). To detect the pulse of the magnet, either the reed contact (S1) or the Hall sensor TMAG5231 (IC2) can be installed. The RFM95W radio module (IC3) ensures LoRa connectivity. The red LED (D1) indicates the transmission status. Optionally, a BMP280 (IC4) can be installed to measure temperature and air pressure, alternatively via the I2C header (K4). The FRAM memory chip MB85RS64T (IC5) is used to store the LoRaWAN activation data and the counter reading. This is done every three transmissions to reduce the number of write cycles. In the event of a battery change, the stored information is restored. The battery voltage measurement in the lower right-hand area of the circuit diagram remains unpopulated; this could be realized directly with hardware from the microcontroller. The ISP connector (K3) is initially used to program the bootloader. The serial interface on connector K2 can then be used to upload the firmware. Only 3.3V logic levels are permitted on both connections.
Assembly
With two exceptions (C5, C6), resistors and capacitors are kept in the component size 0805 and can be soldered by hand with a little practice. The ceramic resonator can simply be soldered onto evenly pre-tinned pads using hot air. The 2.0x2.5mm BMP280, on the other hand, is much more difficult to solder. However, if you do not want to solder the air pressure sensor directly onto the board using stencil, solder paste and hot air, you can use the I2C pin header (K4) to connect a standard module board. The voltage regulator and level shifter must be desoldered and bridged as shown in the picture in order to keep the quiescent current consumption low.
Mounting
To enable the air pressure to be measured and to prevent moisture from being suctioned into the housing, a 1 mm hole is drilled next to the antenna board and sealed again from the inside with a self-adhesive pressure equalization membrane. The main board and battery board are inserted into each other and initially connected at a solder joint. The two circuit boards are carefully pushed into the housing and the alignment is checked. The main board should be as parallel as possible to the housing wall so that the reed contact or Hall sensor responds properly. After correcting the alignment if necessary by pulling out the circuit boards and heating the solder joint, all connections are finally soldered. Be careful not to mechanically damage the solder pads on the circuit board. The main board and antenna board are connected with a 10 cm long U.FL cable. Once the bootloader has been flashed to the microcontroller, the board can be screwed into the housing. It is also possible to update the firmware while the device is still installed. The serial connector required for this is located on the top edge of the board.
Firmware
The firmware is using the PlatformIO build environment as well as Arduino and the MCCI LMIC library. All dependencies are resolved automatically. For use on the console PlatformIO Core can be downloaded, alternatively VS Code with PlatformIO as plugin can be used as IDE.
After downloading the firmware from Github, we change to the corresponding directory on the console.
cd lora-raingauge
The first step is to burn the bootloader into the ATMega328P once. This requires an AVR-ISP (e.g. USBasp) with a 6-pin connector (K3), which operates at a level of 3.3V. Caution: A higher voltage will destroy the RFM95W. The programming process is performed as follows:
In the src directory the file config.h.example is now copied to config.h and opened with any editor. The OTAA activation data corresponding to the LoRaWAN stack to be used (e.g. The Things Network or Chirpstack) is stored here. The firmware can then be compiled and uploaded via the Arduino bootloader using the serial interface (K2). Again, only 3.3V level is permitted. The process is done via the following command:
After a successful upload, the rain gauge starts and performs an OTAA activation on the LoRaWAN stack. An active LED indicates data transmission. If the activation fails, the LED flashes for a few seconds. The microcontroller then goes into deep sleep mode for 15 minutes. After waking up, data transmission (or activation if failed) starts again. The debug output on the serial interface can be viewed with the following command:
The sensor saves its activation data and the rainfall counter value in the FRAM from time to time so it can be restored from there after a battery replacement. To repeat the OTAA activation, the button in the battery compartment can be held down for a few seconds when inserting the batteries. Furthermore, a checksum of the configuration file is compiled into the firmware and stored in the header of the FRAM data for later comparison. Flashing the firmware with a modified config.h therefore also results in a reset of the data.
The appropriate payload decoder for TTN and Chirpstack can be found in the repository under docs/payload-decoder.js. The packet size varies depending on whether a BMP280 temperature / air pressure sensor has been detected or not. This is also automatically taken into account by the payload decoder.
It is possible to adjust the transmission interval and the rain counter at runtime by sending a corresponding downlink message. Here, fPort 1 is used for the interval and fPort 2 for the counter value. Both are transmitted as an unsigned 16-bit integer (MSB first).
Battery runtime
To determine the battery runtime, the power consumption was measured using the Power Profiler Kit 2. The screenshots below show the power consumption for data acquisition and transmission over a period of about 6s at SF7 and the deep sleep over a period of one minute.
The software outputs the charge drawn in Coulomb. If we look at this over a period of one hour, the circuit will carry out four measurements (36mC/6s - 144mC/24s each) at 15-minute intervals and spend almost one hour (966µC/min - 58mC/h each) in deep sleep. Since the duration of the measurement is negligible, we sum both values to simplify the calculation and get a charge of 202mC/h. This corresponds to 0.0561mAh/h. With a battery capacity of 2000mAh, this results in a runtime of 1485 days or 4 years.
Notes
When using the ATMega328P, please note that the datasheet only specifies it down to 2.7V. However, the newer ATMega328PB can be used as an alternative, which should work reliably at 8MHz in the entire temperature range down to around 2.1V. Corresponding discharge curves show that the assumed capacity of 2000mAh can realistically be provided by an alkaline manganese battery at a discharge current ≤100mA with this final discharge voltage.
The MCUs internal watchdog is used for timing in deep sleep mode. Unfortunately, this uses an RC oscillator, which shows deviations depending on the operating voltage and ambient temperature. If necessary, the interval can be corrected using an uplink packet.
Work on the project has not yet been completed. Changes to the circuit diagram and layout are still possible.
Downloads
- Schematic (PDF)
- Bill of materials (PDF)
- Layouts and BOM
- Layout of the main board in Gerber format
- Layout of the battery board in Gerber format
- Layout of the antenna board in Gerber format
- Firmware at GitHub
- Mounting plate with 90 degree angle for brick wall or flat roof (Thingiverse)
More interesting projects
- rtl_433 - Decoding the original transmitter using an SDR stick (github.com)
- Decoding the TFA 30.3161 rain transmitter (via Raspberry Pi Pico) (blog.heimetli.ch)
- Rain gauge for ioBroker (Source for calculating the amount of rain per pulse) (forum.iobroker.net)
Lizenz
The project may be freely used and modified for private use, but the author's name must be retained (CC BY-NC-SA).