RTL-SDR Weather Station Radio setup

Mike Tango Bravo · Updated 2024-08-24

Installing RTL-SDR from source

$ sudo apt install libusb-1.0.0-dev git cmake
$ sudo apt install pkg-config

Then clone the rtl-sdr repo

$ git clone https://github.com/osmocom/rtl-sdr.git 

Once it is cloned, make a build directory, build it and install it:

$ cd rtl-sdr
$ mkdir build
$ cd build
$ cmake ../ -DINSTALL_UDEV_RULES=ON
$ make
$ sudo make install
$ sudo cp ../rtl-sdr.rules /etc/udev/rules.d
$ sudo ldconfig

Creating permissions for non-root users

Next, you need to add some udev rules to make the dongle available for the non-root users.

Find the vendor id and product id for your dongle.

mtb@netrabrick:~$ lsusb
Bus 005 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 004 Device 005: ID 1d6b:0104 Linux Foundation Multifunction Composite Gadget
Bus 004 Device 004: ID 05e3:0610 Genesys Logic, Inc. Hub
Bus 004 Device 003: ID 8087:0029 Intel Corp. AX200 Bluetooth
Bus 004 Device 002: ID 0bda:2838 Realtek Semiconductor Corp. RTL2838 DVB-T
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 002: ID 04b4:0003 Cypress Semiconductor Corp. USB-UART LP
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
lsusb

The important line was the Realtek dongle:

Bus 004 Device 002: ID 0bda:2838 Realtek Semiconductor Corp. RTL2838 DVB-T

The important parts are "0bda" (the vendor id) and "2838" (the product id).

Create a new file as root named /etc/udev/rules.d/20.rtlsdr.rules that contains the following line:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", GROUP="adm", MODE="0666", SYMLINK+="rtl_sdr"

With the vendor and product ids for your particular dongle. This should make the dongle accessible to any user in the adm group. and add a /dev/rtl_sdr symlink when the dongle is attached.

mtb@netrabrick:~$ ls -la /dev/rtl_sdr 
lrwxrwxrwx 1 root root 15 Aug 24 12:14 /dev/rtl_sdr -> bus/usb/004/002

When finished, run

mtb@netrabrick:~$ rtl_test
Found 1 device(s):
  0:  Realtek, RTL2838UHIDIR, SN: 00000001

Using device 0: Generic RTL2832U OEM
Found Rafael Micro R820T tuner
Supported gain values (29): 0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6 
[R82XX] PLL not locked!
Sampling at 2048000 S/s.

Info: This tool will continuously read from the device, and report if
samples get lost. If you observe no further output, everything is fine.

Reading samples in async mode...

Blacklisting the Default Kernel Driver

The default kernel might automatically load drivers for the RTL-SDR, which can prevent SDR applications from accessing the dongle. You need to blacklist it.

  1. Create a blacklist.conf file

    sudo vi /etc/modprobe.d/blacklist.conf

  2. Add the following lines at the end of the file:

    blacklist dvb_usb_rtl28xxu

  3. Unload the conflicting kernel module:

    sudo rmmod dvb_usb_rtl28xxu

  4. Reboot the system to apply changes:

    sudo reboot

Testing SDR

If the SDR radio receiver can be found, you should see similar output:

mtb@netrabrick:~$ rtl_test
Found 1 device(s):
  0:  Realtek, RTL2838UHIDIR, SN: 00000001

Using device 0: Generic RTL2832U OEM
Found Rafael Micro R820T tuner
Supported gain values (29): 0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6 
[R82XX] PLL not locked!
Sampling at 2048000 S/s.

Info: This tool will continuously read from the device, and report if
samples get lost. If you observe no further output, everything is fine.

Reading samples in async mode...

rtl_433

rtl_433 is an open-source software tool used with RTL-SDR (Software Defined Radio) dongles. It is specifically designed for decoding and demodulating signals from various low-cost ISM (Industrial, Scientific, and Medical) band devices that operate around the 433 MHz frequency range. These devices often include sensors such as:

  • Weather stations
  • Tire pressure monitoring systems (TPMS)
  • Remote temperature and humidity sensors
  • Power consumption meters (e.g., smart meters)
  • Doorbells and remote control devices

Installing rtl_433

mkdir -p rtl
git clone https://github.com/switchdoclabs/rtl_433.git
cd rtl_433
mkdir build
cd build
cmake ..
make
sudo make install
rtl_433

Connecting to weather relayer

mtb@netrabrick:~/Aleph_hackathon/oviwox$ weather_relay.py

import json
import subprocess
import requests

# Define the web service endpoint
WEB_SERVICE_URL = "https://aleph-hackathon.vercel.app/api/sensors"  # Replace with your actual web service URL

# Start rtl_433 process
process = subprocess.Popen(['rtl_433', '-F', 'json'], stdout=subprocess.PIPE)

# Read output line by line
for line in iter(process.stdout.readline, b''):
    try:
        # Decode the JSON output from rtl_433
        data = json.loads(line.decode('utf-8').strip())

        # Optionally, you can add custom processing here, for example, adjusting units
        # Example: converting temperature from tenths of a degree if needed
        data['temperature'] = data['temperature'] / 10 if 'temperature' in data else None

        # Send the JSON data to the web service via POST request
        response = requests.post(WEB_SERVICE_URL, json=data)

        # Check if the data was successfully sent
        if response.status_code == 200:
            print(f"Data successfully sent: {data}")
        else:
            print(f"Failed to send data. Status code: {response.status_code}")
            print(f"Response content: {response.content}")

    except json.JSONDecodeError:
        print("Received invalid JSON, skipping...")
    except requests.RequestException as e:
        print(f"Failed to send data due to network error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")