print("This practical project about weather monitoring system by using esp32")
from machine import I2C, Pin, Timer, PWM
from time import sleep
from dht import DHT22
import ssd1306
# For temperature sensor
dht22 = DHT22(Pin(12))
# For buzzer using OOP
buzzer = PWM(Pin(23), Pin.OUT)
# For LED
led1 = Pin(19, Pin.OUT)
led2 = Pin(18, Pin.OUT)
# FOR OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Function to read DHT
def readDht():
dht22.measure()
return dht22.temperature(), dht22.humidity()
# BUZZER
def play_buzzer(freq, duty, duration):
buzzer = PWM(Pin(23), Pin.OUT)
buzzer.freq(freq)
buzzer.duty(duty)
sleep(duration)
buzzer.deinit()
while True:
sleep(5)
dht22.measure()
print("Temperature: {}C".format(dht22.temperature()))
print("Humidity: {}%".format(dht22.humidity()))
temp = dht22.temperature()
hum = dht22.humidity()
# the range temperature and humidity
if temp <= 30 and hum >= 80:
print('Overcast weather detected!')
for _ in range(5): # Blink the LED 5 times
play_buzzer(freq=1704, duty=400, duration=0.3)
sleep(0.3)
play_buzzer(freq=1, duty=0, duration=0.3)
led1.value(1)
sleep(0.3)
led1.value(0)
sleep(0.3)
elif temp >= 35 and hum <= 60:
print('Hot weather detected!')
for _ in range(5): # Blink the LED 5 times
play_buzzer(freq=1704, duty=400, duration=0.3)
sleep(0.3)
play_buzzer(freq=1, duty=0, duration=0.3)
led2.value(1)
sleep(0.3)
led2.value(0)
sleep(0.3)
else:
sleep(1)
play_buzzer(freq=1, duty=0, duration=5)
oled.fill(0)
oled.text('Temp: {:.2f}C'.format(temp), 10, 10)
oled.text('Humi: {:.2f}%'.format(hum), 10, 20)
oled.show()