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
| # -*- coding: UTF-8 -*-
import serial
import tkinter as tk
def getData():
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
while True:
flag = False
if ser.in_waiting>0:
arduinoData = ser.readline().decode(encoding='utf-8', errors='ignore').rstrip().split(';')
if len(arduinoData)!=4 or arduinoData=='':
continue
for i in range(0,3):
if arduinoData[i]=='0':
flag = True
break
if flag:
continue
else:
temperature_value['text'] = arduinoData[0] + ' 度C'
humidity_value['text'] = arduinoData[1] + ' %'
pmat25_value['text'] = arduinoData[2] + ' ug/m^3'
wind_value['text'] = arduinoData[3] + ' 級'
break
window.after(1000, getData)
if __name__=='__main__':
window = tk.Tk()
window.title('Weather')
window.geometry('500x250')
window.resizable(False, False)
header_label = tk.Label(window, text='環境監測', font=('Arial', 20), width=30, height=2, borderwidth=2, relief='solid')
header_label.pack(side=tk.TOP)
temperature_frame = tk.Frame(window)
temperature_frame.pack(side=tk.TOP)
temperature_label = tk.Label(temperature_frame, text='溫度: ', font=('Arial', 16))
temperature_label.pack(side=tk.LEFT)
temperature_value = tk.Label(temperature_frame, font=('Arial', 16))
temperature_value.pack(side=tk.LEFT)
humidity_frame = tk.Frame(window)
humidity_frame.pack(side=tk.TOP)
humidity_label = tk.Label(humidity_frame, text='濕度: ', font=('Arial', 16))
humidity_label.pack(side=tk.LEFT)
humidity_value = tk.Label(humidity_frame, font=('Arial', 16))
humidity_value.pack(side=tk.LEFT)
pmat25_frame = tk.Frame(window)
pmat25_frame.pack(side=tk.TOP)
pmat25_label = tk.Label(pmat25_frame, text='PM2.5: ', font=('Arial', 16))
pmat25_label.pack(side=tk.LEFT)
pmat25_value = tk.Label(pmat25_frame, font=('Arial', 16))
pmat25_value.pack(side=tk.LEFT)
wind_frame = tk.Frame(window)
wind_frame.pack(side=tk.TOP)
wind_label = tk.Label(wind_frame, text='風速: ', font=('Arial', 16))
wind_label.pack(side=tk.LEFT)
wind_value = tk.Label(wind_frame, font=('Arial', 16))
wind_value.pack(side=tk.LEFT)
getData()
window.mainloop()
|