import dht
import time
from machine import Pin, PWM
sensor = dht.DHT22(Pin(16))
red = PWM(Pin(13))
green = PWM(Pin(14))
blue = PWM(Pin(15))
red.freq(1000)
green.freq(1000)
blue.freq(1000)
buzzer = PWM(Pin(11))
def set_color(r, g, b):
red.duty_u16(r)
green.duty_u16(g)
blue.duty_u16(b)
def fade_red():
for i in range(0, 65535, 2000):
set_color(i, 0, 0)
time.sleep(0.01)
def rainbow():
colors = [
(65535,0,0),
(65535,30000,0),
(65535,65535,0),
(0,65535,0),
(0,0,65535),
(30000,0,65535),
(50000,0,65535)
]
for c in colors:
set_color(c[0], c[1], c[2])
time.sleep(0.2)
while True:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print(f"Temperature: {temp}")
print(f"Humidity: {hum}")
# =========================
# COOL + DRY
# =========================
if temp < 25 and hum < 40:
set_color(0, 0, 65535)
buzzer.duty_u16(0)
print("Cool & Dry")
# =========================
# COMFORT CONDITION
# =========================
elif temp < 35 and hum < 70:
rainbow()
buzzer.duty_u16(0)
print("Comfort Environment")
# =========================
# HIGH HUMIDITY
# =========================
elif hum > 70:
set_color(0, 65535, 65535)
buzzer.freq(1200)
buzzer.duty_u16(20000)
print("High Humidity ALERT")
# =========================
# HIGH TEMPERATURE
# =========================
else:
fade_red()
buzzer.freq(2000)
buzzer.duty_u16(30000)
print("High Temperature ALERT")
print("------------------------")
time.sleep(2)