from machine import Pin, I2C, PWM, SoftI2C
from i2c_lcd import I2cLcd # Import LCD library
import hcsr04 # Ultrasonic sensor library
import ssd1306 # OLED library
from dht import DHT11, DHT22 # DHT sensor library
from utime import sleep
print("This program integrates ESP32 with ultrasonic sensor, DHT sensor, and health messages.")
# Initialize I2C for LCD
i2c_lcd = I2C(0, scl=Pin(5), sda=Pin(17))
print("I2C scan:", i2c_lcd.scan()) # Verify LCD detection
lcd = I2cLcd(i2c_lcd, 0x27, 2, 16) # Adjust I2C address if necessary
# Initialize I2C for OLED
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)
# Initialize Ultrasonic Sensor
ultrasonic_sensor = hcsr04.HCSR04(trigger_pin=13, echo_pin=27, echo_timeout_us=500 * 2 * 30)
# Initialize DHT Sensor
dht_sensor = DHT11(Pin(4)) # Replace with DHT22 if using it
# Initialize Push Button
button = Pin(0, Pin.IN, Pin.PULL_UP)
# Initialize Buzzer
buzzer = PWM(Pin(23))
# Initialize LEDs (LED1 and LED2)
led1 = Pin(32, Pin.OUT) # Pin for LED1
led2 = Pin(33, Pin.OUT) # Pin for LED2
# Initialize Servo Motor
servo = PWM(Pin(14), freq=50)
# Function to rotate servo motor 360 degrees
def rotate_servo_360():
servo.duty(120) # Start rotation (adjust for your servo)
sleep(2) # Duration for a 360-degree rotation
servo.duty(75) # Stop the servo (neutral position)
# Function to display "999" on the LCD
def display_999():
lcd.clear()
lcd.putstr("999")
lcd.move_to(0, 1)
lcd.putstr("CALLING AMBULANCE")
# Function to display health message on OLED
def display_health_on_oled(distance_cm, humidity=None):
oled.fill(0) # Clear the screen
if humidity:
oled.text('Humidity:', 0, 0, 1)
oled.text(f'{humidity}%', 0, 20, 1)
elif distance_cm >= 40:
oled.text('Demam Panas', 3, 0, 1) # Fever message
oled.text('Sila ke Hospital', 3, 20, 1)
elif 20 <= distance_cm <= 29:
oled.text('Sangat Sihat', 3, 0, 1)
oled.text('Good', 3, 20, 1)
elif 30 <= distance_cm <= 35:
oled.text('Sihat', 3, 0, 1)
oled.text('Good', 3, 20, 1)
elif 36 <= distance_cm <= 39:
oled.text('Demam', 3, 0, 1)
oled.text('Sila Makan Ubat', 3, 20, 1)
else:
oled.fill(0) # Clear OLED for other distances
oled.show()
# Function to create ambulance-like alert with buzzer and LEDs
def ambulance_alert():
for _ in range(15): # Repeat the pattern for a short burst
# Low tone
buzzer.init(freq=500, duty=400) # Low tone
led1.value(1) # LED1 on
sleep(0.2) # Tone duration
buzzer.init(freq=1, duty=0) # Turn off buzzer
led1.value(0) # LED1 off
sleep(0.1) # Pause between tones
# High tone
buzzer.init(freq=800, duty=400) # High tone
led2.value(1) # LED2 on
sleep(0.2) # Tone duration
buzzer.init(freq=1, duty=0) # Turn off buzzer
led2.value(0) # LED2 off
sleep(0.1) # Pause between tones
# Main loop
while True:
# Emergency button logic
if not button.value(): # Button is pressed (logic low)
display_999() # Display "999" and "CALLING" on LCD
ambulance_alert() # Activate ambulance-like alert
else: # Default LCD message
lcd.clear()
lcd.putstr("Press button for")
lcd.move_to(0, 1)
lcd.putstr("Emergency Call")
buzzer.init(freq=1, duty=0) # Ensure buzzer is off
led1.value(0) # Ensure LED1 is off
led2.value(0) # Ensure LED2 is off
# Ultrasonic sensor logic
try:
distance_cm = ultrasonic_sensor.distance_cm()
print(f'TAHAP KESIHATAN: {distance_cm:.1f} °C') # Debug log
# DHT sensor logic
try:
dht_sensor.measure()
humidity = dht_sensor.humidity()
display_health_on_oled(distance_cm, Suhu badan:)
except Exception as e:
print(f"Error reading DHT sensor: {e}")
# Rotate servo if distance is below 20 cm
if distance_cm < 20:
rotate_servo_360() # Trigger servo rotation
except Exception as e:
print(f"Error reading sensor: {e}")
sleep(5)