-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmqtt.py
More file actions
30 lines (23 loc) · 873 Bytes
/
mqtt.py
File metadata and controls
30 lines (23 loc) · 873 Bytes
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
from subprocess import call
import paho.mqtt.client as mqtt
import time
def on_connect(client, userdata, flags, rc) :
if rc == 0:
print "Connected with code: " + str(rc)
#make sure to renew subscription at every new connection
client.publish("smartstb/events",'{"event":"play","ts":"2016-02-17T01:40:03+0000"}')
print "sent start"
time.sleep(7)
client.publish("smartstb/events",'{"event":"stop","ts":"2016-02-17T01:40:10+0000"}')
print "sent stop"
#client.subscribe("smartstb/events")
else:
raise Exception
# Listen to PUBLISH messages received by server
def on_message(client, userdata, msg):
print (msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('127.0.0.1', 1883, 60)
client.loop_forever()