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
#2
You lost any indentation (if you had any) when you posted the code. Try pasting between the code tags using
"Cntrl + Shift + V"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
So I've made a little progress. The screensaver now successfully starts and stops upon the PIR receiving motion, however the script doesn't then loop, and i receive the same error as before. For some reason i can't seem to paste the code without removing the formatting, but there's a tab on the line (if gpio.input 11...) and two tabs on each of the bottom three lines. 


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 11, then complete 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
Reply
#4
In Python, use spaces (4 recommended) not tabs.  As it is, it's difficult to tell what you want indented and what you don't.  Replace the tabs with spaces and paste your code using the "Cntl + Shift + V" combo

EDIT:
I'll also add, use the "Preview Post" button to ensure it is as you expect.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
I've actually re-written this code to a streamlined version below...

import RPi.GPIO as GPIO
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN)
while True:
    if GPIO.input(11): # Waits for motion signal from PIR on GPIO pin 11 then complete rest and exit
        print ("deactivating screensaver")
        os.system("xscreensaver-command -deactivate")
This uses the xscreensaver module. The time for the screensaver to come on is controlled by the module, and the 'off' is controlled by this code. I'd now like this to auto-run on start-up. I've been trying to do so all day but have had little success. Would you possibly know how to achieve this?
Reply
#6
Make it part of the boot sequence. Check the docs for your OS on the proper way to do that.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 2,201 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 2,208 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