#Cruz Patino Diego
#22210297
from machine import Pin, I2C
import ssd1306
import network
import urequests
import time
# 📶 Conectar a Wi-Fi
def conectar_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print("Conectado a WiFi:", wlan.ifconfig())
conectar_wifi("TecNM-ITT", "") # ← Cambia esto
# 🧠 Funciones para APIs
def obtener_hora_cdmx():
try:
res = urequests.get("http://worldtimeapi.org/api/timezone/America/Mexico_City")
datos = res.json()
return datos["datetime"][11:19]
except:
return "Error"
def obtener_clima_cdmx():
try:
url = "https://api.open-meteo.com/v1/forecast?latitude=19.4326&longitude=-99.1332¤t_weather=true"
res = urequests.get(url)
datos = res.json()
temp = datos["current_weather"]["temperature"]
code = datos["current_weather"]["weathercode"]
return temp, code
except:
return None, None
def descripcion_clima(code):
codigos = {
0: "Despejado", 1: "Sol", 2: "Parcial", 3: "Nublado",
45: "Niebla", 48: "Niebla densa", 51: "Lluvia leve",
61: "Lluvia", 80: "Lluvia fuerte"
}
return codigos.get(code, "Desconocido")
# 🖥️ Configurar OLED
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# 🎛️ Botones
btn_up = Pin(2, Pin.IN, Pin.PULL_UP)
btn_down = Pin(3, Pin.IN, Pin.PULL_UP)
btn_select = Pin(4, Pin.IN, Pin.PULL_UP)
btn_back = Pin(5, Pin.IN, Pin.PULL_UP)
# 📋 Menú
menu = ["1. Temperatura", "2. Clima", "3. Hora", "4. OFF"]
pos = 0
def mostrar_menu():
oled.fill(0)
oled.text("Menu:", 0, 0)
for i in range(3):
idx = (pos + i) % len(menu)
pref = ">" if i == 0 else " "
oled.text(f"{pref} {menu[idx]}", 0, (i+1)*10)
oled.show()
mostrar_menu()
# 🔁 Loop principal
while True:
if not btn_down.value():
pos = (pos + 1) % len(menu)
mostrar_menu()
time.sleep(0.25)
elif not btn_up.value():
pos = (pos - 1) % len(menu)
mostrar_menu()
time.sleep(0.25)
elif not btn_select.value():
seleccion = menu[pos]
if "Temperatura" in seleccion or "Clima" in seleccion:
temp, code = obtener_clima_cdmx()
clima_txt = descripcion_clima(code)
oled.fill(0)
oled.text("CDMX", 0, 0)
oled.text("Temp: {} C".format(temp), 0, 15)
oled.text(clima_txt, 0, 30)
oled.show()
time.sleep(4)
elif "Hora" in seleccion:
hora = obtener_hora_cdmx()
oled.fill(0)
oled.text("Hora CDMX:", 0, 10)
oled.text(hora, 0, 25)
oled.show()
time.sleep(4)
elif "OFF" in seleccion:
oled.fill(0)
oled.text("Apagando...", 0, 25)
oled.show()
time.sleep(2)
break
mostrar_menu()
elif not btn_back.value():
oled.fill(0)
oled.text("Saliendo...", 0, 25)
oled.show()
time.sleep(2)
break