from machine import Pin, PWM, ADC, I2C
from ssd1306 import SSD1306_I2C
from keypad import Keypad
import dht
import time
# === OLED ===
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
# === Sensors ===
dht_sensor = dht.DHT22(Pin(15))
mq2 = ADC(Pin(34)); mq2.atten(ADC.ATTN_11DB)
ldr = ADC(Pin(35)); ldr.atten(ADC.ATTN_11DB)
pir = Pin(4, Pin.IN)
# === Output ===
servo = PWM(Pin(23), freq=50); servo.duty(77)
buzzer = PWM(Pin(19), freq=1000); buzzer.duty(0)
led_yellow = Pin(5, Pin.OUT)
led_red = Pin(18, Pin.OUT)
# === Keypad ===
rows = [Pin(x, Pin.OUT) for x in (13, 12, 14, 27)]
cols = [Pin(x, Pin.IN, Pin.PULL_DOWN) for x in (26, 25, 33, 32)]
keys = [['1','2','3','A'], ['4','5','6','B'],
['7','8','9','C'], ['*','0','#','D']]
keypad = Keypad(rows, cols, keys)
# === PIN Logic ===
entered_pin = ""
correct_pin = "0205"
sensor_mode = False
last_sensor_key = None
# === ThingSpeak ===
THINGSPEAK_API_KEY = "RGEN1D83NY2CDS7Y"
THINGSPEAK_URL = "https://api.thingspeak.com/update"
upload_interval = 15
last_upload = time.time()
# === Functions ===
def show_pin_screen():
oled.fill(0)
oled.text("Smart Office", 0, 0)
oled.text("Enter PIN:", 0, 20)
oled.text(entered_pin, 0, 40)
oled.show()
def display_sensor(key):
oled.fill(0)
try:
if key == 'A':
dht_sensor.measure()
t = dht_sensor.temperature()
h = dht_sensor.humidity()
oled.text("Temp: {:.1f}C".format(t), 0, 0)
oled.text("Hum : {:.1f}%".format(h), 0, 16)
elif key == 'B':
oled.text("PIR Sensor:", 0, 0)
oled.text("Motion" if pir.value() else "No Motion", 0, 20)
elif key == 'C':
oled.text("LDR:", 0, 0)
oled.text(str(ldr.read()), 0, 20)
elif key == 'D':
oled.text("Gas:", 0, 0)
oled.text(str(mq2.read()), 0, 20)
except:
oled.text("Sensor Error", 0, 0)
oled.text("*: exit", 0, 56)
oled.show()
# === Buzzer bip ringkas ===
def buzzer_beep(duration=0.05):
buzzer.duty(512)
time.sleep(duration)
buzzer.duty(0)
# === Buzzer ===
def buzzer_quick_alert(duration=2):
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < duration * 1000:
buzzer.duty(512)
time.sleep(0.1)
buzzer.duty(0)
time.sleep(0.1)
buzzer.duty(0)
def handle_keypad():
global entered_pin, sensor_mode, last_sensor_key
key = keypad.get_key()
if key:
print("Key:", key)
buzzer_beep()
if sensor_mode:
if key == '*':
sensor_mode = False
entered_pin = ""
show_pin_screen()
else:
last_sensor_key = key
return
if key == '*':
entered_pin = ""
elif key == '#':
if entered_pin == correct_pin:
oled.fill(0)
oled.text("Access Granted", 0, 20)
oled.show()
servo.duty(30)
time.sleep(3)
servo.duty(77)
else:
oled.fill(0)
oled.text("Access Denied", 0, 20)
oled.show()
time.sleep(2)
entered_pin = ""
elif key in ['A','B','C','D']:
sensor_mode = True
last_sensor_key = key
elif key.isdigit() and len(entered_pin) < 4:
entered_pin += key
show_pin_screen()
def hantar_ke_thingspeak(temp, hum, pir_val, ldr_val, gas_val):
data = {
"api_key": THINGSPEAK_API_KEY,
"field1": temp,
"field2": hum,
"field3": pir_val,
"field4": ldr_val,
"field5": gas_val
}
headers = {'Content-Type': 'application/json'}
try:
response = urequests.post(THINGSPEAK_URL, headers=headers, json=data)
print("Hantar ke ThingSpeak:", response.text)
response.close()
except Exception as e:
print("Gagal hantar ke ThingSpeak:", e)
# === Main Loop ===
while True:
handle_keypad()
if sensor_mode and last_sensor_key:
display_sensor(last_sensor_key)
else:
show_pin_screen()
motion = pir.value()
motion_status = 1 if motion else 0
if motion:
buzzer_quick_alert()
led_yellow.value(ldr.read() > 2000)
gas = mq2.read()
if gas > 400:
led_red.on()
time.sleep(0.1)
led_red.off()
time.sleep(0.1)
else:
led_red.off()
if time.time() - last_upload > upload_interval:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
except:
temp = 0
hum = 0
hantar_ke_thingspeak(temp, hum, motion_status, ldr.read(), gas)
last_upload = time.time()
time.sleep(0.1)