0

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/.

  1. 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

  1. The entry in crontab:
@reboot /home/pi/Desktop/motion_music_player.sh

Remarks:

  1. 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.
  2. '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).
mihalios
  • 828
  • 1
  • 6
  • 12
  • I would add a `sleep 120` at the very top of the script (after `#!/bin/bash of course), to let things get going first (just a guess). Good luck! – shellter Nov 08 '20 at 16:26
  • 1
    Don't use sysfs interface. It's deprecated and will be disabled by default in new kernel. Use `libgpiod` and its tools. – 0andriy Nov 12 '20 at 11:39
  • 1
    @shellter thanks, but that didn't do the trick. @0andriy using ```ligpiod``` did the trick. If you'd like to get the accepted answer can you post an answer with some documentation and I'll mark it as the correct one. Thanks both for taking the time. – mihalios Nov 17 '20 at 23:34

0 Answers0