from machine import Pin
import dht
import time
# Initialize DHT22 sensor
dht_sensor = dht.DHT22(Pin(14))
# Initialize Buzzer and LED
led = Pin(2, Pin.OUT)
# Set temperature threshold for the alarm
TEMP_THRESHOLD = 30 # Adjust this value as needed
HUMIDITY_THRESHOLD = 40 # Adjust this value as needed
while True:
# Read data from DHT22
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print(f'Temperature: {temperature}°C|',f'Humidity: {humidity}%')
# Check if temperature exceeds the threshold
if temperature > TEMP_THRESHOLD and humidity < HUMIDITY_THRESHOLD:
# Activate alarm (LED)
led.on()
time.sleep(0.2) # LED on duration
led.off()
time.sleep(0.2) # LED off duration
print("⚠ Warning: High Value!")
else:
# Deactivate alarm
led.off()
# Delay before the next reading
time.sleep(2)