-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_handler.py
More file actions
90 lines (78 loc) · 3.12 KB
/
mqtt_handler.py
File metadata and controls
90 lines (78 loc) · 3.12 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# mqtt_handler.py
import paho.mqtt.client as paho
from paho import mqtt
# hivemq.webclient.1699615123087
# Hqd:<e;24G!I7wABkvL9
class MQTTHandler:
def __init__(self, broker_address, port, topic, username=None, password=None):
self.broker_address = broker_address
self.port = port
self.topic = topic
self.username = username
self.password = password
self.client = paho.Client()
# Set up callbacks
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_subscribe = self.on_subscribe
def on_connect(self, client, userdata, flags, rc, properties=None):
if rc == 0:
print("Connected to MQTT broker")
# Subscribe to the topic when connected
self.client.subscribe(self.topic)
elif rc == 1:
print("Connection refused - incorrect protocol version")
elif rc == 2:
print("Connection refused - invalid client identifier")
elif rc == 3:
print("Connection refused - server unavailable")
elif rc == 4:
print("Connection refused - bad username or password")
elif rc == 5:
print("Connection refused - not authorized")
else:
print(f"Connection failed with result code {rc}")
# def on_connect(self, client, userdata, flags, rc):
# if rc == 0:
# print("Connected to MQTT broker")
# # Subscribe to the topic when connected
# client.subscribe(self.topic)
# elif rc == 1:
# print("Connection refused - incorrect protocol version")
# elif rc == 2:
# print("Connection refused - invalid client identifier")
# elif rc == 3:
# print("Connection refused - server unavailable")
# elif rc == 4:
# print("Connection refused - bad username or password")
# elif rc == 5:
# print("Connection refused - not authorized")
# else:
# print(f"Connection failed with result code {rc}")
def on_message(self, client, userdata, message, tmp=None):
print(
"Received message "
+ str(message.payload)
+ " on topic '"
+ message.topic
+ "' with QoS "
+ str(message.qos)
)
def connect(self):
self.protocol = "paho.MQTTv5"
self.client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
try:
# Set username and password if provided
if self.username and self.password:
self.client.username_pw_set(self.username, self.password)
# Connect to the MQTT broker
self.client.connect(self.broker_address, self.port)
# Start the MQTT loop
self.client.loop_start()
except ConnectionError as e:
print(f"Error connecting to MQTT broker: {e}")
def disconnect(self):
# Disconnect from the MQTT broker
self.client.disconnect()
def on_subscribe(self, client, userdata, mid, granted_qos, properties=None):
print("Subscribed to topic!")