import dht
from time import sleep
from machine import Pin, PWM, I2C
from pico_i2c_lcd import I2cLcd
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# Set the GPIO pin where the DHT22 is connected
DHT_PIN = 28 # Use the GPIO number, not the physical pin number
# Initialize the DHT22 sensor
dht_sensor = dht.DHT22(machine.Pin(DHT_PIN))
def buzzerFunc():
buzzer = PWM(Pin(22))
buzzer.freq(500)
buzzer.duty_u16(1000)
sleep(1)
buzzer.duty_u16(0)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
while True:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature: {:.2f}°C, Humidity: {:.2f}%".format(temperature, humidity))
lcd.putstr("Temp:")
lcd.putstr(str(temperature))
lcd.putstr(" C")
lcd.putstr("\n")
lcd.putstr("Hum:")
lcd.putstr(str(humidity))
lcd.putstr("%")
sleep(5)
lcd.clear()
sleep(1)
if(temperature > 20 and humidity > 30):
buzzerFunc()
else:
None