/raspberry-pi-data-collector

Collect temperature data from Raspberry Pi and send to Azure IoTHub + Homebridge

Primary LanguagePython

raspberry-pi-data-collector

Collect temperature data from 433mhz pool sensor using SDR (Software-defined radio) on Raspberry Pi.

Flow

  1. Use SDR to collect data from 433 mhz temperature sensor
  2. Send data to local MQTT server
  3. A number of clients subscribe to MQTT data and do the following:

Temperature sensor

First I tried the WeatherHawk Myblue-T bluetooth dongle, inspired by this article: https://medium.com/jesseproudman/raspberry-pi-weatherhawk-myblue-t-bluetooth-temperature-sensors-9dac2c8be482. However, the range of the bluetooth dongle is extremely limited. I therefore moved on to a 433mhz solution.

For some reason the TFA sensor is detected by RTL_433 as "Ambient Weather F007TH Thermo-Hygrometer". This could indicate it is an OEM product being sold under various brands.

SDR radio

SDR radio for Raspberry Pi:

Works perfectly with the RTL_433 project.

Setup

Setup RTL 433

sudo apt-get install libtool libusb-1.0.0-dev librtlsdr-dev rtl-sdr

git clone https://github.com/merbanan/rtl_433.git

cd rtl_433/
mkdir build
cd build
cmake ../
make
sudo make install

Setup local MQTT server

sudo apt-get install mosquitto mosquitto-clients

you may want to change the config if you plan to handle large bursts of messages

sudo vim /etc/mosquitto/mosquitto.conf
sudo systemctl restart mosquitto

add

max_inflight_messages 10000
max_queued_messages 40000

Collect samples and send to local MQTT server

rtl_433 -F json -C si -U | mosquitto_pub -t home/rtl_433 -l

or use startCollector.sh script

Test everything is working with simple client

mosquitto_sub -h localhost -t home/rtl_433

Subscribe to topic and write to local file

This is used by homebridge plugin to provide Apple HomeKit access to data - I can ask Siri for my pool temperature.

mosquitto_sub -h localhost -t home/rtl_433|~/writeStdInToFile.py -c poolSensor

or use startLocalFile.sh script

Subscribe to topic and generate local event log

mosquitto_sub -h localhost -t home/rtl_433>>/opt/sensor/temperature-event.log 2>>/opt/sensor/tempeature-client-error.log

or use startEventLog.sh script

Subscribe to topic and send to Azure IotHub

First we need a SAS (shared access signature) token for our device https://github.com/Azure/iothub-explorer

npm install -g iothub-explorer
iothub-explorer login your-iot-connectionstring
iothub-explorer create yourDeviceName
iothub-explorer sas-token -d 315360000 yourDeviceName

(-d 315360000 = Token expires in 10 years)

I spent some time bridging Mosquitto MQTT to Iothub, setting up Mosquitto, Azure certificates, etc.

Then I realized the easiest way to send data to Azure IotHub was a simple python script performing a HTTP POST:

mosquitto_sub -h localhost -t home/rtl_433|~/sendStdInToIothub.py -c poolSensor

To monitor activity on Azure IotHub:

iothub-explorer monitor-events --login HostName=.azure-devices.net\;SharedAccessKeyName=iothubowner\;SharedAccessKey=

Replay old events

cat event-output-aa | mosquitto_pub -t home/rtl_433 -l

or start with latest entries

cat event-output-aa | perl -e 'print reverse <>' | mosquitto_pub -t home/rtl_433 -l

or replay given date

grep 2017-08-30 temperature-event.log | mosquitto_pub -t home/rtl_433 -l

Local dev install of Mosquitto on Mac

brew install mosquitto
brew services start mosquitto

Test publication of event

mosquitto_pub -f temperature-sample.json -t home/rtl_433