see other all data is printing but furnace data is not printing by usinf this code
import serial
import openpyxl
import time
import re
import os
=== Step 1: Setup Serial Connection ===
serial_port = "COM6" # Update this to match your ESP32 port
baud_rate = 115200
try:
ser = serial.Serial(serial_port, baud_rate, timeout=1)
print(f"Connected to {serial_port} at {baud_rate} baud.")
except Exception as e:
print(f"Failed to connect to ESP32. Check the port! Error: {e}")
exit()
=== Step 2: Generate a Unique File Name for Each Experiment ===
timestamp = time.strftime("%Y%m%d_%H%M%S")
file_name = f"esp32_data_{timestamp}.xlsx"
folder_name = "Experiment_Data"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
file_path = os.path.join(folder_name, file_name)
=== Step 3: Setup New Excel File ===
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["Timestamp", "Event", "Initial Temperature (°C)", "Furnace Temperature (°C)",
"Dwell Time (s)", "Pouring Temperature (°C)", "Pouring Time (s)"])
=== Step 4: Read Data and Write to Excel ===
temperature = None
furnace_temperature = None # New variable for furnace temp
dwell_time = None
pouring_temp = None
pouring_time = None
event = None
try:
while True:
line = ser.readline().decode("utf-8", errors="ignore").strip()
print(f"Raw data: {line}")
code
if not line:
continue
# Extract Initial Temperature
if "Ladle in Furnace" in line:
temp_match = re.search(r"Temperature: (\d+\.\d+)", line)
if temp_match:
temperature = temp_match.group(1)
event = "Ladle in Furnace"
# Extract Furnace Temperature (NEW SECTION)
elif "Furnace Temperature:" in line:
furnace_match = re.search(r"Furnace Temperature: (\d+\.\d+)", line)
if furnace_match:
furnace_temperature = furnace_match.group(1)
# Extract Dwell Time
elif "Dwell Time:" in line:
dwell_match = re.search(r"Dwell Time: (\d+\.\d+)", line)
if dwell_match:
dwell_time = dwell_match.group(1)
# Extract Pouring Temperature
elif "Pouring Temperature:" in line:
pouring_temp_match = re.search(r"Pouring Temperature: (\d+\.\d+)", line)
if pouring_temp_match:
pouring_temp = pouring_temp_match.group(1)
# Extract Pouring Time
elif "Pouring Time:" in line:
pouring_time_match = re.search(r"Pouring Time: (\d+\.\d+)", line)
if pouring_time_match:
pouring_time = pouring_time_match.group(1)
# Detect cycle completion
elif "Cycle Complete" in line:
event = "Cycle Complete"
# Check if all required data is available
if all([temperature, furnace_temperature, dwell_time, pouring_temp, pouring_time]):
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
ws.append([
current_time,
event,
temperature,
furnace_temperature, # Added furnace temp
dwell_time,
pouring_temp,
pouring_time
])
wb.save(file_path)
print(f"Logged: {current_time}, {temperature}, {furnace_temperature}, {dwell_time}, {pouring_temp}, {pouring_time}")
# Reset variables for next cycle
temperature = None
furnace_temperature = None
dwell_time = None
pouring_temp = None
pouring_time = None
event = None
except KeyboardInterrupt:
print("\nStopped by User. Closing Excel file.")
wb.save(file_path)
ser.close()
Assistant
I'm sorry to hear you're experiencing issues with your code, particularly with the furnace temperature data not being printed. Let's troubleshoot this step by step:
Verify Serial Data Reception:
First, ensure that the serial data is being received correctly. Modify your code to print all incoming lines:
python
while True:
line = ser.readline().decode("utf-8", errors="ignore").strip()
print(f"Raw data: {line}")
# ... rest of your code ...
This will help confirm that data is being received and whether the furnace temperature information is present in the incoming data.
Check Data Format:
Ensure that the data sent by your device includes the "Furnace Temperature:" label exactly as expected. Serial data can sometimes include unexpected characters or formatting issues.
Adjust Regular Expression:
Your current regular expression for extracting the furnace temperature is:
This expects the temperature to be in the format of one or more digits, a decimal point, and one or more digits (e.g., "Furnace Temperature: 123.45"). If the data format differs, the regular expression might not match.
To handle potential variations, you can modify the regular expression to be more flexible:
This pattern accounts for optional spaces and allows for optional signs (positive or negative) and decimal points.
Handle Data Decoding:
Ensure that the data is being decoded correctly. If the data includes special characters or is in a different encoding, it might cause issues. You can try decoding with a different encoding or handle errors gracefully:
Test with a Minimal Script:
Create a minimal script to test the furnace temperature extraction:
python
import re
test_line = "Furnace Temperature: 123.45"
furnace_match = re.search(r"Furnace Temperature:\s*([+-]?\d*\.?\d+)", test_line)
if furnace_match:
furnace_temperature = furnace_match.group(1)
print(f"Extracted Furnace Temperature: {furnace_temperature}")
else:
print("Furnace Temperature not found.")
This will help you verify that the regular expression works as expected with sample data.
Check for Serial Port Conflicts:
Ensure that no other application (like a serial monitor) is accessing the same serial port, as this can interfere with data reception.
By following these steps, you should be able to identify and resolve the issue with the furnace temperature data not being printed.