print("This program integrate ESP32 with DHT22 + oled + SERVO + Buzzer\n")
# Import required libraries
import machine
import dht
import ssd1306
from machine import Pin, PWM
from utime import sleep
import network
import time
# Set up pins for the DHT22 sensor, OLED display and servo motor
dht_pin = machine.Pin(15)
dht_sensor = dht.DHT22(dht_pin)
i2c = machine.I2C(-1, machine.Pin(22), machine.Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
servo_pin = machine.Pin(14)
servo = PWM(servo_pin, freq=50,)
buzzer = PWM(Pin(18) ,Pin.OUT)
# Led for Level Water Sprikler
led_red = Pin(27, Pin.OUT) #Level 0
led_yellow = Pin(26, Pin.OUT) #Level 1
led_green = Pin(25, Pin.OUT) #Level 2
# Connect to WiFi network
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
sleep(0.1)
print(" Connected!")
# Read data from the DHT22 sensor, display on the OLED, and activate servo motor if temperature exceeds threshold
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
# Display temperature and humidity on OLED screen
oled.fill(0)
oled.text('Welcome To',0 , 0)
oled.text('Football Field',0 , 10)
oled.text('Temperature:{:.1f} C'.format(temp), 0, 30)
oled.text('Stadium Bukit Jalil!! ',0 , 40)
oled.show()
# Check if temperature more than 20 C, and activate servo motor if it does
if temp > 20:
print('\n')
print("-----------------------------------------------------")
print("Water Sprinkler On AT lEVEL 2 !")
print("Your Football Field Temperature is HIGH Then 20 C ")
print("Temperature is {:.1f} C".format(temp))
print("GAMEE ONNNN!!")
print("-----------------------------------------------------")
print('\n')
duty = 90 # change duty cycle to change servo motor angle
servo.duty(duty)
led_green.on()
led_red.on()
led_yellow.on()
buzzer.init(freq=500, duty=10)
time.sleep(0.2)
buzzer.init(freq=200, duty=100)
time.sleep(0.2)
buzzer.init(freq=100, duty=1000)
time.sleep(0.2)
elif 0<= temp <20:
print('\n')
print("-----------------------------------------------------")
print("Water Sprinkler On At LEVEL 1!")
print("Your Football Field Temperature is MEDIUM")
print("Temperature is {:.1f} C".format(temp))
print("PLEASE WAIT ULTILL DRY!!")
print("-----------------------------------------------------")
print('\n')
duty = 60 # change duty cycle to change servo motor angle
servo.duty(duty)
led_yellow.on()
led_green.off()
led_red.on()
buzzer.init(freq=100, duty=1000)
time.sleep(0.2)
buzzer.init(freq=200, duty=100)
time.sleep(0.2)
buzzer.init(freq=500, duty=10)
time.sleep(0.2)
else:
print('\n')
print("-----------------------------------------------------")
print("Water Sprinkler Off AT lEVEL 0")
print("Your Football Field Temperature is Bellow THAN 0C!! ")
print("Temperature is {:.1f} C".format(temp))
print("WEATHER IS TOO COLD.MATCH WILL BE POSTPONED!!")
print("-----------------------------------------------------")
print('\n')
duty = 30 # change duty cycle to change servo motor angle
servo.duty(duty)
led_red.on()
led_green.off()
led_yellow.off()
buzzer.init(freq=1, duty=1)
time.sleep(1)
# Wait for some time before reading data again
time.sleep(1)