import network, time, urequests
from machine import Pin
from utime import sleep, sleep_ms, ticks_us
#import ujson
from dht import DHT22
sensor_dht = DHT22(Pin(4))
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())
url = "https://api.thingspeak.com/update?api_key=S8G0KF9XVQDJSD5G"
while True:
sensor_dht.measure()
tem = sensor_dht.temperature()
hum = sensor_dht.humidity()
print("Tem: {}°C, Hum: {}% ".format(tem, hum))
respuesta = urequests.get(url+"&field1="+str(tem)+"&field2="+str(hum))
print(respuesta.text)
print (respuesta.status_code)
respuesta.close ()
else:
print ("Imposible conectar")
miRed.active (False)