import network, time
from machine import Pin
from utime import sleep, sleep_ms, ticks_us
from dht import DHT22
sensor = DHT22(Pin(2))
led_rojo = Pin(14, Pin.OUT)
def conectaWifi (red, password):
global miRed
miRed = network.WLAN(network.STA_IF)
if not miRed.isconnected(): #Si no está conectado…
miRed.active(True) #activa la interface
miRed.connect(red, password) #Intenta conectar con la red
print('Conectando a la red', red +"…")
timeout = time.time ()
while not miRed.isconnected(): #Mientras no se conecte..
if (time.ticks_diff (time.time (), timeout) > 10):
return False
return True
if conectaWifi ("Wokwi-GUEST", ""):
print ("Conexión exitosa!")
print('Datos de la red (IP/netmask/gw/DNS):', miRed.ifconfig())
while True:
sensor.measure()
tem = sensor.temperature()
hum = sensor.humidity()
print("Temperatura:{}°C, Humedad:{}%".format(tem, hum))
sleep_ms(500)
if tem>30:
led_rojo.on()
else:
led_rojo.off()
sleep(1)
else:
print ("Imposible conectar")
miRed.active (False)