from machine import Pin, I2C, PWM
import ssd1306
import dht
from utime import sleep
print("Hello, ESP32!")
rled = Pin(15, Pin.OUT) #LED SETUP PIN
gled = Pin(2, Pin.OUT)
bled = Pin(4, Pin.OUT)
btnr = Pin(12, Pin.IN) #BUTTON SETUP PIN
buzzer = PWM(Pin(19), freq=1000, duty=0) #BUZZER SETUP PIN
servopwm = PWM(Pin(5), freq=50, duty=75) #SERVO SETUP PIN
servopwm2 = PWM(Pin(18), freq=50, duty=75)
sensor = dht.DHT22(Pin(13)) #DHT SETUP PIN
#OLED SETUP
i2c = I2C(0, scl=Pin(22), sda=Pin(21)) #OLED SETUP PIN
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
#SERVO SETUP
def Servo(servo, angle):
servopwm.duty(int(((angle)/180 *2 + 0.5) / 20 * 1023))
def Servo2(servo, angle):
servopwm2.duty(int(((angle)/180 *2 + 0.5) / 20 * 1023))
while True:
#READ BUTTON PARAMETER
rbtn = btnr.value()
#read the parameters from the sensor
sensor.measure()
temp = sensor.temperature()
humi = sensor.humidity()
#ALWAYS SHOW DISPLAY
oled.fill(0)
oled.text('CLIMATE ACTION', 9, 0)
oled.text('by Luuth', 30, 10)
oled.text('Temp: {:.1f} C'.format(temp) , 10, 25)
oled.text('Humi: {:.1f} %'.format(humi), 10, 35)
oled.show()
#LOOP FOR SENSOR READINGS
if temp > 34:
gled.off()
rled.on()
sleep(0.1)
rled.off()
bled.on()
sleep(0.1)
bled.off()
Servo(2, 180)
oled.text('Temp is HOT', 10, 45)
oled.show()
if humi < 80:
gled.off()
rled.on()
sleep(0.1)
rled.off()
bled.on()
sleep(0.1)
bled.off()
Servo(2, 180)
oled.text('Humi is LOW', 10, 55)
oled.show()
if temp < 33.9 and humi > 80.1:
gled.on()
oled.text('GOOD ENVIRONMENT', 0, 50)
oled.show()
buzzer.duty(0)
Servo(2, 90)
#FUNCTION LOOP FOR BUTTON READINGS
if rbtn == True:
Servo(2, 180)
print("rbtn pushed")
#if rbtn == False:
# Servo(2,90)
sleep(2)