-
Notifications
You must be signed in to change notification settings - Fork 0
/
elec291_lab3.py
118 lines (97 loc) · 3.49 KB
/
elec291_lab3.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sys
import serial
import tkinter as tk
import tkinter.messagebox as tkmb
from tkinter import ttk, simpledialog
# Global variables
maxTemp = None
xdata, ydata_celsius, ydata_derivative = [], [], []
def get_max_temperature():
global maxTemp
maxTemp = simpledialog.askfloat("Input", "Enter the max safe temperature:")
if maxTemp is not None:
print(f"Ideal max temperature set to: {maxTemp}°C")
# Start temperature monitoring and graphing after getting the ideal max temperature
start_monitoring()
#def is_safe_temperature(val):
#return 24.0 <= val <= 25.0
# configure the serial port
ser = serial.Serial(
port='COM10',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS
)
xsize = 100
def data_gen():
data_gen.t = -1
info_win = 0
previous_val = 0 # Initialize previous value
while True:
data_gen.t += 1
strin = ser.readline()
temp_val = strin[2:7]
val = float(temp_val)
print(temp_val)
# Convert temperature to Fahrenheit if needed
derivative_val = 0 if data_gen.t == 0 else (val - previous_val) / 1 # Assuming constant time interval of 1
if val > maxTemp and info_win == 0:
tkmb.showinfo("Temperature Alert", "Temperature is in an unsafe range!")
info_win = 1
if val < maxTemp-1.0 and info_win == 1:
tkmb.showinfo("Temperature Alert", "Temperature has returned to a safe range")
info_win = 0
previous_val = val
yield data_gen.t, val, derivative_val
def run(data):
t, y_celsius, y_derivative = data
if t > -1:
xdata.append(t)
ydata_celsius.append(y_celsius)
ydata_derivative.append(y_derivative)
if t > xsize:
ax.set_xlim(t - xsize, t)
line_celsius.set_data(xdata, ydata_celsius)
line_derivative.set_data(xdata, ydata_derivative)
return line_celsius, line_derivative
def on_close_figure(event):
ser.close()
sys.exit(0)
def start_monitoring():
def run(data):
t, y_celsius, y_derivative = data
if t > -1:
xdata.append(t)
ydata_celsius.append(y_celsius)
ydata_derivative.append(y_derivative)
if t > xsize:
ax.set_xlim(t - xsize, t)
line_celsius.set_data(xdata, ydata_celsius)
line_derivative.set_data(xdata, ydata_derivative)
return line_celsius, line_derivative
fig, ax = plt.subplots()
fig.canvas.mpl_connect('close_event', on_close_figure)
global line_celsius, line_derivative
line_celsius, = ax.plot([], [], lw=2, color='pink', label='Celsius')
line_derivative, = ax.plot([], [], lw=2, color='blue', label='Derivative of Celsius')
ax.set_ylim(-5, 70)
ax.set_xlim(0, xsize)
ax.grid()
ax.set_title("Temperature and Derivative of LM355 Sensor", fontsize=15)
ax.set_ylabel("Temperature and Derivative", color='black')
plt.xlabel("Time (s)", color='black')
plt.legend()
ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=100, repeat=False)
plt.show()
# Create the main window
root = tk.Tk()
root.title("Temperature Monitoring System")
# Button to set the ideal max temperature
button = tk.Button(root, text="Set Ideal Max Temperature", command=get_max_temperature)
button.pack(pady=20)
# Start the main loop
root.mainloop()