from machine import Pin, PWM, ADC, I2C, SPI
import time
from i2c_lcd import I2cLcd
from ili9341 import Display, color565
from xglcd_font import XglcdFont
import os
from sdcard import SDCard
# **Konfigurasi Pin**
led = Pin(2, Pin.OUT) # LED status sistem
buzzer = PWM(Pin(15), freq=1000, duty=0) # Buzzer dengan PWM
relay = Pin(4, Pin.OUT) # Relay untuk kunci pintu
pir = Pin(13, Pin.IN) # Sensor PIR
switch = Pin(14, Pin.IN) # switch aktifkan sistem
emergency_button = Pin(5, Pin.IN, Pin.PULL_UP) # Butang kecemasan
pot = ADC(Pin(34)) # Potensiometer untuk kawalan
pot.atten(ADC.ATTN_11DB)
servo = PWM(Pin(12), freq=50, duty=0) # Servo untuk pintu
# I2C LCD
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# **SPI untuk LCD ILI9341**
sck = Pin(18)
mosi = Pin(23)
miso = Pin(19)
spi = SPI(1, baudrate=32000000, sck=sck, mosi=mosi, miso=miso)
cs = Pin(5)
dc = Pin(16)
rst = Pin(17)
display = Display(spi, dc=dc, cs=cs, rst=rst)
arcadepix = XglcdFont('ArcadePix9x11.c', 9, 11)
# SPI Configuration for SD Card
spi2 = SPI(2, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs_sd = Pin(25, Pin.OUT)
cs_sd.value(1) # Keep CS high initially
# Create an SDCard instance
sd = SDCard(spi2, cs_sd)
# Mount the SD card
try:
os.mount(sd, "/sd")
print("SD card mounted successfully!")
except OSError as e:
print("Failed to mount SD card:", e)
# Write a file to the SD card
try:
with open("/sd/test.txt", "w") as f:
f.write("Hello from ESP32 with SD card!")
print("File written successfully.")
except OSError as e:
print("Failed to write to the SD card:", e)
# Read the file from the SD card
try:
with open("/sd/test.txt", "r") as f:
print("File content:", f.read())
except OSError as e:
print("Failed to read from the SD card:", e)
# Unmount the SD card (optional)
try:
os.umount("/sd")
print("SD card unmounted.")
except OSError as e:
print("Failed to unmount SD card:", e)
# **Fungsi Paparan LCD untuk Status Sistem**
def display_status(message):
lcd.clear()
lcd.putstr(message)
# **Fungsi Kawalan Pintu (Relay + Servo)**
def lock_door():
relay.on()
servo.duty(115) # Tutup pintu
def unlock_door():
relay.off()
servo.duty(115) # Buka pintu
# **Fungsi Kawalan Buzzer**
def alarm_on():
duty = pot.read() // 4 # Kawalan volume buzzer
buzzer.duty(duty)
def alarm_off():
buzzer.duty(0)
# **Fungsi Paparan ILI9341**
def display_armed():
display.fill_rectangle(0, 0, 240, 320, color565(0, 0, 0)) # Kosongkan skrin (hitam)
display.fill_rectangle(85, 135, 70, 70, color565(0, 255, 0)) # Kotak hijau sebagai indikator "Armed"
def display_intruder():
display.fill_rectangle(0, 0, 240, 320, color565(0, 0, 0)) # Kosongkan skrin (hitam)
display.fill_rectangle(85, 135, 70, 70, color565(255, 0, 0)) # Kotak merah sebagai indikator "Intruder"
display.draw_text(110, 160, "X", arcadepix, color565(255, 255, 255)) # Huruf "X" putih
system_armed = False # Status sistem
display_status("Slide Switch to Arm")
write_log("Waiting...")
while True:
if not system_armed:
if switch.value() == 1: # Slide switch dihidupkan
system_armed = True
led.on()
display_status("ARMED")
write_log("System Armed")
display_armed()
time.sleep(1)
else:
if pir.value(): # Jika sensor PIR mengesan pergerakan
display_status("Intruder Detected")
write_log("Intruder Detected")
alarm_on()
lock_door()
display_intruder()
time.sleep(5)
alarm_off()
time.sleep(0.1)