dfd_rgss
Posts: 4
Joined: Thu Oct 02, 2014 8:31 pm

PIR Sensor and Keyboard Space Key

Thu Nov 06, 2014 2:02 am

I am using both models - B and B+ - in a kiosk type environment. I am also using a PIR sensor (http://www.amazon.com/gp/product/B00KKR ... UTF8&psc=1) to turn on the screen on when the sensor is tripped. For the most part, this works.

Where I am having issue is here - When the sensor is connected and the python code I have is running, anytime I use a keyboard to type something out (URL, login info, file editing), I am automatically getting spaces added at the rate of 1 space per second. I had originally thought it was related to my USB keyboard/track pad combo.

Here is my python code:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
import subprocess
from subprocess import Popen, PIPE

sensorPin = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

key_seq = '''key A'''

def keypress(sequence):
    p = Popen(['xte'], stdin=PIPE)
    p.communicate(input=sequence)

while True:
    time.sleep(1)
    prevState = "LOW"
    currState = GPIO.input(sensorPin)
    if currState != prevState:
        newState = "HIGH" if currState else "LOW"
        if currState:
           keypress(key_seq)/code]

I am sure now that this is directly related to how I am using the "key_seq" to turn the screen back on I am just not sure how or why.

Where can I look to get more info on this?

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: PIR Sensor and Keyboard Space Key

Thu Nov 06, 2014 3:05 pm

The code is easier to read and debug if you put it in a [/b] block, especially with python's indentation rules.

Gr.
Dirk.

dfd_rgss
Posts: 4
Joined: Thu Oct 02, 2014 8:31 pm

Re: PIR Sensor and Keyboard Space Key

Thu Nov 06, 2014 3:07 pm

Sorry - I thought I did. Try this:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
import subprocess
from subprocess import Popen, PIPE

sensorPin = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevState = False
currState = False

key_seq = '''key A'''

def keypress(sequence):
p = Popen(['xte'], stdin=PIPE)
p.communicate(input=sequence)

while True:
time.sleep(1)
prevState = "LOW"
currState = GPIO.input(sensorPin)
if currState != prevState:
newState = "HIGH" if currState else "LOW"
if currState:
keypress(key_seq)

dfd_rgss
Posts: 4
Joined: Thu Oct 02, 2014 8:31 pm

Re: PIR Sensor and Keyboard Space Key

Thu Nov 06, 2014 7:24 pm

So I have made some progress on this issue - I changed my PIR python code over to this:
(from http://www.ofbrooklyn.com/2014/01/2/bui ... -detector/)

Code: Select all

#!/usr/bin/env python

import sys
import time
import RPi.GPIO as io
import subprocess

io.setmode(io.BCM)
SHUTOFF_DELAY = 60 # seconds
PIR_PIN = 4       # 7 on the board

def main():
    io.setup(PIR_PIN, io.IN)
    turned_off = False
    last_motion_time = time.time()

    while True:
        if io.input(PIR_PIN):
            last_motion_time = time.time()
            print ".",
            sys.stdout.flush()
            if turned_off:
                turned_off = False
                turn_on()
        else:
            if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
                turned_off = True
                turn_off()
        time.sleep(.1)

def turn_on():
    subprocess.call("sh /home/pi/screen_status.sh on", shell=True)

def turn_off():
    subprocess.call("sh /home/pi/screen_status.sh off", shell=True)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        io.cleanup()
and created this file - screen_status.sh
(from https://gist.github.com/AGWA/9874925)

Code: Select all

#!/bin/sh
 
# Enable and disable HDMI output on the Raspberry Pi
 
is_off ()
{
	tvservice -s | grep "TV is off" >/dev/null
}
 
case $1 in
	off)
		tvservice -o
	;;
	on)
		if is_off
		then
			tvservice -p
			curr_vt=`fgconsole`
			if [ "$curr_vt" = "1" ]
			then
				chvt 2
				chvt 1
			else
				chvt 1
				chvt "$curr_vt"
			fi
		fi
	;;
	status)
		if is_off
		then
			echo off
		else
			echo on
		fi
	;;
	*)
		echo "Usage: $0 on|off|status" >&2
		exit 2
	;;
esac
 
exit 0
This change has fixed my automatic space issue and I am no longer using a key sequence to turn of the xscreensaver.

With that said, I would prefer to use xscreensaver however I cannot get the command "xscreensaver-command -activate" to work from my pir.py file. I believe this to be due to the fact the python script is being launched as sudo in the /etc/xdg/lxsession/LXDE/autostart" file. If this is a correct assumption, how can I structure these 2 files to (1) start as the user pi and (2) be able to use the commands "xscreensaver-command -activate" and "xscreensaver-command -deactivate" in my pir.py file?

Return to “General discussion”