from machine import Pin
import time
import ifttt
import network
import wifi
# Define GPIO pins
led_pin = 14
sensor_pin = 12
# Initialize GPIO pins
led = Pin(led_pin, Pin.OUT)
sensor = Pin(sensor_pin, Pin.IN)
def connect_to_wifi():
print("Connecting to WiFi...", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(wifi.ssid, wifi.password)
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
def main_loop():
while True:
motion_detected = sensor.value()
print("Motion Detected:", motion_detected)
if motion_detected:
led.on()
# Connect to IFTTT and send a notification when motion is detected
ifttt.alert_motion(motion_detected)
else:
led.off()
time.sleep(5)
if __name__ == '__main__':
connect_to_wifi()
main_loop()