#----------IMPORTANDO MODULOS------------
import time
import machine
import dht
import micropython
import network
from machine import Pin,PWM
from umqtt.simple import MQTTClient
#-----------SE CONECTA AL WIFI --------------
#Red wifi y clave
ssid = 'Wokwi-GUEST'
wifipassword = ''
#Modo Station
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
#Para conectarse al wifi
sta_if.connect(ssid, wifipassword)
print("Conectando")
while not sta_if.isconnected():
print(".",end="")
time.sleep(0.1)
print("Conectado a Wifi")
#Para ver las IP
print(sta_if.ifconfig())
#-----------DATOS DEL SERVER MQTT-------------
#Informacion del server MQTT (Broker)
mqtt_server = 'io.adafruit.com'
port = 1883
user = 'embebidos1234'
password = 'aio_xfkK52ZuK4Da6NCG0JL3RLCg7EAO'
#ID y topicos
client_id = 'IoT Embebidos'
topic_umbralTemperatura = 'embebidos1234/feeds/umbralhumedad'#topico del dato que ingresa el usuario por teclado
topic_SERVO = 'embebidos1234/feeds/servo'#saber si esta prendido regando o no
topic_pantalla = 'embebidos1234/feeds/pantalla'
#-----------FUNCION CALLBACK----------------
lista=["#","#","#","#"]
global i
i=0
#Funcion que se llama cada vez que se produce conexionMQTT.check_msg()
#sobre un topico al que estamos suscriptos
def callback_datos(topic, msg):
global f
temp_user=0
temp_user = msg.decode('utf-8')
topicrec = topic.decode('utf-8')
print("Mensaje en el topico: "+topicrec+":"+temp_user)
if topicrec == topic_umbralTemperatura:
#return temp_user
f=i
print(f)
lista[f]=temp_user
#--------Se intenta conectar al broker MQTT-----------
try:
conexionMQTT = MQTTClient(client_id, mqtt_server,user=user,password=password,port=int(port))
conexionMQTT.set_callback(callback_datos)#establece que funcion se llama para leer los mesajes de los topicos
conexionMQTT.connect()
conexionMQTT.subscribe(topic_umbralTemperatura)
print("Conectado con Broker MQTT")
#-------------SE PIDEN LOS DATOS----------------------
#vienen los digitos por separado
bandera=0
while lista[0]=="#" or lista[0]=="*":
if bandera==1:
conexionMQTT.publish(topic_pantalla,"valor invalido")
time.sleep(3)
bandera=1
conexionMQTT.publish(topic_pantalla,"Ingrese el primer digito de la temperatura maxima")
print("hola1")
time.sleep(5)
conexionMQTT.check_msg()
bandera=0
i=i+1
while lista[1]=="#" or lista[1]=="*":
if bandera==1:
conexionMQTT.publish(topic_pantalla,"valor invalido")
time.sleep(3)
bandera=1
conexionMQTT.publish(topic_pantalla,"Ingrese el segundo digito de la temperatura maxima")
print("hola2")
time.sleep(5)
conexionMQTT.check_msg()
bandera=0
i=i+1
while lista[2]=="#" or lista[2]=="*":
if bandera==1:
conexionMQTT.publish(topic_pantalla,"valor invalido")
time.sleep(3)
bandera=1
conexionMQTT.publish(topic_pantalla,"Ingrese el primer digito de la temperatura minima")
print("hola3")
time.sleep(5)
conexionMQTT.check_msg()
bandera=0
i=i+1
while lista[3]=="#" or lista[3]=="*":
if bandera==1:
conexionMQTT.publish(topic_pantalla,"valor invalido")
time.sleep(3)
bandera=1
conexionMQTT.publish(topic_pantalla,"Ingrese el segundo digito de la temperatura minima")
print("hola4")
time.sleep(5)
conexionMQTT.check_msg()
conexionMQTT.publish(topic_pantalla,"!LISTO¡")
except OSError as e:
#Si falla, se reinicia por completo
print("Fallo la conexion al Broker, reiniciando...")
time.sleep(5)
machine.reset()
#--------------SE JUNTAN LOS DATOS------------------
temp_max1=lista[0]
temp_max2=lista[1]
temp_min1=lista[2]
temp_min2=lista[3]
temp_max = int(temp_max1+temp_max2)
temp_min = int(temp_min1+temp_min2)
print(f"Temperatura maxima = {temp_max}\nTemperatura minima = {temp_min}")
#--------SE EJECUTA LA FUNCIONALIDAD DEL PROGRAMA---------
#----------SENSOR DHT22----------
sensor=dht.DHT22(Pin(14))
#----------SERVO(ASPERSOR)--------
ASPERSOR_ACTIVO="0"
pin16 = machine.Pin(16)
pwmPin = machine.PWM(pin16)
pwmPin.freq(50)
#----------FUNCIONAMIENTO---------
conexionMQTT.publish(topic_SERVO,str(0))
while True:
sensor.measure()
temp_actual=sensor.temperature()
hum_actual=sensor.humidity()
band = 0
if temp_actual>=temp_max:
while temp_actual>temp_min:
ASPERSOR_ACTIVO = "1"
#mandar el estado del aspersor
if band == 0:
conexionMQTT.publish(topic_SERVO,str(1))
band = 1
#mover servo
pwmPin.duty(25)
time.sleep_ms(800)
pwmPin.duty(45)
time.sleep_ms(800)
sensor.measure()
temp_actual=sensor.temperature()
if temp_actual<= temp_min:
break
pwmPin.duty(65)
time.sleep_ms(800)
pwmPin.duty(85)
time.sleep_ms(800)
sensor.measure()
temp_actual=sensor.temperature()
if temp_actual<= temp_min:
break
pwmPin.duty(105)
time.sleep_ms(800)
pwmPin.duty(125)
time.sleep_ms(800)
sensor.measure()
temp_actual=sensor.temperature()
if temp_actual<= temp_min:
break
pwmPin.duty(105)
time.sleep_ms(800)
pwmPin.duty(85)
time.sleep_ms(800)
pwmPin.duty(65)
sensor.measure()
temp_actual=sensor.temperature()
if temp_actual<= temp_min:
break
time.sleep_ms(800)
pwmPin.duty(45)
time.sleep_ms(800)
pwmPin.duty(25)
time.sleep_ms(800)
sensor.measure()
temp_actual=sensor.temperature()
ASPERSOR_ACTIVO = "0"
if band == 1 :
conexionMQTT.publish(topic_SERVO,str(0))
time.sleep_ms(800)