import RPi.GPIO as GPIO
import requests
import time
channel_id=2504862
# ThingSpeak API endpoint and API key
THINGSPEAK_API_ENDPOINT = 'https://api.thingspeak.com/update'
API_KEY = 9Z4W4L4FYCV61YT0
# PIR sensor GPIO pin
PIR_PIN = 17
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR_PIN, GPIO.IN)
def send_to_thingspeak(data):
payload = {'api_key': API_KEY, 'field1': data}
requests.post(THINGSPEAK_API_ENDPOINT, params=payload)
def main():
setup()
print("PIR Module Test (CTRL+C to exit)")
time.sleep(2)
print("Ready")
try:
while True:
if GPIO.input(PIR_PIN):
print("Motion Detected!")
send_to_thingspeak(1) # You can send any value to ThingSpeak
time.sleep(5) # Wait for 5 seconds to avoid multiple detections
else:
print("No Motion Detected.")
time.sleep(0.1)
except KeyboardInterrupt:
print("Quit")
GPIO.cleanup()
if __name__ == '__main__':
main()