diff --git a/src/Dockerfile b/src/Dockerfile index 95b6ea4..8f74a50 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -12,7 +12,7 @@ # # docker run --name rtl_433 -d -e MQTT_HOST= --privileged -v /dev/bus/usb:/dev/bus/usb -FROM python:3.6.3 +FROM python:3.7 MAINTAINER Marco Verleun LABEL Description="This image is used to start a script that will monitor for events on 433,92 Mhz" Vendor="MarCoach" Version="1.0" diff --git a/src/config.py.example b/src/config.py.example index f5e6e67..8843594 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -1,9 +1,10 @@ # Config section -# Uncomment these lines if your MQTT server requires authentication -#MQTT_USER="mqtt-user" -#MQTT_PASS="mqtt-password" +# Fill in the next 2 lines if your MQTT server expected authentication +MQTT_USER="" +MQTT_PASS="" MQTT_HOST="mqtt.example.com" MQTT_PORT=1883 MQTT_TOPIC="sensors/rtl_433" MQTT_QOS=0 +DEBUG=False # Change to True to log all MQTT messages # End config section diff --git a/src/rtl2mqtt.py b/src/rtl2mqtt.py index 88fc88d..85de674 100755 --- a/src/rtl2mqtt.py +++ b/src/rtl2mqtt.py @@ -7,24 +7,37 @@ import time import paho.mqtt.client as mqtt import os import json +import re from config import * -rtl_433_cmd = "/usr/local/bin/rtl_433 -G -F json" # linux +rtl_433_cmd = "/usr/local/bin/rtl_433 -F json" + +important_rtl_output_re = re.compile("^(Found|Tuned)") # Define MQTT event callbacks def on_connect(client, userdata, flags, rc): - print("Connected with result code "+str(rc)) + connect_statuses = { + 0: "Connected", + 1: "incorrect protocol version", + 2: "invalid client ID", + 3: "server unavailable", + 4: "bad username or password", + 5: "not authorised" + } + print("MQTT: " + connect_statuses.get(rc, "Unknown error")) def on_disconnect(client, userdata, rc): if rc != 0: - print("Unexpected disconnection.") + print("Unexpected disconnection") + else: + print("Disconnected") def on_message(client, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) def on_publish(client, obj, mid): - print("mid: " + str(mid)) + print("Pub: " + str(mid)) def on_subscribe(client, obj, mid, granted_qos): print("Subscribed: " + str(mid) + " " + str(granted_qos)) @@ -33,43 +46,57 @@ def on_log(client, obj, level, string): print(string) # Setup MQTT connection - mqttc = mqtt.Client() -# Assign event callbacks -#mqttc.on_message = on_message + mqttc.on_connect = on_connect -#mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_disconnect = on_disconnect -# Uncomment to enable debug messages -#mqttc.on_log = on_log +if DEBUG: + print("Debugging messages enabled") + mqttc.on_log = on_log + mqttc.on_message = on_message + mqttc.on_publish = on_publish + +if MQTT_PASS: + print("Connecting with authentication") + mqttc.username_pw_set(MQTT_USER, password=MQTT_PASS) +else: + print("Connecting without authentication") -# Uncomment the next line if your MQTT server requires authentication -#mqttc.username_pw_set(MQTT_USER, password=MQTT_PASS) mqttc.connect(MQTT_HOST, MQTT_PORT, 60) - mqttc.loop_start() # Start RTL433 listener -rtl433_proc = subprocess.Popen(rtl_433_cmd.split(),stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) - +print("Starting RTL433") +rtl433_proc = subprocess.Popen(rtl_433_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) while True: if rtl433_proc.poll() is not None: + print("RTL433 exited with code " + str(rtl433_proc.poll())) sys.exit(rtl433_proc.poll()) + for line in iter(rtl433_proc.stdout.readline, '\n'): + if DEBUG: + print("RTL: " + line) + elif important_rtl_output_re.match(line): + print(line) + if rtl433_proc.poll() is not None: + print("RTL433 exited with code " + str(rtl433_proc.poll())) sys.exit(rtl433_proc.poll()) + if "time" in line: - mqttc.publish(MQTT_TOPIC, payload=line,qos=MQTT_QOS) + mqttc.publish(MQTT_TOPIC, payload=line, qos=MQTT_QOS, retain=True) json_dict = json.loads(line) for item in json_dict: value = json_dict[item] if "model" in item: - subtopic=value + subtopic = value + if "id" in item: + subtopic += "/" + str(value) for item in json_dict: value = json_dict[item] if not "model" in item: - mqttc.publish(MQTT_TOPIC+"/"+subtopic+"/"+item, payload=value,qos=MQTT_QOS) + mqttc.publish(MQTT_TOPIC+"/"+subtopic+"/"+item, payload=value, qos=MQTT_QOS, retain=True)