import time
import machine
import dht
dht_pin = machine.Pin(4)
dht_sensor = dht.DHT22(dht_pin)
buzzer = machine.Pin(2, machine.Pin.OUT)
threshold_temp = 40
alarm_counter = 0
check_interval = 60
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
print("Temperature:", temp, "°C")
if temp > threshold_temp:
alarm_counter += 1
print(f"High temperature count: {alarm_counter}")
else:
alarm_counter = 0
if alarm_counter >= 10:
print("Alarm! Temperature exceeded threshold 10 times.")
buzzer.on()
time.sleep(10)
buzzer.off()
alarm_counter = 0
except OSError as e:
print("Failed to read sensor.")
time.sleep(check_interval)