forked from mverleun/RTL433-to-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtl2mqtt.py
executable file
·102 lines (81 loc) · 2.86 KB
/
rtl2mqtt.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import subprocess
import sys
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 -F json"
important_rtl_output_re = re.compile("^(Found|Tuned)")
# Define MQTT event callbacks
def on_connect(client, userdata, flags, 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")
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("Pub: " + str(mid))
def on_subscribe(client, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(client, obj, level, string):
print(string)
# Setup MQTT connection
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_disconnect = on_disconnect
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")
mqttc.connect(MQTT_HOST, MQTT_PORT, 60)
mqttc.loop_start()
# Start RTL433 listener
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, retain=True)
json_dict = json.loads(line)
for item in json_dict:
value = json_dict[item]
if item == "model":
subtopic = value
if item == "id":
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, retain=True)