-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensornode.py
53 lines (42 loc) · 1.29 KB
/
sensornode.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
#/bin/python
import serial
import paho.mqtt.client as paho
from threading import Timer
import config
room_open = False
def on_connect(mosq, obj, rc):
print "[MQTT] Connect with RC " + str(rc)
def on_disconnect(client, userdata, rc):
print "[MQTT] Disconnected " + str(rc)
try_reconnect(client)
def on_log(client, userdata, level, buf):
print "[MQTT] LOG: " + buf
# MQTT reconnect
def try_reconnect(client, time = 60):
try:
print "[MQTT] Trying reconnect"
client.reconnect()
except:
print "[MQTT] Reconnect failed. Trying again in " + str(time) + " seconds"
Timer(time, try_reconnect, [client]).start()
# initialize MQTT
print "[Main] Initializing MQTT Client"
mqttc = paho.Client()
mqttc.username_pw_set(config.broker["user"], config.broker["password"])
mqttc.connect(config.broker["hostname"], config.broker["port"], 60)
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_log = on_log
print "[Main] Initialize serial port"
ser = serial.Serial(config.serial_port, 9600)
# Loop forever
print "[Main] Entering loop"
mqttc.loop_start()
while True:
message = ser.readline().strip()
print "[Serial] " + message
(topic,value) = message.split(";")
mqttc.publish(config.topic_prefix + topic, value, 0, False)
# Clean up afterwards
print "[Main] Cleanup"
mqttc.disconnect()