-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest2.py
176 lines (127 loc) · 6.35 KB
/
test2.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json
import paho.mqtt.client as mqtt
from tkinter import *
from tkinter import ttk
_SUB = ""
_PUB = ""
class MqttClient(object):
def __init__(self):
self.client = mqtt.Client()
self.subscription_topic_name = None
self.publish_topic_name = None
self.callback = None
def connect(self, subscription_topic_name, publish_topic_name,mqtt_broker_ip_address="broker.emqx.io",use_off_campus_broker=False):
# mqtt_broker_ip_address = "test.mosquitto.org"
# mqtt_broker_ip_address = "broker.hivemq.com"
mqtt_broker_ip_address = "broker.emqx.io"
self.subscription_topic_name = subscription_topic_name
self.publish_topic_name = publish_topic_name
# Callback for when the connection to the broker is complete.
self.client.on_connect = self._on_connect
self.client.message_callback_add(self.subscription_topic_name, self._on_message)
print("Connecting to mqtt broker {}".format(mqtt_broker_ip_address), end="")
self.client.connect(mqtt_broker_ip_address)
# self.client.connect(mqtt_broker_ip_address, 1883, 60)
self.client.loop_start()
def send_message(self, type_name, payload=None):
message_dict = {"type": type_name}
if payload is not None:
message_dict["payload"] = payload
message = json.dumps(message_dict)
self.client.publish(self.publish_topic_name, message)
def _on_connect(self, client, userdata, flags, rc): # called when broker responds to our connection request
if rc == 0:
print(" ... Connected!")
else:
print(" ... Error!!!")
exit()
print("Publishing to topic:", self.publish_topic_name)
self.client.on_subscribe = self._on_subscribe
# Subscribe to topic(s)
self.client.subscribe(self.subscription_topic_name)
def _on_subscribe(self, client, userdata, mid, granted_qos): # called when broker responds to clients subscribe request
print("Subscribed to topic:", self.subscription_topic_name)
def _on_message(self, client, userdata, msg): # called when a message has been recieved on a topic that the client has subscribed to.
message = msg.payload.decode()
if not self.callback:
print("Missing a callback")
return
# Attempt to parse the message and call the appropriate function.
try:
message_dict = json.loads(message)
except ValueError:
return
if "type" not in message_dict:
return
message_type = message_dict["type"]
message_payload = None
if "payload" in message_dict:
message_payload = message_dict["payload"]
self.callback(message_type, message_payload)
def close(self):
self.callback = None
self.client.loop_stop()
self.client.disconnect()
def Page2():
# global _PUB,_SUB
publisher = player1_entry.get()
subscriber = player2_entry.get()
# print(publisher,subscriber)
window.destroy()
# MQTT on_message callback (use via a lambda function below)
def example_mqtt_callback(type_name, payload, chat_window):
print("Received message payload: ", payload)
chat_window["text"] += "\nFrom "+ str(type_name) + " : " + payload
# Tkinter callbacks
def send_message(mqtt_client, chat_window, msg_entry,publisher="chat"):
msg = msg_entry.get()
msg_entry.delete(0, 'end')
chat_window["text"] += "\nMe: " + msg
mqtt_client.send_message(publisher, msg) # Sending MQTT message
def quit_program(mqtt_client):
mqtt_client.close()
exit()
root = Tk()
root.title("Chat App using paho Python(MQTT protocol)")
s = ttk.Style()
s.configure('My.TFrame', background='cyan',foreground='green')
s.configure('My.TButton', font=('Helvetica', 12))
s.configure('My.TLabel', font=('Helvetica', 18))
main_frame = ttk.Frame(root, padding=50, relief='raised',style='My.TFrame')
main_frame.grid()
label = ttk.Label(main_frame,foreground="white",background="black", justify=LEFT, text="Send a message",style='My.TLabel')
label.grid(columnspan=10,padx=10,pady=10)
msg_entry = ttk.Entry(main_frame, width=60,style='My.TLabel',font=('Arial', 18, 'bold'))
msg_entry.grid(row=10, column=0,padx=10,pady=10,ipadx=10,ipady=10)
msg_button = ttk.Button(main_frame, text="Send",style='My.TButton')
msg_button.grid(row=10, column=20,padx=10,pady=10)
msg_button['command'] = lambda: send_message(mqtt_client, chat_window, msg_entry,publisher)
label = ttk.Label(main_frame,foreground="white",background="black", justify=LEFT, text="HISTORY",style='My.TLabel')
label.grid(columnspan=10,padx=10,pady=10)
chat_window = ttk.Label(main_frame,padding=20, justify=LEFT, text="", width=60, wraplength="500p",style='My.TLabel')
chat_window.grid(columnspan=10,padx=10,pady=10)
q_button = ttk.Button(main_frame, text="Quit",style='My.TButton')
q_button.grid(row=20, column=20,padx=10,pady=10)
q_button['command'] = (lambda: quit_program(mqtt_client))
mqtt_client = MqttClient()
mqtt_client.callback = lambda type, payload: example_mqtt_callback(type, payload, chat_window)
mqtt_client.connect(publisher, subscriber, use_off_campus_broker=True) # "Send to" and "listen to" the same topic
root.mainloop()
if __name__ == "__main__":
print("MQTT Application Window 1")
# Driver Code
window = Tk()
window.title("PY - MQTT")
Label(window,text = 'Welcome To Chat App',font=('sans sherif bold', 25),bg = 'black' , fg = 'white').place(x = 220 , y = 20)
Label(window, text='Topic 1',font=('sans sherifbold', 19),bg = 'black' , fg = 'red').place(x=180, y=180)
player1_entry = Entry(window,width=20,font=('Courier New bold italic' , 14) , fg = 'red', highlightthickness = '1' , highlightcolor = 'red')
player1_entry.focus_set()
player1_entry.place(x=350, y=185)
#player1 = player1_entry.get()
Label(window, text='Topic 2', font=('sans sherif', 19),bg = 'black' , fg = 'green').place(x=180, y=250)
player2_entry = Entry(window, width=20 ,font=('Courier New bold italic' , 14), highlightthickness = '1' , highlightcolor = 'green' , fg = 'green')
player1_entry.bind('<Return>', lambda event :player2_entry.focus_set())
player2_entry.place(x=350, y=255)
Button(window, text='Submit', command= Page2,font=('sans sherif', 16),bg = 'black' , fg = 'white').place(x=350, y=325)
window.geometry('700x500+150+100')
window.mainloop()