import machine
import utime
import network
import urequests
# Your Blynk Auth Token
BLYNK_AUTH = 'Your_Blynk_Auth_Token'
# Wi-Fi credentials
SSID = 'Your_SSID'
PASSWORD = 'Your_PASSWORD'
# Connect to Wi-Fi
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
utime.sleep(1)
print("Connected to Wi-Fi")
# Send notification to Blynk
def send_blynk_notification(message):
url = f'http://blynk-cloud.com/{BLYNK_AUTH}/notify'
data = {'body': message}
response = urequests.post(url, json=data)
response.close()
# PIR sensors and peripherals setup
sensor_pir1 = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_pir2 = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN)
led = machine.Pin(15, machine.Pin.OUT)
buzzer = machine.Pin(14, machine.Pin.OUT)
print("Hello!")
def pir_handler(pin):
utime.sleep_ms(100)
if pin.value():
message = ""
if pin is sensor_pir1:
message = "ALARM! Motion detected in bedroom!"
print(message)
elif pin is sensor_pir2:
message = "ALARM! Motion detected in living room!"
print(message)
send_blynk_notification(message)
for i in range(50):
led.toggle()
buzzer.toggle()
utime.sleep_ms(100)
# Set up the interrupt handlers
sensor_pir1.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_pir2.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
# Connect to Wi-Fi
connect_wifi()
# Main loop
while True:
led.toggle()
utime.sleep(5)