Contents

Experimenting with RF using RTL-SDR

Using CubicSDR, rtl_433, MQTT and telegraf to stream live data to InfluxDB

Introduction

I was always interested in the RF world, the fact that we are surrounded by an infinite amount of invisible waves that carry information is intriguing. A lot has changed over the years, the technology advanced and today you can easily dive into the electromagnetic realm without fiddling with mountains of equipment and burning your pocket.

In this post we’ll explore using RTL-SDR to read live temperature and humidity stats from cheap 433.92Mhz modules, feeding them to InfluxDB using rtl_433, MQTT and Telegraf on a Raspberry Pi (but any Linux based device can work)

Hardware

RTL-SDR

The definition of SDR is “Software Defined Radio”, it’s a radio-communication system where instead of using components that were traditionally implemented in hardware - software is used, allowing greater flexibility and reducing costs.

RTL-SDR is a high quality USB dongle (but relatively cheap at ~$25) that can be used to scan and receive radio signals from 500Khz (24Mhz for the device I used) up to 1.75Ghz. It was designed and built by the awesome dudes over at RTL-SDR.com based on DVB-T TV tuner dongles. I ordered this kit which included two modular dipole antennas.

RF Wireless Hygrometer + Thermometer

Almost any RF based wireless thermometer / humidity sensor is probably readable by rtl_433. Since we’re talking about cheap hardware you can’t really get any assurances, checking the RF frequency is probably enough - if it says 433.92Mhz, you are 99% good. I’ve used this as my indoor sensor and this as my outdoor, for monitoring my garden soil humidity level and outdoor temperature

Raspberry Pi

I’ve used a RPi4 that I had lying around loaded with Raspbian Buster.

Software

CubicSDR for MacOS

This is an awesome free open-source software for SDR. It wasn’t easy finding one for MacOS, most of the commonly-used projects are made for Linux and Windows. It’s not really required for decoding 433.92Mhz signals, but I wanted to get my hands dirty and look on live RF, it’s pretty neat (and of course to test the device is actually working!)

rtl_433

An amazing project that can be used to decode a variety of RF-based devices on various frequencies (433.92 MHz, 868 MHz, 315 MHz, 345 MHz, and 915 MHz ISM bands). This is the software that does the heavy lifting.

Mosquitto

Very simple MQTT server and client implementation, we’ll be using that as the output of rtl_433 and input of Telegraf to stream the metrics to a remote InfluxDB instance.

Telegraf

Agent for collecting and writing metrics. It will subscribe to our MQTT queue, parse and transmit the metrics to InfluxDB.

Getting a signal

Connecting the antennas

Let’s start by quickly testing our shiny RTL-SDR receiver, I’ve used the two short 5cm dipole telescopic antennas for this test, you should definitely read this great guide that the awesome dudes over at rtl-sdr.com wrote on using the provided antennas. The antenna used is critical for getting high-quality signal in some cases. For our radio FM test any antenna will probably suffice, but if you want to receive weather satellite signal running on 137Mhz, you’ll have to be more specific.

Hopefully you’ve ended up with something like this:

Listening to radio

Now you can connect the dongle to your usb port, launch CubicSDR, choose “Generic RTL2832U OEM” and hit “Start”, you can ignore the extra settings you see on the right for now. You should see a pretty moving signal heatmap, you can change the center frequency by hovering over it and tapping space (the key), I chose a local FM radio station that is broadcasting on 88Mhz (FM).

Now your bandwidth setting should be set to 200Khz, you should get a nice signal and when you hover your mouse over it the band will “encapsulate” the signal, click and you should start hearing radio!

Checking Frequency 433.92Mhz

Let’s get ready to use rtl_433 by looking at the frequency and making sure there are devices there. Usually they will transmit information in 30 second bursts, you can change the center frequency to 433.92Mhz and check that you see data. Near the end of the video above you can see the signal bursts when I switch to 433.92Mhz

Decoding on the RPi

Installing and running rtl_433 on our RPi

I’m assuming you have a working Raspberry Pi with SSH access running Raspbian. We’ll need to compile rtl-sdr and rtl_433:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install RTL-SDR
mkdir ~/sdr
cd ~/sdr
sudo apt-get update
sudo apt-get install git git-core cmake libusb-1.0-0-dev build-essential
git clone git://git.osmocom.org/rtl-sdr.git
cd rtl-sdr/ && mkdir build && cd build/
cmake ../ -DINSTALL_UDEV_RULES=ON -DDETACH_KERNEL_DRIVER=ON
make
sudo make install
sudo ldconfig
1
2
3
4
5
6
7
8
# Install rtl_433
cd ~/sdr
git clone https://github.com/merbanan/rtl_433.git
cd rtl_433
mkdir build
cd build && cmake ../
make
sudo make install

You can now connect the RTL-SDR dongle to the Raspberry Pi and see if you can get some temperature and humidity data! (If you haven’t tested your sensors, now is the time to do it). Run rtl_433 and you should get some data like in the video below

Installing Mosquitto and Telegraf

Mosquitto

So we’re getting live sensor data, that’s pretty awesome, let’s stream everything to InfluxDB. You can open a free Influx Cloud account and we’ll configure Telegraf to stream information to it, after you open the account generate an API key for your bucket.

We’ll use Eclipse Mosquitto as the MQTT server, it’s a very lightweight implementation of MQTT server, providing simple pub/sub service which we’ll use.

1
2
3
4
5
sudo apt-get install mosquitto mosquitto-clients

# Enable Mosquitto service and check that it's running
sudo systemctl enable mosquitto
sudo systemctl status mosquitto

The logs are accessible over at /var/log/mosquitto/mosquitto.log

Telegraf

1
2
3
4
5
6
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
DISTRIB_ID=$(lsb_release -c -s)
echo "deb https://repos.influxdata.com/debian ${DISTRIB_ID} stable" | tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update
sudo apt-get install telegraf

Before running Telegraf service let’s configure it, you can backup the existing config and paste the contents of my /etc/telegraf/telegraf.conf like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
sudo mv /etc/telegraf/telegraf.conf /etc/telegraf/telegraf.conf.bak
sudo bash -c 'cat << EOF > /etc/telegraf/telegraf.conf
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = ""
  omit_hostname = false
[[outputs.influxdb_v2]]	
  urls = ["CHANGE THIS"]
  token = "CHANGE THIS"
  organization = "CHANGE THIS"
  bucket = "CHANGE THIS"
[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  qos = 0
  connection_timeout = "30s"
  topics = [ "rtl_433/#" ]
  client_id = "telegraf"
  persistent_session = false
  data_format = "json"
EOF'

# You can now run telegraf manually to make sure the configuration is okay
telegraf --config /etc/telegraf/telegraf.conf

# If all went well go ahead and restart the already running service
sudo systemctl restart telegraf

You’ll need to get your InfluxDB endpoint url, token, organization name and bucket from Influx cloud. The MQTT consumer settings specifies connecting to our MQTT server and listening to rtl_433/# topic, which basically says any topic with the prefix rtl_433/ will be sent to InfluxDB.

The expected data should be json (as specified by the data_format option)

Running rtl_433

Now for the last part, just run rtl_433 with mqtt output!, to make sure everything works you can first run a mosquitto subscription client and listen to incoming events by running mosquitto_sub -t "rtl_433/#", afterwards launch it (I like using screen for that, see the next video):

1
2
3
rtl_433 -C si -F "mqtt://localhost:1883,events=rtl_433[/model][/id]"
# You will not get any data in the shell so if you think 
# something is not working launch mosquitto_sub and make sure you are getting events

If all went well data should be flowing to InfluxDB! hurrah!

Viewing your data

Using InfluxDB is outside the scope of this post but you should be able to get funky graphs like this one:

/posts/2020-07-11-rtl-sdr-rpi-influxdb-temperature-monitoring/images/influx_dashboard.png

Final words

Using RTL-SDR for decoding RF data is only the tip of the iceberg of what you can do with it, I highly suggest going to rtl-sdr.com and checking some of their amazing tutorials - You can receive live weather satellite data, decode trunked radio systems and see the universe!