diff --git a/rtlamr/Dockerfile b/rtlamr/Dockerfile new file mode 100755 index 0000000..d94587f --- /dev/null +++ b/rtlamr/Dockerfile @@ -0,0 +1,54 @@ +# Docker file to create an image for a hass.io add-on that contains enough software to listen to events via RTL_SDR/RTL_433 and then publish them to a MQTT broker. +# The script resides in a volume and can be modified to meet your needs. +# This addon is based on the addon from jdeath here: https://github.com/jdeath/RTLAMR2MQQT +# which in tturn is based on Chris Kacerguis' project here: https://github.com/chriskacerguis/honeywell2mqtt, +# which is in turn based on Marco Verleun's rtl2mqtt image here: https://github.com/roflmao/rtl2mqtt + +# IMPORTANT: The container needs privileged access to /dev/bus/usb on the host. + +ARG BUILD_FROM +FROM $BUILD_FROM + +ENV LANG C.UTF-8 + +MAINTAINER bvlaicu + +LABEL Description="This image is used to start a script that will monitor for RF events on 433Mhz (configurable) and send the data to an MQTT server" + +# +# First install software packages needed to compile rtl_433 and to publish MQTT events +# +RUN apk add --no-cache --virtual build-deps alpine-sdk cmake git libusb-dev && \ + mkdir /tmp/src && \ + cd /tmp/src && \ + git clone git://git.osmocom.org/rtl-sdr.git && \ + mkdir /tmp/src/rtl-sdr/build && \ + cd /tmp/src/rtl-sdr/build && \ + cmake ../ -DINSTALL_UDEV_RULES=ON -DDETACH_KERNEL_DRIVER=ON -DCMAKE_INSTALL_PREFIX:PATH=/usr/local && \ + make && \ + make install && \ + chmod +s /usr/local/bin/rtl_* && \ + apk del build-deps && \ + rm -r /tmp/src && \ + apk add --no-cache libusb mosquitto-clients jq coreutils=8.31-r0 wget=1.20.3-r0 make musl-dev go git gcc + +# Configure Go +ENV GOROOT /usr/lib/go +ENV GOPATH /go +ENV PATH /go/bin:$PATH +RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin + +RUN go get github.com/bemasher/rtlamr + +# +# Define an environment variable +# +# Use this variable when creating a container to specify the MQTT broker host. +ENV MQTT_HOST="hassio.local" +ENV MQTT_USER="guest" +ENV MQTT_PASS="guest" +ENV MQTT_TOPIC="homeassistant/sensor/rtl433" + +ADD ./rtl2mqtt.sh /rtl2mqtt.sh + +CMD cd / && chmod +x /rtl2mqtt.sh && /rtl2mqtt.sh \ No newline at end of file diff --git a/rtlamr/README.md b/rtlamr/README.md new file mode 100755 index 0000000..e36e44f --- /dev/null +++ b/rtlamr/README.md @@ -0,0 +1,56 @@ +# RTLAMR to MQTT Bridge hass.io addon +A hass.io addon for a software defined radio tuned to listen for 433MHz RF transmissions and republish the data via MQTT + +This hassio addon is based on biochemguy's (non-docker) setup: https://community.home-assistant.io/t/get-your-smart-electric-water-and-gas-meter-scm-readings-into-home-assistant-with-a-rtl-sdr +This addon is based on the addon from jdeath here: https://github.com/jdeath/RTLAMR2MQQT +which in tturn is based on James Fry' project here: https://github.com/james-fry/hassio-addons/tree/master/rtl4332mqtt +which was based on Chris Kacerguis' project here: https://github.com/chriskacerguis/honeywell2mqtt, +which is in turn based on Marco Verleun's rtl2mqtt image here: https://github.com/roflmao/rtl2mqtt + +## Usage + +1) Install the addon. Do this by downloading this repository and adding in a folder under /addons/ (eg. /addons/rtlamr2mqtt) + +2) Use addon configuration to configure: +- mqtt_host +- mqtt_user +- mqtt_password +- msgType +- ids + +3) Copy rtl2mqtt.sh to your hass.io config dir in a subdir called rtlamr2mqtt. +i.e. +.../config/rtlamr2mqtt/rtl2mqtt.sh +This allows you to edit the start script if you need to make any changes + +NOTE that some people have reported issues using samba to copy the script. For some reason it does not get copied to the container on start up of the addon. If you see this issue, please scp the script to your hassio config folder, or ssh in and edit the file locally with vi/nano. + + +4) Start the addon + + +## MQTT Data + +Data to the MQTT server is based on biochemguy setup: readings/$DEVICEID/meter_reading + +## Hardware + +This has been tested and used with the following hardware (you can get it on Amazon) + +- NooElec NESDR Nano 2+ Tiny Black RTL-SDR USB +- RTL-SDR Blog R820T2 RTL2832U 1PPM TCXO SMA Software Defined Radio + + +## Troubleshooting + +If you see this error: + +> Kernel driver is active, or device is claimed by second instance of librtlsdr. +> In the first case, please either detach or blacklist the kernel module +> (dvb_usb_rtl28xxu), or enable automatic detaching at compile time. + +Then run the following command on the host + +```bash +sudo rmmod dvb_usb_rtl28xxu rtl2832 +``` diff --git a/rtlamr/config.json b/rtlamr/config.json new file mode 100755 index 0000000..c3fe4be --- /dev/null +++ b/rtlamr/config.json @@ -0,0 +1,33 @@ +{ + "name": "RTLAMR to MQTT Bridge", + "version": "0.0.1", + "slug": "rtlamr", + "description": "RF to MQTT Bridge based on RTL_SDR/RTL_433 for RTL2832U based DVB-T USB tuners", + "startup": "before", + "boot": "auto", + "map": ["config:rw", "ssl"], + "devices": ["/dev/bus/usb:/dev/bus/usb:rwm"], + "host_network": "False", + "arch": [ + "aarch64", + "amd64", + "armv7", + "i386" + ], + "options": + { + "mqtt_host": "hassio.local", + "mqtt_user": "", + "mqtt_password": "", + "msgType": "scm", + "ids": "1234,1235" + }, + "schema": + { + "mqtt_host": "str", + "mqtt_user": "str", + "mqtt_password": "str", + "msgType": "str", + "ids":"str" + } +} diff --git a/rtlamr/icon.png b/rtlamr/icon.png new file mode 100644 index 0000000..6fe5b83 Binary files /dev/null and b/rtlamr/icon.png differ diff --git a/rtlamr/logo.png b/rtlamr/logo.png new file mode 100644 index 0000000..6fe5b83 Binary files /dev/null and b/rtlamr/logo.png differ diff --git a/rtlamr/rtl2mqtt.sh b/rtlamr/rtl2mqtt.sh new file mode 100755 index 0000000..725ef52 --- /dev/null +++ b/rtlamr/rtl2mqtt.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +export LANG=C +PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin" + +CONFIG_PATH=/data/options.json +MQTT_HOST="$(jq --raw-output '.mqtt_host' $CONFIG_PATH)" +MQTT_USER="$(jq --raw-output '.mqtt_user' $CONFIG_PATH)" +MQTT_PASS="$(jq --raw-output '.mqtt_password' $CONFIG_PATH)" +MQTT_MSGTYPE="$(jq --raw-output '.msgType' $CONFIG_PATH)" +MQTT_IDS="$(jq --raw-output '.ids' $CONFIG_PATH)" + + +# Start the listener and enter an endless loop +echo "Starting RTLAMR with parameters:" +echo "MQTT Host =" $MQTT_HOST +echo "MQTT User =" $MQTT_USER +echo "MQTT Password =" $MQTT_PASS +echo "MQTT Message Type =" $MQTT_MSGTYPE +echo "MQTT Device IDs =" $MQTT_IDS + + +#set -x ## uncomment for MQTT logging... +/usr/local/bin/rtl_tcp &>/dev/null & + +# Sleep to fill buffer a bit +sleep 15 + +LASTVAL="0" + +# set a time to listen for. Set to 0 for unliminted + +# Do this loop, so will restart if buffer runs out +while true; do + +/go/bin/rtlamr -format json -msgtype=$MQTT_MSGTYPE -filterid=$MQTT_IDS | while read line + +do + VAL="$(echo $line | jq --raw-output '.Message.Consumption' | tr -s ' ' '_')" # replace ' ' with '_' + DEVICEID="$(echo $line | jq --raw-output '.Message.ID' | tr -s ' ' '_')" + MQTT_PATH="rtlamr/$DEVICEID/meter_reading" + + # Create file with touch /tmp/rtl_433.log if logging is needed + [ -w /tmp/rtl_433.log ] && echo $line >> rtl_433.log + if [ "$VAL" != "$LASTVAL" ]; then + echo $VAL | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_PATH + LASTVAL=$VAL + fi + +done + +sleep 60 + +done \ No newline at end of file