DIY low-power LoRa water-meter - Part 5 : Integration with TTN, Node-RED and Home-Assistant

Saturday, August 5, 2023

This blog post is part of a series of posts:


Now that the device is built and the firmware is running as expected, let’s see how to actually use it and get the measurements out of the sensor!

As a reminder, the device counts pulses from the watermeter and sends the data (message number, battery voltage and number of pulses detected) to TTN using LoRaWAN technology.

If you own your own LoRaWAN gateway, you should start by registering it to the TTN network .

Untitled

My gateway is the Pine64 indoor gateway . It’s currently sitting on my desk, and I’ve already installed it in my garage for some range testing. It works very well, and I know that Pine64 plans on releasing an outdoor version of this gateway, which would be awesome!

Next, you’ll need to register your end device on the TTN network . During this process, you’ll be asked to provide a few parameters like the Device ID, keys, etc. Those info are hard-coded in the file src/LoRaMac-node/src/peripherals/soft-se/se-identity.h . Don’t forget to edit this file with different values and to ensure that you provide consistent information to the TTN console.

Untitled

Then, if you power up the device, messages should appear on the Live Data panel of the TTN console:

Untitled

Great, the messages are correctly sent by the device and received by TTN.

Now, we want to integrate those data into our custom application : Home-Assistant. Fortunately, TTN provides multiple integration functionalities.

Untitled

For this project, I decided to use the MQTT integration, which I already know and which integrates well with home-assistant. With this integration, TTN publishes all messages received from the end device into a specific topic on their MQTT server.

Untitled

Now, all I need to do is make Home-Assistant connect to this MQTT server and process the data. The simplest way I found to do this is using the Node-RED integration in Home-Assisant. Node-RED allows the creation of nice automations using a graphical environment.

Here is what I came up with:

Untitled

This flow diagram first connects to the TTN MQTT server and subscribe to the “global” topic #. When a new message is receives (in JSON format), the payload is parsed and the 3 values are extracted. Finally, those processed values are published on… the internal MQTT server of my Home-Assistant instance. That’s probably not the most straightforward way to do this, but I couldn’t find any other way to make HA work with 2 MQTT servers…

Then I added a new MQTT sensor by editing the file configuration.yaml :

mqtt:
  sensor:
   - name: "watermeter_usage"
     state_topic: "/watermeter/water_value"
     unit_of_measurement: "L"
     device_class: "water"
     state_class: "total_increasing"
     unique_id: "watermetervalue"
   - name: "watermeter_battery"
     state_topic: "/watermeter/battery"
     unit_of_measurement: "v"
     device_class: "voltage"
     state_class: "measurement"
     unique_id: "watermeterbattery"

This adds 2 new sensors to my HA instance : watermeter_usage which represents the number of pulses detected by the sensor and watermeter_battery which is the battery voltage of the device.

Now, all I have to do is adding this sensor to the Energy dashboard of home assistant :

Untitled

Wrap up

The general idea of this project is very simple : I just want to count the number of pulses generated by my water meter and process the data in my home automation application.

But when you look at all the details, this project becomes way more interesting, especially for a software engineer who has somewhat limited experience with hardware and electronics. This project allowed me to learn, experiment and have fun with

  • multiple MCU families and architectures
  • low-power embedded software programming
  • LoRa, LoRaWAN and The Things Network (TTN) public network
  • MQTT and home automation
  • prototyping on breadboard
  • PCB design and manufacturing
  • assembly of the whole device in a waterproof case

And, once it’ll be installed on my water meter, this project will eventually allow me to monitor my water usage. I’ll probably also connect it to my gas meter in the near future!

Embeddeddiynrf52projectlorattn

JF

I am passionate about IT, (embedded) software development and open source technologies in general. I’m mainly working on the InfiniTime project , an open source firmware for the PineTime smartwatch from Pine64 .

Install Armbian and Proxmox on the OrangePi5+ (RK3588)

DIY low-power LoRa water-meter - Part 4 : Software