from machine import Pin,PWM
from time import sleep
from dht import DHT22
from machine import I2C, Pin

from pico_i2c_lcd import I2cLcd
toneA = 440
toneB = 494
toneC = 523
toneD = 587
toneE = 659
toneF = 698
toneG = 784


tones = [toneA, toneB, toneC, toneD, toneE, toneF, toneG]


def play_buzzer_and_sleep(freq, duration_ms, duty_value=512):
    buzzer= PWM(Pin(26))
    buzzer.freq(freq)
    buzzer.duty_u16(duty_value)
    sleep(duration_ms)
    buzzer.duty_u16(0)
    
def buzzerFunc():
    buzzer= PWM(Pin(26))
    buzzer.freq(500)
    buzzer.duty_u16(1000)
    sleep(1)
    buzzer.duty_u16(0)


dht = DHT22(Pin(22)) 
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)


I2C_ADDR = i2c.scan()[0]


lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)

RELAY_CTRL_PIN =  28
relay = Pin(RELAY_CTRL_PIN, Pin.OUT)


while True:

    dht.measure()
    temp = dht.temperature()
    hum = dht.humidity()


    lcd.move_to(1,0)
    lcd.putstr(f"Temp  :{temp} C")
    lcd.move_to(1,1)
    lcd.putstr(f"Hum :{hum} %")
  
    sleep(5)       
    lcd.clear()
    sleep(0.2)        
    
    sleep(1)
    if(temp > 28 or hum > 30):
        relay.high()
        buzzerFunc()

    else:
        relay.low()
        None
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
NOCOMNCVCCGNDINLED1PWRRelay Module