-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQTT_Dashboard.py
More file actions
335 lines (263 loc) · 10.8 KB
/
MQTT_Dashboard.py
File metadata and controls
335 lines (263 loc) · 10.8 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
import random
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import datetime as dt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from paho.mqtt import client as mqtt_client
from tkinter import *
from dotenv import load_dotenv
broker = os.getenv("MQTT_BROKER")
port = int(os.getenv("PORT"))
daily_topic = os.getenv("DAILY_TOPIC")
monthly_topic = os.getenv("MONTHLY_TOPIC")
motion_topic = os.getenv("MOTION_TOPIC")
coaster_topic = os.getenv("COASTER_TOPIC")
client_id = f'{os.getenv("CLIENT_ID")}-{random.randint(0,1000)}'
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
device_id = os.getenv("DEVICE_ID")
with open("daily_count.txt","r") as d_file:
daily_count = int(d_file.read())
d_file.close
with open("monthly_count.txt", "r") as m_file:
monthly_count = int(m_file.read())
m_file.close()
motion_data = 0
coaster_data = 0
lst = []
water_frames = []
img_x = 0
img_y = 0
# MQTT PROTOCOLS -------------------------------------------------------
def connect_mqtt():
def on_connect(client, userdata, flags, rc, properties):
if rc==0:
print("successfully connected to MQTT broker")
else:
print("failed to connect")
client = mqtt_client.Client(client_id=client_id, callback_api_version=mqtt_client.CallbackAPIVersion.VERSION2)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client,update_ui, day_complete):
def on_message(client, userdata, msg):
global daily_count
global monthly_count
global motion_data
global coaster_data
if msg.topic != motion_topic and msg.topic != coaster_topic:
valid_message = False
allowed_msgs = list(range(0,32)) # needs to read up to 31 to allow for the reset to happen
allowed_msgs =[str(i) for i in allowed_msgs]
# check the recieved message against each value
for i in allowed_msgs:
if msg.payload.decode() == i:
valid_message = True
break
else:
valid_message = False
# if the message is allowed determine what to do with it
if valid_message:
print(f"recieved '{msg.payload.decode()}' from '{msg.topic}' topic")
if msg.topic == daily_topic:
# to avoid an error with the animation frames
if int(msg.payload.decode()) < 9:
with open("daily_count.txt", "w") as d_file:
# update the txt file to hold the count between reruns of the script
d_file.write(msg.payload.decode())
daily_count = int(msg.payload.decode())
d_file.close()
update_ui(daily_count)
# does not need an additional check
if msg.topic == monthly_topic:
with open("monthly_count.txt", "w") as m_file:
m_file.write(msg.payload.decode())
monthly_count = int(msg.payload.decode())
m_file.close()
day_complete()
elif msg.topic == motion_topic:
valid_message = False
allowed_msgs = list(range(0,31))
allowed_msgs =[str(i) for i in allowed_msgs]
# check the recieved message against each value
for i in allowed_msgs:
if msg.payload.decode() == i:
valid_message = True
break
else:
valid_message = False
if valid_message:
motion_data = int(msg.payload.decode())
else:
valid_message = False
allowed_msgs = list(range(-15,5000))
allowed_msgs =[str(i) for i in allowed_msgs]
for i in allowed_msgs:
if msg.payload.decode() == i:
valid_message = True
break
else:
valid_message = False
if valid_message:
coaster_data = int(msg.payload.decode())
client.subscribe(daily_topic)
client.subscribe(monthly_topic)
client.subscribe(motion_topic)
client.subscribe(coaster_topic)
client.on_message = on_message
def publish(client,topic,msg, retain):
result = client.publish(topic,msg, retain=True)
status = result[0]
if status == 0:
print(f"send '{msg}' to topic '{topic}'")
else:
print("failed")
if topic == daily_topic:
with open("daily_count.txt","w") as d_file:
d_file.write(str(msg))
d_file.close()
if topic == monthly_topic:
with open("monthly_count.txt","w") as m_file:
m_file.write(str(msg))
m_file.close()
# ---------------------------------------------------------------------
def inc_daily():
global daily_count
daily_count+=1
def inc_monthly():
global monthly_count
monthly_count+=1
# UI CREATION ----------------------------------------------------------
def create_window(client):
global daily_count
window = Tk()
window.title("MQTT Dashboard")
window.geometry('325x700')
window.resizable(False,False)
# background image
canvas = Canvas(window, width=325, height=700, borderwidth=0, highlightthickness=0)
background_img = PhotoImage(file="assets/background.png")
canvas.create_image(0,0, anchor=NW, image=background_img)
frame_img = PhotoImage(file=f"assets/F{daily_count}.png")
water_frames.append(frame_img)
canvas.create_image(22*5,99*5, anchor=NW, image=frame_img)
#reset button for testing
reset_img = PhotoImage(file="assets/reset_button.png")
reset_btn = Button(window, image = reset_img,command=lambda:day_end(), borderwidth=0, highlightthickness=0)
reset_btn.imgref = reset_img
reset_btn.place(x=55*5,y=131*5)
# garbage collection
canvas.imgref = background_img
# update the water bottles animation frame
def update_ui(daily_count):
frame_img = PhotoImage(file=f"assets/F{daily_count}.png")
water_frames.append(frame_img)
canvas.create_image(22*5,99*5, anchor=NW, image=frame_img)
# recreate what would be performed at midnight
def day_end():
global daily_count
daily_count = 0
publish(client, daily_topic, daily_count, True)
# carry out with every button click
def glass_finished(client):
global daily_count
global monthly_count
if daily_count <= 8:
publish(client, daily_topic, daily_count, True)
if daily_count == 8:
inc_monthly()
publish(client, monthly_topic, monthly_count, True)
# carried out if all 8 glasses were finished in a day
def day_complete():
global monthly_count
active_img = PhotoImage()
canvas.create_image(0,0,image=active_img)
# store each of the x,y coords of the shelf items
pos_dict = {"img0":(0,13),"img1":(11,19),"img2":(22,17),"img3":(32,19),"img4":(46,20),"img5":(60,20),
"img6":(0,39),"img7":(6,37),"img8":(23,39),"img9":(29,38),"img10":(46,42),"img11":(57,35),
"img12":(0,59),"img13":(6,59),"img14":(22,59),"img15":(33,57),"img16":(47,63),"img17":(54,57),
"img18":(1,79),"img19":(0,85),"img20":(23,85),"img21":(32,84),"img22":(45,84),"img23":(56,81),
"img24":(6,100),"img25":(0,107),"img26":(45,101),"img27":(60,105),
"img28":(0,122),"img29":(47,122)}
if monthly_count < 31: # at 31 to allow a roll over for reset
for x in range(monthly_count):
active_img = PhotoImage(file=f"assets/img_{x}.png")
lst.append(active_img)
values = pos_dict.get(f"img{x}", [])
img_x = values[0]
img_y = values[1]
canvas.create_image(img_x*5,img_y*5, anchor=NW, image=active_img)
else:
# reset the count
monthly_count = 0
publish(client,monthly_topic, monthly_count, True)
# button creation
img = PhotoImage(file="assets/button.png")
btn = Button(window, image=img, command=lambda:[inc_daily(), glass_finished(client)], borderwidth=0, highlightthickness=0)
btn.imgref = img
btn.place(x=100,y=610)
motion_graph = createMotionGraph()
coaster_graph = createCoasterGraph()
canvas.pack()
return window, update_ui, day_complete, motion_graph, coaster_graph
# --------------------------------------------------------------------------------
def createMotionGraph():
# purely used to display motion sensor information - wouldnt be seen by the user.
graph_window = Toplevel()
graph_window.title('Motion Sensor Data')
graph_window.geometry('600x600+1400+0')
fig = Figure(figsize=(9,9),dpi=100)
plot1 = fig.add_subplot(111)
xs = []
ys = []
def animate(i, xs, ys):
global motion_data
xs.append(dt.datetime.now().strftime('%H:%M:%S'))
ys.append(motion_data)
xs = xs[-10:]
ys = ys[-10:]
plot1.clear()
plot1.plot(xs,ys)
plt.setp(plot1.get_xticklabels(), rotation=45)
canvas = FigureCanvasTkAgg(fig, master=graph_window)
ani = animation.FuncAnimation(fig,animate, fargs=(xs,ys),interval=1000, cache_frame_data=False)
plt.show()
canvas.draw()
canvas.get_tk_widget().pack()
return graph_window
# --------------------------------------------------------------------------------
def createCoasterGraph():
graph_window = Toplevel()
graph_window.title('Coaster Data')
graph_window.geometry('600x600+750+0')
fig = Figure(figsize=(9,9),dpi=100)
plot1 = fig.add_subplot(111)
xs = []
ys = []
def animate(i, xs, ys):
global motion_data
xs.append(dt.datetime.now().strftime('%H:%M:%S'))
ys.append(motion_data)
xs = xs[-10:]
ys = ys[-10:]
plot1.clear()
plot1.plot(xs,ys)
plt.setp(plot1.get_xticklabels(), rotation=45)
canvas = FigureCanvasTkAgg(fig, master=graph_window)
ani = animation.FuncAnimation(fig,animate, fargs=(xs,ys),interval=1000, cache_frame_data=False)
plt.show()
canvas.draw()
canvas.get_tk_widget().pack()
return graph_window
# MAIN -----------------------------------------------------------
def main():
client = connect_mqtt()
client.loop_start()
window, update_ui, day_complete, motion_graph, coaster_graph = create_window(client)
subscribe(client, update_ui, day_complete)
window.mainloop()
client.loop_stop()
if __name__ == '__main__':
main()