from machine import Pin , ADC , PWM , SoftI2C
from time import sleep
import ssd1306
import moon
ldr = ADC(0)
pir = Pin(12, Pin.IN )
button = Pin(0 , Pin.IN , Pin.PULL_UP)
red = Pin(2 , Pin.OUT)
green = Pin(15 , Pin.OUT)
blue =Pin(10 , Pin.OUT)
buzzer = Pin(16 , Pin.OUT)
servo = PWM(Pin(13))
servo.freq(50)
i2c = SoftI2C(scl = Pin(5) , sda = Pin(4))
oled = ssd1306.SSD1306_I2C(128 , 64 , i2c)
Security_Token = ""
Device_Token = ""
Door_Widget_ID = 3098
Light_Widget_ID =3100
Status_Widget_ID =3102
moon.connect()
moon.setup(Security_Token , Device_Token)
while True:
light = ldr.read()
motion = pir.value()
Door_Status,_ = moon.status(Door_Widget_ID)
Light_Status,_ = moon.status(Light_Widget_ID)
oled.fill(0)
if light <300 and Light_Status:
red.value(1)
green.value(1)
blue.value(1)
oled.text("Lights ON" , 0 , 0)
else:
red.value(0)
green.value(0)
blue.value(0)
oled.text("Lights OFF" , 0 , 0)
if motion ==1:
buzzer.value(1)
oled.text("MOTION" , 0 , 20)
moon.send(Status_Widget_ID, "Someone is at the door!")
else:
buzzer.value(0)
oled.text("ALL CLEAR" , 0 , 20)
moon.send(Status_Widget_ID , "All quiet")
if button.value()==0 or Door_Status:
servo.duty(77)
oled.text("DOOR OPEN" , 0 , 40)
else:
servo.duty(25)
oled.text("DOOR CLOSED" , 0 , 40)
oled.show()
sleep(1)