import machine
import dht
import time
import micropython
import network
from machine import Pin
from machine import PWM
from umqtt.simple import MQTTClient
#--------------------------------------------------------------#
#indicamos red WIFI y clave
ssid = 'Wokwi-GUEST'
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)
#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())
#Antes de conectarnos al broker, definimos una funcion
#que sera llamada cada vez que se produzca un publish sobre
#un topico donde estamos suscriptos
def callback_alarma(topic, msg):
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)
conexionMQTT.connect()
conexionMQTT.subscribe(topic_ALARMA)
print("Conectado con Broker MQTT")
except OSError as e:
print("Fallo la conexion al Broker, reiniciando...")
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()
#while True:
# Sensor.measure()
# Temperatura = Sensor.temperature()
# Humedad = Sensor.humidity()
# print("Temperatura = "+str(Temperatura))
# print("Humedad = "+str(Humedad))
# if (Temperatura > 35 or Humedad > 65):
# LedALARMA.value(1)
#
# LedALARMA.value(0)
# else:
# LedALARMA.value(0)
# Parlante.duty(0)
#time.sleep(5)
#----------------------------------------------#