Python Forum
PIR Sensor to control screensaver
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIR Sensor to control screensaver
#1
Hi All!

I'm fairly new to programming, and am currently trying to build a 'magic mirror' for my partners 21st birthday present. As part of this i'm trying to incorporate a PIR sensor with a raspberry pi. My hope is that by default the screensaver (which is just a black screen) is on. When a user swipes the PIR sensor, the screensaver is turned off for 30 minutes. Once the 30 minutes is up, the screensaver comes back on again. This needs to then loop constantly. My code so far is this...

import os #for OS program calls
import sys #For Clean sys.exit command
import time #for sleep/pause 
import RPi.GPIO as GPIO #read the GPIO pins

GPIO.setmode(GPIO.BCM)

pir_pin = 11 # Sensor pin 11 for data out of the PIR sensor 

GPIO.setup(pir_pin, GPIO.IN)
print("Motion Activated (PIR) Screen Saver")
print("less than 30 seconds to leave the sensor area...")
time.sleep(20)
os.system("xscreensaver-command -activate") #OS command for Xscreensaver program = Activate, turns on screensaver instantly

while True:  # endless loop, waiting for nothing. Use Control+C to exit
if GPIO.input(11): # Waits for motion signal from PIR on GPIO pin 25, then com[plete rest and exit
print ("working")
os.system("xscreensaver-command -deactivate") ##OS command for Xscreensaver program = Activate, turns on screensaver instantly
print("PIR Screen Saver Deactivated")
GPIO.cleanup() #reset GPIO
GPIO.setup(pir_pin, GPIO.IN)
The code successfully starts the screensaver, however, quickly stops it and returns the error....



Error:
"Traceback (most recent call last):   File "/home/pi/test.py", line 17, in <module>     if GPIO.input(11): # Waits for motion signal from PIR on GPIO pin 25, then com[plete rest and exit RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)   "
Can anyone help me to finish off this code and solve the error? 

Massive thanks in advance!
Reply


Messages In This Thread
PIR Sensor to control screensaver - by toothie96 - May-11-2017, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 2,218 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 2,223 Jun-24-2018, 08:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020