[chbot] No-code weather-station to web dashboard

Stephen Irons stephen at irons.nz
Thu May 5 09:53:59 BST 2022


An interesting little project I got going over the past few Covid 
recovery days: connecting a weather-station to a web dashboard, without 
writing any code.

<https://iot.irons.nz/dashboard/cfd3f2f0-c9cf-11ec-820b-4b7d98d3a1b6?publicId=6e6c2c50-a97a-11ec-9bda-613c875470bf>

I have a wireless weather-station, one of those ones that sends data 
from an outside sensor (wind speed and direction, temperature and 
humidity and rain-gauge) to an indoor console over a 433 MHz link. My 
system is the cheap version where the console has no PC/USB interface, 
and does not have Wi-Fi to upload to online weather services; more 
expensive versions do.

I also have a Realtek RTL2832U DVB dongle, often used for cheapo 
software-defined radios.

Duck-duck-going led me to RTL-433, a project that combines an SDR 
project with the RTL dongle, and includes a whole bunch of decoders for 
common 433 MHz devices: garage-door openers, car locks, remote 
controls, and, of course, weather stations.

Installed the available package (sudo apt install rtl-433), fired it 
up, waited a minute (the weather station sends data every 45 s or so), 
and, sure enough, there was the decoded data: temperature in °C, 
humidity in %, wind speed in °, wind direction in km/h.

There are options to
restrict the type of messages decoded, so I don't hear any other 
devicesformat the output as JSON rather than human-readable text
Now, I have been playing around with Thingsboard, an 'IoT' telemetry 
platform, which takes data from external devices, stores and processes 
the data, and has a clicky control centre to create dashboards with 
graphs, tables, etc.

You push data to Thingboard using either an HTTP rest-like API, or 
using MQTT. In either case, the data must be a specific JSON format.

The JSON format from rtl-433 is not the same as expected by Thingsboard.

jq to the resque.

jq is a command-line JSON processor, which can reformat incoming JSON, 
changing the structuring, filtering out unnecessary fields, etc. A 
little bit of playing go the rtl-433 JSON into Thingsboard JSON format. 
It treats each input line as a single JSON document, and transforms 
that into an output line of a single JSON document.

mosquitto_pub is a command-line tool to publish data to a MQTT broker. 
It can send each line of input as a separate message to the broker.

A single rather long command line does the whole thing: rtl_433 | jq | 
mosquitto_pub

So, a bunch of environment variables and a single command line serves 
to receive data from the weather-station and push the data to the 
Thingsboard MQTT broker.

RTL_SERIAL, MQTT_VER, MQTT_HOST, MQTT_PORT, MQTT_TOPIC, MQTT_TOKEN are 
set for your specific device, thenrtl_433 -d:$RTL_SERIAL -F json -M 
time:unix:utc -M protocol -M level -C si | tee -a rtl-thing.log | jq 
--compact-output --monochrome-output --unbuffered '{ ts: (.time | 
tonumber * 1000), values: {battery_ok, temperature_C, humidity, 
wind_dir_deg, wind_avg_km_h, wind_max_km_h, rain_mm, rssi, snr, 
noise}}' | mosquitto_pub -V $MQTT_VER --host $MQTT_HOST --port 
$MQTT_PORT --topic $MQTT_TOPIC --username $MQTT_TOKEN -l
This runs on a Linux media server sitting near the TV.

A simple systemd .service file starts this up and keeps it going if it 
crashes.

There was a bit of clicking around in Thingsboard to create the 
'device' that receives the MQTT data and stores it. Then much clicking 
around to create the dashboard at 
<https://iot.irons.nz/dashboard/cfd3f2f0-c9cf-11ec-820b-4b7d98d3a1b6?publicId=6e6c2c50-a97a-11ec-9bda-613c875470bf>

*Updates that need code*

Depending on when you look at the dashboard, you might see
Wind-speed in knots rather than km/h, as I think of wind in knots. 
There is a one-line Javascript function in Thingboard to do this 
conversion for the wind-speed graph widget and the wind-speed numeric 
display. I guess I could have done this in the jq transformation 
step.The wind-direction is very noisy. I had to add my own smoothing 
calculation. Thingsboard can do averaging, etc. However, smoothing a 
compass reading is not a simple average: if the wind is hovering around 
north, jumping from say 355° to 5°, a normal average gives 180°, 
rather than 0°, definitely not what you want. DDG-ing leads to 
<https://en.wikipedia.org/wiki/Circular_mean>. Essentially, convert the 
angle to cartesian coordinates, smooth the x and y-coordinates 
separately, then use atan2 to convert back to angle.The rain gauge 
reports a single accumulated value since power-on. I have processing to 
calculate rain in the past 1 hour and the past 24 hours. There was a 
tiny bit of rain yesterday evening.Pressure comes from a different 
sensor via a different channel.

Stephen Irons

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20220505/a8d1330e/attachment.html>


More information about the Chchrobotics mailing list