-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_client_receiver.py
46 lines (31 loc) · 1 KB
/
mqtt_client_receiver.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
#!/usr/local/bin/python3
# Subscriber for MQTT with Morse Code Beep
import paho.mqtt.client as paho
import config
from mopp import *
from beep import *
mopp = Mopp()
def on_connect(mqttc, obj, flags, rc):
print("rc: " + str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
r = mopp.decode_message(msg.payload)
print (r)
# Beep if message received
if not "Keepalive" in r:
b = Beep(speed=r["Speed"]) # FIXME
b.beep_message(r["Message"])
def on_publish(mqttc, obj, mid):
print("mid: " + str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
mqttc = paho.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect(config.MQTT_HOST, config.MQTT_PORT, 60)
mqttc.subscribe(config.TOPIC, 0)
mqttc.loop_forever()