-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.py
71 lines (56 loc) · 1.68 KB
/
subscriber.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
67
68
69
70
71
# python 3.11
import random
import os
from paho.mqtt import client as mqtt_client
from dotenv import load_dotenv
from colorama import Fore
load_dotenv()
broker = os.getenv("BROKER")
port = int(os.getenv("PORT"))
topic = os.getenv("TOPIC")
if os.getenv("USER_NAME") is None or os.getenv("USER_NAME") == "":
user_name = None
pass_word = None
else:
user_name = os.getenv("USER_NAME")
pass_word = os.getenv("PASSWORD")
# Generate a Client ID with the publish prefix.
client_id = f"publish-{random.randint(0, 1000)}"
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(user_name, pass_word)
client.on_connect = on_connect
client.connect(broker, port)
# client.connect_async(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
try:
print(
Fore.WHITE
+ "TOPIC: "
+ Fore.LIGHTYELLOW_EX
+ msg.topic
+ Fore.WHITE
+ "\tDATA: "
+ Fore.GREEN
+ msg.payload.decode("utf-8", "ignore")
)
except Exception as e:
print("there was a problme processing this message", exc_info=e)
client.subscribe(topic)
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
try:
client.loop_forever()
except:
print("application has shutdown")
if __name__ == "__main__":
run()