-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
31 lines (22 loc) · 880 Bytes
/
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
# mqtt.py
import paho.mqtt.client as mqtt
import json
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
client.subscribe("notification_topic")
def on_message(client, userdata, msg):
payload = json.loads(msg.payload.decode())
print(f"Received message: {payload}")
process_received_message(client, payload)
def process_received_message(client, payload):
print(f"Custom processing: {payload}")
response_payload = {'response': payload}
client.publish('response_topic', json.dumps(response_payload), qos=0, retain=False)
def start_mqtt_listener():
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_forever() #client.loop_start() if using in django
if __name__ == "__main__":
start_mqtt_listener()