# hola led con interface en pc
# by Jorge Anzaldo
from machine import Pin
import sys
import time
led = Pin(4, Pin.OUT) # Configura el LED en GPIO2
print("Listo para recibir comandos por USB (COM3)...")
while True:
# Leer una línea desde el puerto serial USB
comando = sys.stdin.readline().strip().upper() # .readline() espera hasta recibir '\n'
if comando == 'ON':
led.on()
print("LED encendido")
elif comando == 'OFF':
led.off()
print("LED apagado")
else:
print("Comando no reconocido:", comando)
'''
codigo en pc
import tkinter as tk
import serial
import time
PORT = 'COM3'
BAUDRATE = 115200
try:
esp32 = serial.Serial(PORT, BAUDRATE, timeout=1)
time.sleep(2) # Espera a que el ESP32 se inicialice
except Exception as e:
print(f"Error: {e}")
esp32 = None
def encender_led():
if esp32:
esp32.write(b'ON\n') # ¡Importante: el '\n' final!
print("Encendiendo LED")
def apagar_led():
if esp32:
esp32.write(b'OFF\n') # ¡Importante: el '\n' final!
print("Apagando LED")
ventana = tk.Tk()
ventana.title("Control LED ESP32")
btn_on = tk.Button(ventana, text="Encender", command=encender_led)
btn_on.pack()
btn_off = tk.Button(ventana, text="Apagar", command=apagar_led)
btn_off.pack()
ventana.mainloop()
'''