import network
import time
import dht
from machine import Pin
import urequests
import json
sensor = dht.DHT22(Pin(4))
ssid = 'Wokwi-GUEST'
password = ''
def conectar_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
print('Conectando a WiFi...')
time.sleep(1)
print('Conectado a WiFi')
conectar_wifi()
url = "http://8e1b-34-16-148-52.ngrok-free.app"
def enviar_datos():
try:
sensor.measure()
temperatura = sensor.temperature()
humedad = sensor.humidity()
print(f"Temperatura: {temperatura}°C, Humedad: {humedad}%")
data = {
"temperatura": temperatura,
"humedad": humedad
}
json_data = json.dumps(data)
response = urequests.post(url, data=json_data, headers={'Content-Type': 'application/json'})
if response.status_code == 200:
print("Datos enviados correctamente")
else:
print(f"Error al enviar los datos: {response.status_code}")
response.close()
except OSError as e:
print("Error al leer el sensor:", e)
while True:
enviar_datos()
time.sleep(5)