-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagestore.py
executable file
·52 lines (32 loc) · 1018 Bytes
/
messagestore.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
#!/usr/bin/python
from store_to_db import mqtt_device
import paho.mqtt.client as mqttClient
import time
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
def on_message(client, userdata, message):
print message.topic + message.payload
mqtt_device(message.topic, message.payload)
Connected = False #global variable for the state of the connection
broker_address= "4ytm.com"
port = 1883
client = mqttClient.Client("Python")
client.on_connect= on_connect
client.on_message= on_message
client.connect(broker_address, port=port)
client.loop_start()
while Connected != True:
time.sleep(0.1)
client.subscribe("owntracks/#")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
client.disconnect()
client.loop_stop()