import serial
import sys
import time
PORT = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyUSB0"
DURATION = int(sys.argv[2]) if len(sys.argv) > 2 else 30
BAUD = 115200
ser = serial.Serial(PORT, BAUD, timeout=1)
ser.dtr = False
ser.rts = True
time.sleep(0.1)
ser.rts = False
print(f"=== ESP32 Monitor ({PORT}) - {DURATION}s ===")
start = time.time()
while time.time() - start < DURATION:
if ser.in_waiting:
try:
line = ser.readline().decode('utf-8', errors='replace').strip()
if line:
print(line)
except Exception as e:
print(f"Error: {e}")
ser.close()
print("=== End ===")