-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_pub_test1.py
149 lines (125 loc) · 3.79 KB
/
mqtt_pub_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
144
145
146
147
148
# =============================================================================
# imported libraries
from paho.mqtt import client as mqtt
import time
import colorama
from colorama import Fore
import random
# =============================================================================
# Global constants
# => To referesh the cmd.
colorama.init(autoreset=True)
tcp_port = 1883
tcp_ssl_port = 8883
# =============================================================================
# Basic configurations
client_id = f'publisher-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 Callback 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 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='0',
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 publish (send) a message to the broker
with the specified topic.
'''
def publish(client=None, topic=None, message=None):
# Publish the message
result = client.publish(topic, message)
# Get the status
status = result[0]
if status == 0:
print(f"{Fore.GREEN} Published {Fore.WHITE}`{message}` {Fore.GREEN}on Topic {Fore.WHITE}`{topic}`")
else:
print(f"{Fore.RED} Failed to Publish {Fore.WHITE}`{message}` {Fore.RED}on Topic {Fore.WHITE}`{topic}`")
firmware_lines = []
'''
@brief Function to set and run the application.
'''
def run_app():
try:
client = connect_mqtt()
client.loop_start()
time.sleep(1)
while True:
#msg = str(input("Enter: "))
msg = "Hello!"
publish(client, topic, msg)
time.sleep(0.5)
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()