from machine import Pin, PWM, ADC
import time
import network
from umqtt.simple import MQTTClient
# Configuración WiFi
ssid = 'Wokwi-GUEST'
wifipassword = ''
# Configuración de la conexión WiFi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
# Conectamos al WiFi
sta_if.connect(ssid, wifipassword)
print("Conectando")
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print("Conectado a Wifi!")
# Vemos cuales son las IP
print(sta_if.ifconfig())
# Configuración MQTT
mqtt_server = 'io.adafruit.com'
port = 1883
user = 'aleexzz'
password = 'caca12345'
client_id = 'LUZRGB'
topic_LED_RGB = 'aleexzz/feeds/LED_RGB'
topic_RED_POT = 'aleexzz/feeds/SLIDERr'
topic_GREEN_POT = 'aleexzz/feeds/SLIDERg'
topic_BLUE_POT = 'aleexzz/feeds/SLIDERb'
# Configuración LED RGB
pin_red = 23
pin_green = 22
pin_blue = 21
led_red = PWM(Pin(pin_red), freq=1000, duty=0)
led_green = PWM(Pin(pin_green), freq=1000, duty=0)
led_blue = PWM(Pin(pin_blue), freq=1000, duty=0)
# Configuración potenciómetros
pot_red_pin = 25
pot_green_pin = 26
pot_blue_pin = 27
pot_red = ADC(Pin(pot_red_pin))
pot_green = ADC(Pin(pot_green_pin))
pot_blue = ADC(Pin(pot_blue_pin))
# Función para obtener el valor normalizado del potenciómetro (de 0 a 1023)
def get_normalized_pot_value(pot):
value = pot.read_u16() >> 4
return min(value, 1023)
# Función para establecer el color del LED RGB según los valores de los potenciómetros
def set_rgb_color_from_potentiometers():
r = min(get_normalized_pot_value(pot_red), 1023)
g = min(get_normalized_pot_value(pot_green), 1023)
b = min(get_normalized_pot_value(pot_blue), 1023)
# Ajusta la lógica de inversión según sea necesario
r = 1023 - r
g = 1023 - g
b = 1023 - b
# Escala los valores para ajustarse al rango de duty (0-1023)
r = int((r / 1023) * 1023)
g = int((g / 1023) * 1023)
b = int((b / 1023) * 1023)
# Establece el color del LED RGB
led_red.duty(r)
led_green.duty(g)
led_blue.duty(b)
# Función de callback para el tópico POTENTIOMETER
def callback_red_pot(topic, msg):
pot_value = int(msg.decode('utf-8'))
pot_red.write_u16(pot_value)
set_rgb_color_from_potentiometers()
def callback_green_pot(topic, msg):
pot_value = int(msg.decode('utf-8'))
pot_green.write_u16(pot_value)
set_rgb_color_from_potentiometers()
def callback_blue_pot(topic, msg):
pot_value = int(msg.decode('utf-8'))
pot_blue.write_u16(pot_value)
set_rgb_color_from_potentiometers()
# Intenta conectarse al broker MQTT
try:
mqtt_client = MQTTClient(client_id, mqtt_server, user=user, password=password, port=port)
mqtt_client.set_callback(callback_potentiometer)
mqtt_client.connect()
mqtt_client.subscribe(topic_SLIDER_RED)
mqtt_client.subscribe(topic_SLIDER_GREEN)
mqtt_client.subscribe(topic_SLIDER_BLUE)
print("Conectado con el Broker MQTT")
except OSError as e:
print("Fallo la conexión al Broker MQTT, reiniciando...")
time.sleep(5)
machine.reset()
# Bucle principal
while True:
try:
# Chequea por mensajes nuevos y ejecuta la función de callback
mqtt_client.check_msg()
# Actualiza el color del LED RGB basado en los potenciómetros
set_rgb_color_from_potentiometers()
# Publica el estado actual del LED RGB
mqtt_client.publish(topic_LED_RGB, f"{led_red.duty()},{led_green.duty()},{led_blue.duty()}")
time.sleep(0.1) # Ajusta el tiempo de espera según sea necesario
except OSError as e:
print("Error", e)
time.sleep(5)
machine.reset()