-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluemaestro_mqtt.py
executable file
·64 lines (51 loc) · 1.51 KB
/
bluemaestro_mqtt.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import paho.mqtt.client as paho
import BlueMaestro
import time
import fcntl
import base64
mqtt_host = os.environ['MQTT_HOST']
topic = os.environ['MQTT_TOPIC']
client_name = os.environ['MQTT_CLIENT_NAME']
temperature_calibrate = 0
frequency = 1
mqtt = paho.Client(client_name)
def on_disconnect(mqtt, userdata, rc):
print("Disconnected from MQTT server with code: %s" % rc)
print(userdata)
while rc != 0:
try:
time.sleep(1)
rc = mqtt.reconnect()
except:
pass
print("Reconnected to MQTT server.")
mqtt.on_disconnect = on_disconnect
mqtt.connect(mqtt_host, 1883, 60)
mqtt.loop_start()
def callback(data):
now = time.time()
timestamp = int(now)
if 'data' in data: data['data'] = base64.b64encode(data['data'])
print (data)
try:
mqtt.publish('{}/{}/watchdog'.format(topic, data['name']), 'reset', retain=False)
for k, v in data.items():
mqtt.publish('{}/{}/{}'.format(topic, data['name'], k), v, retain=False)
mqtt.publish('{}/{}/{}/timestamp'.format(topic, data['name'], k), timestamp, retain=False)
except ValueError as e:
print (e)
print (data)
try:
while True:
try:
sensor = BlueMaestro.init()
BlueMaestro.get(sensor, callback)
time.sleep(frequency)
except IOError as e:
print("IOError: "+str(e))
time.sleep(3)
except KeyboardInterrupt:
pass