import network,urequests,time
from machine import Pin
from time import sleep
import dht
#Sensor de temperatura
sensor= dht.DHT22(Pin(21))
def WiFi(red,passwrd):
global redM
redM = network.WLAN(network.STA_IF)
if not redM.isconnected():
redM.active(True)
redM.connect(red,passwrd)
print("Conectando",red+"...")
timeout=time.time()
while not redM.isconnected():
if(time.ticks_diff(time.time(),timeout)>10):
return False
return True
def Caracteres(mensaje):
return mensaje.replace(' ','%20').replace('°','%C2%B0').replace(':','%3A').replace('\n',"%0A")
def mensajeWhats(temp,hum):
mensaje= "💦Notificación☀️\nEstado del clima actual\n\n->Temperatura: {:.2f}\n->Humedad: {:.2f}".format(temp,hum)
mensaje_enviar= Caracteres(mensaje)
url = f"https://api.callmebot.com/whatsapp.php?phone=50499080961&text={mensaje_enviar}&apikey=5579431"
print(f"Envio del mensaje:{url}")
try:
response = urequests.get(url)
if response.status_code == 200:
print("Mensaje enviado :0")
else:
print("Error al enviar mensaje :C",response.text)
except Exception as e:
print("Error en la API: ",e)
if WiFi("Wokwi-GUEST",""):
print("***********************************************************************")
print("Conexión exitosa :D")
print("IP/NetMask/DNS: ", redM.ifconfig())
print("***********************************************************************")
print("***********************************************************************")
print("")
print("Verifique el la Temperatura y Humedad")
print("")
while True:
print("***********************************************************************")
try:
tempActu=None
humActu=None
sensor.measure()
tempNew=sensor.temperature()
humNew=sensor.humidity()
if tempActu != tempNew or humActu != humNew:
mensajeWhats(tempNew,humNew)
tempActu=tempNew
humActu=humNew
sleep(20)
except Exception as e:
print("Error en lectura o envio: ", e)
sleep(10)