print("This project uses ESP 32 for soil movement on hillsides highway")
# Import necessary libraries/modules
import ultrasonic_library
from machine import Pin, SoftI2C, PWM
import Oled_Library
from machine import Pin
from utime import sleep
import dht
# Declare pin constants for better readability
ULTRASONIC_TRIGGER_PIN = 27
ULTRASONIC_ECHO_PIN = 26
LED_PIN = 12
PIR_PIN = 14
BUZZER_PIN = 5
I2C_SCL_PIN = 22
I2C_SDA_PIN = 21
DHT_PIN = 4 # Adjust the pin number accordingly
# Declare Ultrasonic Pin Connection
pikachu = ultrasonic_library.HCSR04(ULTRASONIC_TRIGGER_PIN, ULTRASONIC_ECHO_PIN, 30000)
# Declare Oled connection
i2c_oled = SoftI2C(scl=Pin(I2C_SCL_PIN), sda=Pin(I2C_SDA_PIN))
oled_width = 128 # based on spec
oled_height = 64 # based on spec
screen = Oled_Library.SSD1306_I2C(oled_width, oled_height, i2c_oled)
# Declare LED, PIR, and Buzzer Pin Connections
led_red = Pin(LED_PIN, Pin.OUT)
pir = Pin(PIR_PIN, Pin.IN)
buzzer = PWM(Pin(BUZZER_PIN), Pin.OUT)
# Declare DHT22 connection
dht22 = dht.DHT22(Pin(DHT_PIN)) # Adjust the pin number accordingly
# Initialize motion parameter
motion = False
# Create function for interrupt handling
# Remarks: This function will be called every time motion is detected!
# If the PIR Motion Sensor detected a landslide, it will generate output to LED and Buzzer
def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
def display_distance(distance_mm, distance_cm):
print('Land Slide Motion:', distance_mm, 'mm') # Choose mm if you want whole number
print('Land Slide Motion:', distance_cm, 'cm') # Choose cm if you want fraction number
print('--------------------------------------------')
print('\n')
def display_on_oled(text1, value1, unit1, text2, value2, unit2):
screen.fill(0) # To make screen becoming white
screen.text(text1, 0, 0)
screen.text(str(value1), 0, 10)
screen.text(unit1, 50, 10)
screen.text(text2, 0, 20)
screen.text(str(value2), 0, 30)
screen.text(unit2, 50, 30)
screen.show()
def display_temperature_humidity_on_oled(temperature, humidity):
screen.fill(0) # To make screen becoming white
screen.text('Temp: {}C'.format(temperature), 0, 0)
screen.text('Humidity: {}%'.format(humidity), 0, 20)
screen.show()
while True:
# Let's test marii
jarak_in_mm = pikachu.distance_mm()
jarak_in_cm = pikachu.distance_cm()
# Set Interrupt for PIR Motion Sensor
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
# Read temperature and humidity data
dht22.measure()
temperature = dht22.temperature()
humidity = dht22.humidity()
# Display the distance from the object
display_distance(jarak_in_mm, jarak_in_cm)
# Display temperature and humidity on OLED
display_temperature_humidity_on_oled(temperature, humidity)
# Making Decision on how the landslide moves
if jarak_in_mm > 3000:
for i in range(10):
# Turn on the buzzer
buzzer.init(freq=1500, duty=300)
sleep(1)
buzzer.init(freq=1, duty=0)
sleep(0.3)
led_red.on()
sleep(0.1)
led_red.off()
sleep(0.1)
display_on_oled('LandSlide Detected:', jarak_in_mm, 'mm', 'Temp:', temperature, 'C')
elif 3000 >= jarak_in_mm >= 2000:
for i in range(10):
# Turn on the buzzer
buzzer.init(freq=1500, duty=300)
sleep(0.3)
buzzer.init(freq=1, duty=0)
sleep(0.3)
led_red.on()
sleep(0.1)
led_red.off()
sleep(0.1)
display_on_oled('Motion Detected:', jarak_in_mm, 'mm', 'Temp:', temperature, 'C')
else:
buzzer.init(freq=1, duty=0)
sleep(1)