import machine
import time
import urequests
# Wi-Fi configuration
WIFI_SSID = "Infinix NOTE 7"
WIFI_PASSWORD = "khan2453"
# ThingSpeak configuration
THINGSPEAK_API_KEY = "BFM1TENWC2H6BJYM"
THINGSPEAK_URL = "https://api.thingspeak.com/update.json"
# PIR sensor configuration
PIR_SENSOR_PIN = 14 # GPIO pin where PIR sensor is connected
BUZZER_PIN = 12 # GPIO pin where the buzzer is connected
# Initialize the PIR sensor and buzzer
pir_sensor = machine.Pin(PIR_SENSOR_PIN, machine.Pin.IN)
buzzer = machine.Pin(BUZZER_PIN, machine.Pin.OUT)
# Function to send an alert to ThingSpeak
def send_alert():
payload = {"api_key": BFM1TENWC2H6BJYM, "field1": "1"} # Update a field with 1 to indicate motion
try:
response = urequests.post(https://thingspeak.com/channels/2268154/api_keys, json=payload)
response.close()
except Exception as e:
print("Error sending alert:", e)
# Main loop
while True:
if pir_sensor.value() == 1: # Motion detected
print("Motion detected!")
buzzer.on() # Turn on the buzzer
send_alert() # Send an alert to ThingSpeak
time.sleep(5) # Buzzer alarm duration (in seconds)
buzzer.off() # Turn off the buzzer
time.sleep(1) # Check PIR sensor every second