-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqttc.py
51 lines (36 loc) · 1.43 KB
/
mqttc.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
import paho.mqtt.client as mqtt
server = "maqiatto.com"
port = 1883
# use the values shown after logging into https://cloud.arduino.cc
username = "jaybhavsar98@gmail.com"
password = "jaybhavsar98"
topic = "jaybhavsar98@gmail.com/autobot"
# The callback for when the client is connected to the broker
def on_connect(client, userdata, flags, rc):
print("Connected with result: " + mqtt.connack_string(rc))
# subscribe to the topic
client.subscribe(topic)
# The callback for when the client is disconnected from the broker
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection")
# The callback for when the client is subscribed to a topic
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed to topic: " + topic)
# The callback for when a message for a subscribed topic is received from the broker
def on_message(client, userdata, msg):
print("Received message: " + msg.topic + " " + str(msg.payload))
# create a new MQTT client
client = mqtt.Client()
# configure CA cert for SSL connection
# set username and password
client.username_pw_set(username, password)
# assign callbacks
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_message = on_message
# start connection
client.connect(server, port)
# let the client loop forever (this is a blocking call)
client.loop_forever()