-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_publisher_class.py
66 lines (50 loc) · 2.27 KB
/
mqtt_publisher_class.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import paho.mqtt.client as paho
from paho import mqtt
import yaml
import sys
from signal import signal, SIGINT
class MQTTPubClient(paho.Client):
def __init__(self):
# using MQTT version 5 here
# userdata is user defined data of any type, updated by user_data_set()
# client_id is the given name of the client
self.client = paho.Client(
client_id="", userdata=None, protocol=paho.MQTTv5)
with open("config.yaml", "r") as f:
try:
config_params = (yaml.safe_load(f))
self.cloud_port = config_params['cloud_port']
self.username = config_params['username']
self.password = config_params['password']
self.cluster_name = config_params['cluster_name']
self.cloud_message = config_params['cloud_message']
except yaml.YAMLError as e:
print(e)
self.client.on_connect = self.on_connect
# enable TLS for secure connection
self.client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
# set username and password
self.client.username_pw_set(self.username, self.password)
# connect to HiveMQ Cloud on port 8883 (default for MQTT)
self.client.connect(self.cluster_name, self.cloud_port)
# setting callbacks, use separate functions like above for better visibility
self.client.on_publish = self.on_publish
# a single publish, this can also be done in loops, etc.
connect = True
if (connect):
print("publish to connect")
self.client.publish("robots/information/7",
payload=self.cloud_message, qos=1)
else:
print("publish to disconnect")
self.client.publish("robots/information/1",
payload="Offline", qos=2)
self.client.loop_forever()
def on_connect(self, client, userdata, flags, rc, properties=None):
print("CONNACK received with code %s." % rc)
# with this callback you can see if your publish was successful
def on_publish(self, client, userdata, mid, properties=None):
print("mid: " + str(mid))
if __name__ == "__main__":
mqqq_client = MQTTPubClient()