from picozero import Speaker
from time import sleep
import machine
import utime
import dht
DATA_PIN = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_UP)
sensor = dht.DHT22(DATA_PIN)
speaker = Speaker(15)
def read_and_display_data():
try:
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
print("Temperature: {:.2f}°C, Humidity: {:.2f}%".format(temp, humidity))
if(temp>30):
speaker.on()
sleep(1)
speaker.off()
sleep(1)
if(humidity>40):
speaker.on()
sleep(1)
speaker.off()
sleep(1)
except Exception as e:
print("Error reading sensor data:", e)
while True:
read_and_display_data()
utime.sleep(1)