from machine import Pin
from time import sleep
from dht import DHT22
from urequests import post
from network import WLAN, STA_IF
#Configurar sensor
dht_sensor = DHT22(Pin(15))
WEBAPP_URL = "https://script.google.com/macros/s/AKfycbzXogTXvAQ2Cn9r0Jtk41HdhHmVae_hqRnL0-hZDHM2kagWPueFQ31smfe5TqmqzzUzAg/exec"
#Configurar WiFi
def connect_wifi():
sta_if = WLAN(STA_IF)
sta_if.active(True)
sta_if.connect("Wokwi-GUEST", "")
while not sta_if.isconnected():
print(".", end="")
sleep(0.5)
print("WiFi OK IP:", sta_if.ifconfig()[0])
def send_to_sheets(temp, hum): # funcion enviar datos a la hoja de calculo
data = { #creacion de un diccionario para identificar datos y enviarlos a la URL
"temp": temp, #el valor que le va a pasar
"hum": hum
}
try:
response = post(WEBAPP_URL, json=data)
print("Respuesta: ", response.text)
response.close()
except Exception as e:
print("Error al enviar a la hoja de calculo: ", e)
connect_wifi()
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print(f"Temperatura: {temp:.1f}C")
print(f"Humedad: {hum:.1f}%")
print("-"*20)
send_to_sheets(temp, hum)
except Exception as e:
print("Error: ", e)
sleep(2)