-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt.py
52 lines (37 loc) · 1.3 KB
/
mqtt.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
import sys
import paho.mqtt.client as mqtt
server = "maqiatto.com"
port = 1883
username = "jaybhavsar98@gmail.com"
password = "jaybhavsar98"
topic = "jaybhavsar98@gmail.com/autobot"
message = "1"
if len(sys.argv) > 1:
message = sys.argv[1]
# 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))
# publish message to the topic
client.publish(topic, message)
# 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 has published a message
def on_publish(client, userdata, mid):
print("Message published: " + message)
# message published, disconnect
client.disconnect()
# 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_publish = on_publish
# start connection
client.connect(server, port)
# let the client loop (this is a blocking call), will unblock when the client disconnects
client.loop_forever()