When I run These Code (I got from The Bored Robot In youtube) for import output from arduino IDE to plot. It's not respond, It's just show only white fleeze window.
import serial
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import csv
plt.show(block=True)
SERIAL_PORT = 'COM3'
BAUD_RATE = 9600
def handle_missing_data(data):
try:
return float(data)
except ValueError:
return 0.0
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
x_vals = []
celsius_data = []
fahrenheit_data = []
def read_and_process_data():
line = ser.readline().decode('utf-8').strip()
sensorValues = line.split(', ')
time_value = handle_missing_data(sensorValues[0])
celsius_value = handle_missing_data(sensorValues[1])
fahrenheit_value = handle_missing_data(sensorValues[2])
x_vals.append(time_value)
celsius_data.append(celsius_value)
fahrenheit_data.append(fahrenheit_value)
print(f'Time: {time_value}, Celsius: {celsius_value}, Fahrenheit: {fahrenheit_value}')
def update_plot(frame):
read_and_process_data()
plt.cla()
plt.plot(x_vals, celsius_data, label='Celsius')
plt.plot(x_vals, fahrenheit_data, label='Fahrenheit')
plt.xlabel('Time')
plt.ylabel('Temperature')
plt.legend()
def on_close(event):
with open('sensor_data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Time', 'Celsius', 'Fahrenheit'])
for x, c, f in zip(x_vals, celsius_data, fahrenheit_data):
writer.writerow([x, c, f])
fig, ax = plt.subplots()
fig.canvas.mpl_connect('close_event', on_close)
ani = FuncAnimation(fig, update_plot, interval=10)
plt.show()
And this is code in Arduino IDE
#include <Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup() {
Serial.begin(9600);
Serial.println("Adafruit MLX90614 test");
mlx.begin();
}
void loop() {
Serial.print("Ambient = ");
Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = ");
Serial.print(mlx.readObjectTempC()); Serial.println("*C");
Serial.print("Ambient = ");
Serial.print(mlx.readAmbientTempF());
Serial.print("*F\tObject = ");
Serial.print(mlx.readObjectTempF()); Serial.println("*F");
Serial.println();
delay(500);
}