-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_sub_test1.py
143 lines (121 loc) · 3.59 KB
/
mqtt_sub_test1.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# =============================================================================
# imported libraries
from paho.mqtt import client as mqtt
import time
import colorama
from colorama import Fore
import random
# =========================a====================================================
# Global constants
# => To referesh the cmd.
colorama.init(autoreset=True)
tcp_port = 1883
tcp_ssl_port = 8883
# =============================================================================
# Basic configurations
client_id = f'subscriber-client-id-{random.randint(0, 127)}'
broker_address = 'mqtt.eclipseprojects.io'
port = tcp_port
Qos = 2
username = f'mohamed-ashraf-wx'
password = f'mohamed-ashraf'
# Subscriber / Publisher Configurations
topic = 'mqtt_testing_python/chat_app/test1'
# =============================================================================
# Functions implementation
'''
@brief Function to print the status on connection.
'''
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(f"{Fore.GREEN}Connection to the broker {Fore.WHITE}`{broker_address}` successful.")
else:
print(f"{Fore.RED}Connection to the broker {Fore.WHITE}`{broker_address}` failed.")
'''
@brief Function to print the incoming message from the broker.
'''
def on_message(client, userdata, msg):
global message
message = msg.payload.decode('ascii')
topic = msg.topic
print(f'{Fore.GREEN}Received {Fore.WHITE}`{message}` {Fore.GREEN}on topic {Fore.WHITE}`{topic}`')
'''
@brief Callback Function
'''
def on_publish(client, userdata, mid):
pass
'''
@brief Callback Function
'''
def on_subscribe(client, userdata, mid, granted_qos):
pass
'''
@brief Callback Function
'''
def on_unsubscribe(client, userdata, mid):
pass
'''
@brief Callback Function
'''
def on_disconnect(client, userdata, mid):
pass
'''
@brief Function to setup the basic configuration
'''
def mqtt_set_cfgs(
client_id=None,
broker='mqtt.eclipseprojects.io',
port=1883,
Qos=2,
username=None,
password=None):
# Set up the basic configurations
client = mqtt.Client(client_id)
client.username_pw_set(username, password)
# Callbacks
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_unsubscribe = on_unsubscribe
# Connect to the broker
client.connect(broker_address, port, Qos)
return client
'''
@brief Function to setup the connection between the client & broker.
'''
def connect_mqtt():
# Setting the configuration.
client = mqtt_set_cfgs(
client_id,
broker_address,
port,
Qos,
username,
password)
return client
'''
@brief Function to subscribe to a topic to receive its messages.
'''
def subscribe(client=None):
# Subscribe to the topic
client.subscribe(topic)
client.on_message = on_message
'''
@brief Function to set and run the application.
'''
def run_app():
try:
client = connect_mqtt()
subscribe(client)
client.loop_forever()
except:
client.disconnect()
client.loop_stop()
print(f'{Fore.GREEN}Disconnected from broker {Fore.WHITE}`{broker_address}` - {Fore.GREEN}Client {Fore.WHITE}`{client_id}`')
# =============================================================================
# Program entry point
# Run the application from the entry point.
if __name__ == "__main__":
# Run the MQTT application.
run_app()