-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTTClientFYP.py
54 lines (39 loc) · 1.54 KB
/
MQTTClientFYP.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
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 21:47:48 2023
@author: Reymond
"""
import paho.mqtt.client as mqtt
class MQTTClientFYP:
def __init__(self, broker_address, broker_port, topic, username, password):
self.broker_address = broker_address
self.broker_port = broker_port
self.topic = topic
self.username = username
self.password = password
self.client = mqtt.Client()
# Set the callback functions
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
# Set MQTT client username and password
self.client.username_pw_set(self.username, self.password)
# Connect to the MQTT broker
self.client.connect(self.broker_address, self.broker_port, 60)
def on_connect(self, client, userdata, flags, rc):
print("Connected to MQTT broker with result code: " + str(rc))
# Subscribe to the topic
self.client.subscribe(self.topic)
def on_message(self, client, userdata, msg):
message = msg.payload.decode("utf-8")
# print("Received message: " + message)
# Perform actions based on the received message
# if message == "on":
# print("Turning on NodeMCU-controlled device")
# elif message == "off":
# print("Turning off NodeMCU-controlled device")
def start(self):
self.client.loop_start()
def stop(self):
self.client.loop_stop()
def publish_message(self, message):
self.client.publish(self.topic, message)