print("Hello, ESP32!")
print("Developed by: HANNAN")
print("Date: 22/4/2035")
#Import library/module
from machine import Pin, PWM, SoftI2C
import dht
import oled # Make sure your oled.py file exists
from time import sleep
# Pin Declaration
dht_sensor = dht.DHT22(Pin(26))
red_led = Pin(18, Pin.OUT)
blue_led = Pin(17, Pin.OUT)
buzzer = PWM(Pin(16), Pin.OUT)
SCL_Pin = Pin(22, Pin.OUT)
SDA_Pin = Pin(21, Pin.OUT)
OLED_Pin = SoftI2C(scl= SCL_Pin, sda = SDA_Pin)
# Object Declaration for OLED
screen = oled.SSD1306_I2C(width=128, height=64, i2c= OLED_Pin)
#Parameter Declaration
#Main Program
#OLED code colour --> 1 is for WHITE , 0 is for BLACK
while True:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature: {:.1f}°C, Humidity: {:.1f}%".format(temperature, humidity))
# Clear screen
screen.fill(0)
# Show values on OLED
screen.text("Temp: {:.1f}C".format(temperature), 0, 0, 1)
screen.text("Humidity: {:.1f}%".format(humidity), 0, 10, 1)
# TEMP alert
if temperature >= 30:
screen.text("WARNING!", 0, 25, 1)
red_led.on()
buzzer.init(freq=5000, duty=512)
else:
red_led.off()
# HUMIDITY alert
if humidity >= 70:
screen.text("ITS DANGEROUS", 0, 35, 1)
blue_led.on()
buzzer.init(freq=1500, duty=512)
else:
blue_led.off()
# Safe message if both okay
if temperature < 30 and humidity < 70:
screen.text("ALL GOOD", 0, 45, 1)
buzzer.init(freq=800, duty=0)
# Footer message
screen.text("HANNAN <3", 35, 55, 1)
screen.show()
sleep(5)