From 6da8436227b9578881c5f191b94e057d6145e57a Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sun, 12 Jan 2020 23:37:41 +0000 Subject: [PATCH] port startup and shutdown scripts from Light-Meter repo. --- auto-start.sh | 17 ---------------- make-log-files.sh | 9 +++++++++ relay.py | 6 ++++-- shutdown.sh | 38 +++++++++++++++++++++++++++++++++++ startup.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 19 deletions(-) delete mode 100755 auto-start.sh create mode 100644 make-log-files.sh create mode 100755 shutdown.sh create mode 100755 startup.sh diff --git a/auto-start.sh b/auto-start.sh deleted file mode 100755 index 292a708..0000000 --- a/auto-start.sh +++ /dev/null @@ -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 diff --git a/make-log-files.sh b/make-log-files.sh new file mode 100644 index 0000000..a15c828 --- /dev/null +++ b/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 diff --git a/relay.py b/relay.py index ab4713a..b703ac6 100644 --- a/relay.py +++ b/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.") diff --git a/shutdown.sh b/shutdown.sh new file mode 100755 index 0000000..aa5b6d4 --- /dev/null +++ b/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 diff --git a/startup.sh b/startup.sh new file mode 100755 index 0000000..7f80394 --- /dev/null +++ b/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