I've got a basic script used to detect motion as that is registered by a PIR sensor hooked on a Rasberry PI. The script works fine when running as user 'pi' and as root, manually from a terminal. However when I try to run it on start-up it seems like either the sensor doesn't work or my script fails to read the corresponding value from /sys/.
- The script:
echo "4" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio4/direction
while true; do
trap 'echo "4" > /sys/class/gpio/unexport' 0
stat=`cat /sys/class/gpio/gpio4/value`
echo $stat > /home/pi/Desktop/no-movement.txt
if [ $stat == 1 ]
then
echo $stat > /home/pi/Desktop/movement.txt
/usr/bin/omxplayer /home/pi/Desktop/sample.mp3
fi
done
exit 0
- The entry in crontab:
@reboot /home/pi/Desktop/motion_music_player.sh
Remarks:
- As you see in the script, I've got some crude logging in text files. 'no-movement.txt' does get updated, which implies that script is definitely running.
- 'movement.txt' is not created, implying the script can't read the corresponding GPIO value (or something's up with the sensor, but only on startup, the sensor works fine otherwise).