· 8 years ago · Dec 15, 2017, 07:06 PM
1import paho.mqtt.client as mqtt
2import RPi.GPIO as gpio
3
4#set up the mqtt
5username = "@gmail.com"
6password = ""
7host = "mqtt.dioty.co"
8port = 1883
9
10#set up the gpio
11gpio.setwarnings(false)
12gpio.setmode(gpio.board)
13gpio.setup(11, gpio.out)
14
15def on_connect(client, userdata, flags, rc):
16 client.subscribe("/@gmail.com/on")
17 print("connected...\n")
18
19
20def on_message(client, userdata, msg):
21 print(msg.topic+" "+str(msg.payload))
22 client.publish("/@gmail.com/connected", str(msg.payload), qos=0, retain=False)
23 # if str(msg.payload) == b'true':
24 # x = x
25
26
27def on_disconnect(client, userdata, rc):
28 if rc != 0:
29 print("Unexpected disconnection.")
30
31client = mqtt.Client()
32client.on_connect = on_connect
33client.on_message = on_message
34client.on_disconnect = on_disconnect
35
36client.username_pw_set(username, password)
37client.connect(host, port, 60)
38
39
40
41client.loop_forever()