import network
import utime
import uasyncio as asyncio
from machine import Pin, I2C
import socket
# Configuración AP
AP_SSID = "esotilin"
AP_PASS = "12345678"
# Configuración I2C y Keypad
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
filas_pines = [Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT)]
columnas_pines = [Pin(6, Pin.IN, Pin.PULL_UP), Pin(7, Pin.IN, Pin.PULL_UP),
Pin(8, Pin.IN, Pin.PULL_UP), Pin(9, Pin.IN, Pin.PULL_UP)]
teclas = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
r, g, b = 0, 0, 0
editando = None
ultimo_pulso = 0
class LCD:
def __init__(self, i2c, addr=0x27):
self.i2c = i2c
self.addr = addr
self.backlight = 0x08
self.init_lcd()
def enviar(self, data, mode=0):
high = mode | (data & 0xF0) | self.backlight
low = mode | ((data << 4) & 0xF0) | self.backlight
self.i2c.writeto(self.addr, bytes([high, high | 0x04, high & ~0x04]))
utime.sleep_ms(1)
self.i2c.writeto(self.addr, bytes([low, low | 0x04, low & ~0x04]))
utime.sleep_ms(1)
def init_lcd(self):
for cmd in [0x03,0x03,0x03,0x02,0x28,0x0C,0x06,0x01]:
self.enviar(cmd)
utime.sleep_ms(5 if cmd == 0x01 else 2)
def limpiar(self):
self.enviar(0x01)
utime.sleep_ms(2)
def escribir_linea(self, text, row=0):
self.enviar(0x80 | (row * 0x40))
text = text + " " * 16
for char in text[:16]:
self.enviar(ord(char), 1)
lcd = LCD(i2c)
def leer_keypad():
global ultimo_pulso
now = utime.ticks_ms()
if now - ultimo_pulso < 200:
return None
for r_idx in range(4):
for i in range(4):
filas_pines[i].value(0 if i == r_idx else 1)
for c in range(4):
if columnas_pines[c].value() == 0:
tecla = teclas[r_idx][c]
ultimo_pulso = now
for i in range(4): filas_pines[i].value(1)
return tecla
for i in range(4): filas_pines[i].value(1)
return None
def actualizar_lcd():
lcd.limpiar()
lcd.escribir_linea(f"R:{r:3}G:{g:3}B:{b:3}", 0)
if editando:
lcd.escribir_linea(f"Editando {editando}...", 1)
else:
lcd.escribir_linea("A=R B=G C=B", 1)
def enviar_estacion(r_val, g_val, b_val):
try:
target_ip = "192.168.4.16"
s = socket.socket()
s.settimeout(2) # Para que no se bloquee si no hay nadie escuchando
s.connect((target_ip, 80))
msg = f"RGB:{r_val},{g_val},{b_val}"
s.send(msg.encode())
s.close()
print(f"Enviado: {msg}")
except Exception as e:
print("Error de conexión:", e)
def procesar_tecla(tecla):
global r, g, b, editando
if editando:
if tecla in ['A', 'B', 'C']:
if tecla == editando:
editando = None
enviar_estacion(r, g, b)
return
if tecla in ['*', '#']: return
if tecla == 'D': # Borrar último dígito
if editando == 'A': r //= 10
elif editando == 'B': g //= 10
elif editando == 'C': b //= 10
elif tecla.isdigit():
val = int(tecla)
if editando == 'A': r = min(255, (r * 10) + val)
elif editando == 'B': g = min(255, (g * 10) + val)
elif editando == 'C': b = min(255, (b * 10) + val)
else: # Modo Standby
if tecla in ['A', 'B', 'C']:
editando = tecla
if tecla == 'A': r = 0
elif tecla == 'B': g = 0
elif tecla == 'C': b = 0
elif tecla == '*': # Apagado
r = g = b = 0
enviar_estacion(r, g, b)
elif tecla == '#': # Blanco
r = g = b = 255
enviar_estacion(r, g, b)
actualizar_lcd()
async def main():
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
wlan.config(essid=AP_SSID, password=AP_PASS)
print(f"AP creado: {AP_SSID}. IP: {wlan.ifconfig()[0]}")
lcd.limpiar()
lcd.escribir_linea("eso tilin", 0)
utime.sleep(2)
actualizar_lcd()
while True:
key = leer_keypad()
if key:
procesar_tecla(key)
await asyncio.sleep_ms(50)
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Programa detenido")
Loading
pi-pico-w
pi-pico-w