import machine
import dht
import time
import micropython
import network #Para comunicarnos por la red
from machine import Pin
from machine import PWM
from umqtt.simple import MQTTClient #Conexion al broker MQTT
#--------------------------------------------------------------#
#----------------indicamos red WIFI y clave--------------------#
ssid = 'Wokwi-GUEST' #Red simulada de wokwi
wifipassword = ''
#----------------Datos Server MQTT (Broker)--------------------#
#-------Indicamos datos MQTT Broker (server y puerto)----------#
mqtt_server = 'io.adafruit.com'
port = 1883
user = 'TomasYagueddu' #definido en adafruit
password = 'aio_pAjY61eAxVLCBxsx2lFJcZ9yhDIr' #key adafruit
#---------------Indicamos ID(unico) y topicos------------------#
client_id = '44100611'
topic_ALARMA = 'TomasYagueddu/feeds/ALARMA'
topic_TEMPERATURA = 'TomasYagueddu/feeds/TEMPERATURA'
topic_HUMEDAD = 'TomasYagueddu/feeds/HUMEDAD'
#--------------------------------------------------------------#
#--------------------------------------------------------------#
#----------Variable que define si la alarma esta activa--------#
ALARMA_ACTIVA = False
LedALARMA = Pin(18,Pin.OUT)
#--------------------------------------------------------------#
#--------------------------------------------------------------#
#---Definimos modo Station (conectarse a Access Point remoto)--#
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True) #enciende el modulo wifi
#---------------------Conectamos al wifi-----------------------#
sta_if.connect(ssid, wifipassword)
print("Conectando")
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print("Conectado a Wifi!")
#----------------- Vemos cuales son las IP---------------------#
print(sta_if.ifconfig())
#--------------------------------------------------------------#
#--------------------------------------------------------------#
#--------------------Nos conectamos al broker------------------#
def callback_alarma(topic, msg): #Se ejecuta cada vez que recibimos un topico
global ALARMA_ACTIVA,LedALARMA
dato = msg.decode('utf-8')
topicrec = topic.decode('utf-8')
print("Cambio en: "+topicrec+":"+dato)
if topicrec == topic_ALARMA and 'OFF' in dato:
ALARMA_ACTIVA=False
else:
ALARMA_ACTIVA=True
LedALARMA.value(ALARMA_ACTIVA)
#--------------------------------------------------------------#
#----------------------------intentamos conectarnos al broker MQTT-------------------------------#
try:
conexionMQTT = MQTTClient(client_id, mqtt_server,user=user,password=password,port=int(port))
conexionMQTT.set_callback(callback_alarma) #funcion callback para recibir del broker mensajes
conexionMQTT.connect() #Hacemos la conexion
conexionMQTT.subscribe(topic_ALARMA) #Nos suscribimos a un topico luego del connect
print("Conectado con Broker MQTT")
except OSError as e:
print("Fallo la conexion al Broker, reiniciando...") #Si fallo la conexion, reiniciamos todo
time.sleep(5)
machine.reset()
#------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------#
Sensor = dht.DHT22(Pin(14))
Parlante = PWM(Pin(19), freq=440, duty_u16=32768)
Parlante.duty(0)
SONANDO = False
while True:
try:
conexionMQTT.check_msg()
time.sleep(3)
Sensor.measure()
Temperatura = Sensor.temperature()
Humedad = Sensor.humidity()
print("Temperatura = "+str(Temperatura))
print("Humedad = "+str(Humedad))
conexionMQTT.publish(topic_TEMPERATURA, str(Temperatura))
conexionMQTT.publish(topic_HUMEDAD, str(Humedad))
if ALARMA_ACTIVA:
if (Temperatura > 35 or Humedad > 65):
SONANDO = True
else:
SONANDO = False
else:
SONANDO = False
if SONANDO:
Parlante.freq(659)
Parlante.duty(50)
time.sleep(0.2)
Parlante.duty(0)
time.sleep(0.2)
else:
Parlante.duty(0)
except OSError as e:
print("Error ",e)
time.sleep(5)
machine.reset()
#----------------------------------------------------------------------------------------------#