Skip to content

Instantly share code, notes, and snippets.

@shantigilbert
Last active August 15, 2021 19:40
Show Gist options
  • Save shantigilbert/6f12dbc03536186978c8f987c3fec2b5 to your computer and use it in GitHub Desktop.
Save shantigilbert/6f12dbc03536186978c8f987c3fec2b5 to your computer and use it in GitHub Desktop.
Script to run at boot and uses a switch to select timelapse.sh or the RPI Camera Web Interface
#!/bin/bash
# RPI Camera Web Interface should be cloned in /home/pi/RPi_Cam_Web_Interface and needs to already be installed, using nginx!
# This file (switch.sh) needs to be in /home/pi/switch.sh
#
# Changes needed to /etc/rc.local
#
# # We need to comment out the autostart for the RPI Camera Web Interface (if available)
# #START RASPIMJPEG SECTION
# #mkdir -p /dev/shm/mjpeg
# #chown www-data:www-data /dev/shm/mjpeg
# #chmod 777 /dev/shm/mjpeg
# #sleep 4;su -c 'raspimjpeg > /dev/null 2>&1 &' www-data
# #if [ -e /etc/debian_version ]; then
# # sleep 4;su -c 'php /var/www/html/schedule.php > /dev/null 2>&1 &' www-data
# #else
# # sleep 4;su -s '/bin/bash' -c 'php /var/www/html/schedule.php > /dev/null 2>&1 &' www-data
# #fi
# #END RASPIMJPEG SECTION
#
# # Then add this
# chgrp -R dialout /sys/class/gpio
# chmod -R g+rw /sys/class/gpio
#
# GPIO=10
# LED=27
#
# echo "${GPIO}" > /sys/class/gpio/export
# echo "${LED}" > /sys/class/gpio/export
# echo "in" > /sys/class/gpio/gpio"${GPIO}"/direction
# echo "out" > /sys/class/gpio/gpio"${LED}"/direction
#
# /bin/bash /home/pi/switch.sh &
# End or changes to /etc/rc.local
# We use GPI pin 10 for the switch (it's close to 3.3v) and 27 for the LED (close to gound)
GPIO=10
LED=27
# We need to set this to a value other than 0 or 1
turned=2
# Prepare the pins, might not be needed since /etc/rc.local should initialize them
if [ ! -d /sys/class/gpio/gpio${GPIO} ]; then
echo "${GPIO}" > /sys/class/gpio/export
fi
if [ ! -d /sys/class/gpio/gpio${LED} ]; then
echo "${LED}" > /sys/class/gpio/export
fi
echo "in" > /sys/class/gpio/gpio"${GPIO}"/direction
echo "out" > /sys/class/gpio/gpio"${LED}"/direction
#/home/pi/RPi_Cam_Web_Interface/stop.sh
#killall timelapse.sh > /dev/null 2>&1
while true; do
if [ 1 == "$(</sys/class/gpio/gpio"${GPIO}"/value)" ]; then
# printf "Timelapse Service On \r"
if [ "$turned" != "1" ]; then
$(
echo "1" > /sys/class/gpio/gpio"${LED}"/value
sleep 5
echo "0" > /sys/class/gpio/gpio"${LED}"/value
)&
echo -n 'tl 0' > /var/www/html/FIFO
/home/pi/RPi_Cam_Web_Interface/stop.sh
systemctl stop nginx
sleep 0.5
/home/pi/timelapse.sh &
turned=1
fi
else
# printf "RPI Cam Interface On \r"
if [ "${turned}" != 0 ]; then
echo "0" > /sys/class/gpio/gpio"${LED}"/value
killall timelapse.sh > /dev/null 2>&1
systemctl start nginx
sleep 1
/home/pi/RPi_Cam_Web_Interface/start.sh &
# If you want to autostart the timelapse uncomment these next 2 lines
#wait
#echo -n 'tl 1' > /var/www/html/FIFO
turned=0
fi
fi
#reduce CPU usage
sleep 0.5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment