print("Hello, ESP32!")
# PIR SENSOR
print("This program integrates PIR sensor & OLED with ESP32")
# Credits to https://RandomNerdTutorials.com & program edited accordingly
# 1.import all necessary module / library
from machine import Pin # to declare Pin 14 & 12
from utime import sleep
from machine import Pin, SoftI2C # SoftI2C untuk SDL & SDA
import ssd1306 #oled library
# 2. INITIALIZE THE MOTION & declare pin on oled display
motion = False #initialize there is no movement
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
# 3. create function interrupt handling & define basic oled parameter
# THIS FUNC WILL BE CALL EVERY TIME A MOTION IS DETECTED
def handle_interrupt(pin): #def = function
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
oled_width = 128 #based on spec
oled_height = 64 #based on spec
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)
# 4. declare input output pin
# LED as output and PIR sensor as input
led_red = Pin(14, Pin.OUT) # OUT = output
pir = Pin(12, Pin.IN) # IN = input
# 5. set interrupt pin on PIR sensor by using irq() method
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
# 6. time to test how we can compare the output of LED when there is motion or no motion
while True: # forever loop
if motion == True: # a motion has been detected
print('Motion detected! Interrupt caused by:', interrupt_pin, 'LED Red = ON' )
for i in range (5): # akan blinking for 10 times
led_red.on()
sleep(0.2)
led_red.off()
sleep(0.2)
oled.fill(1)
oled.text('MOTION DETECTED', 0, 10, 0)
oled.text(' WARNING', 0, 30, 0)
oled.text(' HELEPPPP ', 0, 50, 0)
oled.show() # untuk display the text
sleep(0.2)
oled.invert(True)
oled.show() # untuk display the text
print('Motion stopped! LED Red = OFF') # no motion
motion = False