import machine
import dht
import time
from machine import I2C,Pin
from lcd_i2c import LCD
from umqtt.simple import MQTTClient
import network
I2C_ADDR = 0x27 # DEC 39, HEX 0x27
i2c = I2C(0, scl=Pin(27), sda=Pin(14), freq=800000)
lcd = LCD(addr=I2C_ADDR, cols=20, rows=4, i2c=i2c)
PinAlerta = Pin(22,Pin.OUT)
PinDir = Pin(2,Pin.OUT)
PinDir.value(1)
PinPaso = machine.Pin(0)
pwmPinPs = machine.PWM(PinPaso)
pwmPinPs.freq(10000)
pwmPinPs.duty(0)
ssid = 'Wokwi-GUEST'
wifipassword = ''
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, wifipassword)
print("CONECTANDO")
while not sta_if.isconnected():
print(".",end="")
time.sleep(0.1)
print("CONECTADO A INTERNET")
print(sta_if.ifconfig())
mqtt_server = 'io.adafruit.com'
port = 1883
user = 'LucasLeonel04'
password = 'aio_aXHQ89rmmlVhb9PGHMsIZ485KxE3'
client_id = 'tmp.idem.test'
topic_1 = 'LucasLeonel04/feeds/EstadoTemperatura'
topic_2 = 'LucasLeonel04/feeds/Max_Umbral'
topic_3 = 'LucasLeonel04/feeds/MaxTemp'
topic_4 = 'LucasLeonel04/feeds/ModoManual'
topic_5 = 'LucasLeonel04/feeds/Temp_Manual'
topic_6 = 'LucasLeonel04/feeds/VisorVentilador'
umbralmax = 0
manual_mode = 0
manual_temp = 0
dutyvent = 0
freqvent = 0
def estadoled(temp2, umbral):
if temp2>=umbral:
PinAlerta.value(1)
else:
PinAlerta.value(0)
def detfeq(tempact):
global freqvent, ppm, dutyvent
if tempact > 70:
dutyvent = 1023
freqvent = 10000
if tempact >= 60 and tempact <= 70:
dutyvent = 1023
freqvent = 5000
if tempact >= 40 and tempact < 60:
dutyvent = 1023
freqvent = 3334
if tempact >= 0 and tempact < 40:
dutyvent = 1023
freqvent = 1667
if tempact < 0:
dutyvent = 1023
freqvent = 100
def funcion_callback(topic, msg):
dato = msg.decode('utf-8')
topicrec = topic.decode('utf-8')
print("Mensaje en Topico: "+topicrec+": "+dato)
#topicrec indica la direccion del topico, siendo una cadena de caracteres que indica la direccion de topicoç
#Esto es util porque la funcion callback recibe mensajes de cualquier topico configurado, comparandolo con
#otra cadena de texto es posible diferencia el topico de origen de la informacion.
if topicrec == topic_2:
global umbralmax
print("Aumenta el umbral")
umbralmax = dato
if topicrec == topic_4:
global manual_mode
manual_mode = dato
print("Modo de manejo (0 - Automatico. 1 - Manual): "+str(manual_mode))
if topicrec == topic_5 and manual_mode == str(1):
global manual_temp
manual_temp = dato
print("TemperaturaManual: "+str(manual_temp))
try:
conexionMQTT = MQTTClient(client_id,mqtt_server,user=user,password=password,port=int(port))
conexionMQTT.set_callback(funcion_callback) #Funcion callback para recibir mensajes del broker
conexionMQTT.connect()
conexionMQTT.subscribe(topic_1) #se envia un dato
conexionMQTT.subscribe(topic_2) #se recibe un dato
conexionMQTT.subscribe(topic_3) #se envia un dato
conexionMQTT.subscribe(topic_4) #se recibe un dato
conexionMQTT.subscribe(topic_5) #se recibe un dato
conexionMQTT.subscribe(topic_6) #se envia un dato
print("Conectado con Broker MQTT")
except OSError as e:
print("Fallo la conexion al Broker, Reiniciando...")
time.sleep(3)
machine.reset()
tempsens = dht.DHT22(Pin(26))
tempant = 0
umbant = 0
maytemp = 0
mintemp = 0
tempsens.measure()
temp = tempsens.temperature()
if manual_mode != str(1):
temp = tempsens.temperature()
if manual_mode == str(1):
temp = float(manual_temp)
mintemp = temp
maytemp = temp
while True:
if manual_mode != str(1):
tempsens.measure()
temp = tempsens.temperature()
if manual_mode == str(1):
temp = float(manual_temp)
detfeq(temp)
pwmPinPs.freq(int(freqvent))
pwmPinPs.duty(int(dutyvent))
print("TEMP="+str(temp))
print("Frecuencia Ventilador="+str(freqvent))
if tempant != temp or umbant != umbralmax:
umbant = umbralmax
lcd.begin()
lcd.print("Temperatura: "+str(temp)+" C")
lcd.set_cursor(col=1, row=1)
lcd.print("Maxima: "+str(umbralmax)+" C")
lcd.set_cursor(col=1, row=2)
lcd.print("Rotor (PPS): "+str(freqvent))
lcd.set_cursor(col=1, row=3)
lcd.print("Modo Temp: "+str(manual_mode))
try:
conexionMQTT.check_msg()
#print("Umbral maximo de temperatura: "+str(umbralmax))
estadoled(float(temp),float(umbralmax))
#print("Maxima Temperatura Alcanzada: "+str(maytemp))
#print("Minima Temperatura Alcanzada: "+str(mintemp))
if tempant != temp:
time.sleep(1)
conexionMQTT.publish(topic_1,str(temp))
conexionMQTT.publish(topic_6,str(freqvent))
tempant = temp
if tempant >= maytemp:
maytemp=tempant
conexionMQTT.publish(topic_3,str(maytemp))
if mintemp >= tempant:
mintemp = tempant
except OSError as e:
print("Error ",e)
time.sleep(3)
machine.reset()