import machine
import time
import tm1637
from machine import I2C, Pin # since I2C communication would be used, I2C class is imported
from time import sleep
from dht import DHT22
# creating a DHT object
dht1 = DHT22(Pin(22))
dht2 = DHT22(Pin(21))
dht3 = DHT22(Pin(20))
dhts = [dht1, dht2, dht3]
display1 = tm1637.TM1637(clk=machine.Pin(3), dio=machine.Pin(2))
display2 = tm1637.TM1637(clk=machine.Pin(5), dio=machine.Pin(4))
display3 = tm1637.TM1637(clk=machine.Pin(9), dio=machine.Pin(8))
display4 = tm1637.TM1637(clk=machine.Pin(7), dio=machine.Pin(6))
display5 = tm1637.TM1637(clk=machine.Pin(11), dio=machine.Pin(10))
display6 = tm1637.TM1637(clk=machine.Pin(13), dio=machine.Pin(12))
displays = [display1, display2, display3, display4, display5, display6]
led1 = Pin(28, Pin.OUT)
led2 = Pin(27, Pin.OUT)
led3 = Pin(26, Pin.OUT)
leds = [led1, led2, led3]
btn1 = Pin(14, Pin.IN, Pin.PULL_UP)
btn2 = Pin(15, Pin.IN, Pin.PULL_UP)
btn3 = Pin(16, Pin.IN, Pin.PULL_UP)
pilotV = Pin(19, Pin.OUT)
pilotN = Pin(18, Pin.OUT)
pilotR = Pin(17, Pin.OUT)
# this module needs to be saved in the Raspberry Pi Pico in order for the LCD I2C to be used
from pico_i2c_lcd import I2cLcd
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
def map(value, fromLow, fromHigh, toLow, toHigh):
# Map a value from one range to another
return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow
def printdis(display,tmp, hum):
if display < len(displays) - 1:
displays[display].temperature(tmp)
displays[display+1].numbers(0, hum)
def pilots():
tempval = 0
for led in leds:
hto = led.value()
tempval += hto
if tempval > 1 :
pilotV.value(0)
pilotN.value(0)
pilotR.value(1)
elif tempval == 1:
pilotV.value(0)
pilotN.value(1)
pilotR.value(0)
else:
pilotV.value(1)
pilotN.value(0)
pilotR.value(0)
alerta = 0 # Variable para rastrear si algún sensor está en alerta
def measuredht(displayx, dh):
global alerta
dhts[dh].measure()
tempx = map(dhts[dh].temperature(), (-40), 80, 25, 35) #dhts[dh].temperature()
humx = map(dhts[dh].humidity(), 0, 100, 5, 25) #dhts[dh].humidity()
printdis(int(displayx), int(tempx), int(humx))
if tempx > 30:
lcd.clear()
lcd.move_to(0,0)
lcd.putstr(f"ALERTA SNS N:{dh+1}!")
lcd.move_to(0,1)
lcd.putstr("TEMP ARRIBA 50%")
elif humx > 15:
lcd.clear()
lcd.move_to(0,0)
lcd.putstr(f"ALERTA SNS N:{dh+1}!")
lcd.move_to(0,1)
lcd.putstr("HUMI ARRIBA 50%")
else:
alerta += 1
while True:
alerta = 0 # Reinicia el estado de alerta antes de medir los sensores
measuredht(0, 0)
measuredht(2, 1)
measuredht(4, 2)
if btn1.value() == 0:
led1.value(1)
if btn2.value() == 0:
led2.value(1)
if btn3.value() == 0:
led3.value(1)
pilots()
if alerta == 3: # Solo muestra "NO WORRIES" si ningún sensor está en alerta
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("SENSORES AL PELO!")
lcd.move_to(0,1)
lcd.putstr("NO WORRIES")
#lcd.putstr("Hello world!")
#sleep(5) # "Hello world!" text would be displayed for 5 secs
#lcd.clear()
#sleep(1) # clear the text for 1 sec then print the text again