Make logging, MQTT auth and debugging easier

This commit is contained in:
Adam Whittingham
2019-08-18 18:13:03 +01:00
parent 8a704ba909
commit 1744a53558
2 changed files with 22 additions and 15 deletions

View File

@@ -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

View File

@@ -10,7 +10,7 @@ import json
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"
# Define MQTT event callbacks
def on_connect(client, userdata, flags, rc):
@@ -22,7 +22,7 @@ def on_connect(client, userdata, flags, rc):
4: "bad username or password",
5: "not authorised"
}
print(connect_statuses.get(rc, "Unknown error"))
print("MQTT: " + connect_statuses.get(rc, "Unknown error"))
def on_disconnect(client, userdata, rc):
if rc != 0:
@@ -41,34 +41,40 @@ 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:
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
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 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)
json_dict = json.loads(line)