1
0
Fork 0
Browse Source

port startup and shutdown scripts from Light-Meter repo.

unstable
Craig Oates 4 years ago
parent
commit
6da8436227
  1. 17
      auto-start.sh
  2. 9
      make-log-files.sh
  3. 6
      relay.py
  4. 38
      shutdown.sh
  5. 50
      startup.sh

17
auto-start.sh

@ -1,17 +0,0 @@
#!/bin/bash
# Auto Start
# ================================================================
# This script is used to start the roaming-light-relay controller
# when the Raspberry Pi is turned on. The main thing is does is
# activate the projects virtual environment before running the
# actual program in this project. This file is referenced in
# /etc/xdg/lxsession/LXDE-pi/autostart. The file just referenced
# is a Raspbian specific file and I have found it to do what I want
# better than using crontab -- like traditional Linux set-ups.
# Refer to your Roaming Light: Project Notes file for more info.
# IT IS ASSUMED THIS FILE IS IN THE PROJECTS ROOT FOLDER.
# IT WILL NOT WORK AS INTENDED OUTWISE.
source /home/pi/repos/roaming-light-relay/proj-env/bin/activate
python3 /home/pi/repos/roaming-light-relay/relay.py

9
make-log-files.sh

@ -0,0 +1,9 @@
#!/bin/bash
# This script creates the log files and the fold they will reside in.
# This script must be executed before you set-up the start-up and
# shutdown cron-jobs on the system.
mkdir ~/logs/
touch ~/logs/startup-logs.txt
touch ~/logs/shutdown-logs.txt

6
relay.py

@ -21,10 +21,12 @@ def main():
the_reading = data.get("reading")
# print(the_reading) # For testing.
if the_reading > 30:
print(f"[INFO] Light is on -- {the_reading}.")
# Print for testing.
# print(f"[INFO] Light is on -- {the_reading}.")
GPIO.output(relay_pin, GPIO.HIGH)
else:
print(f"[INFO] Light is off -- {the_reading}.")
# Print for testing.
# print(f"[INFO] Light is off -- {the_reading}.")
GPIO.output(relay_pin, GPIO.LOW)
except KeyboardInterrupt:
print("Keyboard Interrupt: quitting program.")

38
shutdown.sh

@ -0,0 +1,38 @@
#!/bin/bash
# Shutdown Script
# ====================================================================
# I wrote this script with the intention of using it as part of a
# cron-job. Before you set up the cron-job, you must make sure you
# have either ran the "make-log-files.sh" script or created the
# appropriate log file and folder at the location specified at
# "logFile" below.
# As an aside, I usually set-up as alias for "shutdown" called,
# "powerdown". When I enter "powerdown" into the terminal, this script
# should run and then the "shutdown" command is ran at the end.
logDate=$(date '+%Y-%m-%dT%TZ')
logFile="/home/rtrp/logs/shutdown-logs.txt"
mainURL="http://3.9.19.84/api/status/update" # Make sure this is valid.
getApiUrl () {
case $HOSTNAME in
(factory1) apiURL="${mainURL}/1";;
(factory2) apiURL="${mainURL}/2";;
(factory3) apiURL="${mainURL}/3";;
(gallery1) apiURL="${mainURL}/4";;
(gallery2) apiURL="${mainURL}/5";;
(gallery3) apiURL="${mainURL}/6";;
esac
}
logStatusChange () {
cat << EOF >> $logFile
$logDate
EOF
}
logStatusChange
getApiUrl
curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/html' -d '{"status": "off", "time": "'${logDate}'", "token": "QWERTYuiopasdfghjklzxcvbnm_1234567890"}' "${apiURL}"
/sbin/shutdown -h now

50
startup.sh

@ -0,0 +1,50 @@
#!/bin/bash
# Startup Script
# ====================================================================
# I wrote this script with the intention of using it to start a
# cron-job -- when the Raspberry Pi is turned on. Before you set the
# cron-job up, you must make sure you have either ran the
# "make-log-files.sh" script or created the appropriate log file and
# folder at the location specified at "logFile" below.
# ====
# Please note: This script calls another (Python) script at the end of
# it. The code in the Python script (relay.py) is an infinite-loop
# so you will need to kill it manually or turn-off the Raspberry Pi.
# ====
# I put the "sleep 60" call at the start to reduce any errors
# occurring because a part of the system (I.E. curl) has not finished
# loading. 60 seconds is a little excessive but I wrote this script
# with the expectation of it running on an unmanned Raspberry Pi. So,
# reliable/consistent behaviour is my preference over "fast start-up
# times".
sleep 60
logDate=$(date '+%Y-%m-%dT%TZ')
logFile="/home/rtrp/logs/startup-logs.txt"
mainURL="http://3.9.19.84/api/status/update" # Make sure this is valid.
getApiUrl () {
case $HOSTNAME in
(factory1) apiURL="${mainURL}/1";;
(factory2) apiURL="${mainURL}/2";;
(factory3) apiURL="${mainURL}/3";;
(gallery1) apiURL="${mainURL}/4";;
(gallery2) apiURL="${mainURL}/5";;
(gallery3) apiURL="${mainURL}/6";;
esac
}
logStatusChange () {
cat << EOF >> $logFile
$logDate
EOF
}
logStatusChange
getApiUrl
curl -S -X POST --header 'Content-Type: application/json' --header 'Accept: text/html' -d '{"status": "on", "time": "'${logDate}'", "token": "QWERTYuiopasdfghjklzxcvbnm_1234567890"}' "${apiURL}"
python3 /home/rtrp/repos/relay/relay.py