PR.FT5_15·Kit RPi›Estação MeteorológicaPróximo →
Multi-componente · DHT11 + LCD + LED RGB

🌤️ Estação Meteorológica

Temperatura e humidade do DHT11 apresentadas num LCD I²C. LED RGB indica o estado de conforto: verde (confortável), amarelo (atenção), vermelho (alerta).

Código completo

Python · estacao.py
import adafruit_dht, board, time
import RPi.GPIO as GPIO
from RPLCD.i2c import CharLCD

R, G, B = 18, 23, 24
GPIO.setmode(GPIO.BCM)
for p in [R, G, B]: GPIO.setup(p, GPIO.OUT)
pr = GPIO.PWM(R,1000); pr.start(0)
pg = GPIO.PWM(G,1000); pg.start(0)
pb = GPIO.PWM(B,1000); pb.start(0)

lcd    = CharLCD('PCF8574', 0x27)
sensor = adafruit_dht.DHT11(board.D4)

def cor_estado(t, h):
    if 18 <= t <= 26 and h <= 60:
        pr.ChangeDutyCycle(0); pg.ChangeDutyCycle(100); pb.ChangeDutyCycle(0)
    elif 26 < t <= 30:
        pr.ChangeDutyCycle(100); pg.ChangeDutyCycle(40); pb.ChangeDutyCycle(0)
    else:
        pr.ChangeDutyCycle(100); pg.ChangeDutyCycle(0); pb.ChangeDutyCycle(0)

try:
    while True:
        try:
            t, h = sensor.temperature, sensor.humidity
            lcd.clear()
            lcd.write_string(f'Temp:  {t:.1f}\xdfC')
            lcd.cursor_pos = (1, 0)
            lcd.write_string(f'Humid: {h:.0f}%')
            cor_estado(t, h)
        except RuntimeError: pass
        time.sleep(2)
except KeyboardInterrupt:
    lcd.clear(); sensor.exit(); GPIO.cleanup()