from machine import Pin, PWM
import dht
import time
sensor = dht.DHT22(Pin(15))
buzzer = PWM(Pin(14))
def play_tone(freq, duration):
buzzer.freq(freq)
buzzer.duty_u16(30000)
time.sleep(duration)
buzzer.duty_u16(0)
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temperature:", temp, "°C")
print("Humidity:", hum, "%")
if temp > 50:
print("Too HOT!")
for _ in range(5):
play_tone(2000, 0.2)
elif temp < 5:
print("Too COLD!")
for _ in range(3):
play_tone(500, 0.5)
if hum >30:
print("Humidity HIGH!")
play_tone(1500, 1)
elif hum < 5:
print("Humidity LOW!")
play_tone(800, 0.3)
time.sleep(0.2)
play_tone(800, 0.3)
print("----------------------")
except OSError:
print("Sensor error")
time.sleep(2)