from machine import Pin
from time import sleep
import dht
# Inisialisasi sensor DHT22
dht_sensor = dht.DHT22(Pin(15))
# Inisialisasi pin LED
led_merah = Pin(5, Pin.OUT)
led_kuning = Pin(18, Pin.OUT)
led_hijau = Pin(19, Pin.OUT)
# Inisialisasi pin Relay
relay = Pin(4, Pin.OUT)
def update_led_and_relay(temperature, humidity):
# Reset semua LED dan Relay
led_merah.off()
led_kuning.off()
led_hijau.off()
relay.off()
# Kondisi temperatur
if temperature > 35:
led_merah.on() # Nyalakan LED Merah
elif 30 < temperature <= 35:
led_kuning.on() # Nyalakan LED Kuning
else:
led_hijau.on() # Nyalakan LED Hijau
# Kondisi kelembaban
if humidity > 80:
relay.on() # Aktifkan relay (Nyalakan LED Biru)
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print(f"Temperature: {temp} C")
print(f"Humidity: {hum} %")
# Update status LED dan Relay berdasarkan temperatur dan kelembaban
update_led_and_relay(temp, hum)
except Exception as e:
print(f"Error: {e}")
sleep(2) # Baca data setiap 2 detik