@james mutch
Hi James
I have managed to cobble together yet another script that has now solved all my problems.
This may not work for you, James, but I am posting this for everyone needing the same solution. You may be able to adapt this, or create something from it!
PLEASE BACK UP THE RPI FIRST! I DON'T ACCEPT ANY RESPONSABILITY.
My system:
Rpi Model 3 b+ running headless.
x708 with 3000mah connected to PH2.0-2P
External power supplied to Type-C
================
I will describe the scenario that I needed and the solution.
Scenario:
My pi and x708 are going to be installed in my car. The power will come from the fuse box and power will ONLY be available when the engine is on. There needs to be a variable delay on turning the Rpi on, since my car is a diesel and I need to wait for the glow plugs to warm up before starting the engine. On turning the engine off, the Rpi needs to turn off gracefully after a variable delay and following this, the x708 needs to turn off too (i.e fan needs stop running). If the x708 were not to turn off, then the battery attached to the Rpi would discharge and I would end up with a constant cycle of charging (when engine on) and discharging (when engine off).
Now for the solution:
First of all, the provided scripts do not do what I needed, but I have used bits of code.
1) Go to your command line and type: sudo nano /etc/rc.local
You will see something like this:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
#Start power management on boot
#/etc/x728pwr.sh &
/var/www/html/scripts/total-sd-on-pl.sh
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s
exit 0
Comment out (put a # in front) any line that has the 'x728' in it. There may be a few. I have actually deleted all of them bar one as an example.
Then add a line with the path to the file that we will create later. In my case that file is called 'total-sd-on-pl.sh' and is in the 'var/www/html/scripts' directory. Of course you can name the file anything you want and place it anywhere you like. Just remember to put a '/' in front which makes it executable when the Rpi starts up.
Now, 'Ctrl + x' to close, 'y' to save and enter.
2) Now create the new file:
Wherever you decided to have the new file, create the file with the name you decided to give it by typing 'sudo touch xxx.sh'.
Now, make the file executable by typing 'sudo chmod 755 xxx.sh'.
Now, open the file by typing 'sudo nano xxx.sh'
Copy and paste the code below into xxx.sh
#!/bin/bash
POWERLOSS=6
SHUTDOWN=5
BOOT=13
interval=3 # time to pause in seconds between checking if power has been lost
shuttime=10 # the power off time in seconds
REBOOTPULSEMINIMUM=200
REBOOTPULSEMAXIMUM=600
sudo echo "$POWERLOSS" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$POWERLOSS/direction
while [ 1 ]; do
powerlosssignal=$(cat /sys/class/gpio/gpio$POWERLOSS/value)
if [ $powerlosssignal = 1 ]; then
echo "Power loss"
echo "Shutdown procedure started..."
sudo echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
pulseStart=$(date +%s%N | cut -b1-13)
while [ $shutdownSignal = 0 ]; do
sleep 0.02
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
echo "Shutting down Rpi on pin $SHUTDOWN and X708 on pin $BOOT"
sudo echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
sudo shutdown -P +$shuttime "Shutting down in $shuttime seconds"
exit
fi
done
else
echo "Power on"
sleep $interval
fi
done
Now, 'Ctrl + x ' to save, the 'y' and enter to close.
At the top of this file you can see two variables, feel free to change these.
3) Now, if you want to add a delay to the start of the Rpi (not the x708, as that will start as soon as it receives power):
Type: 'sudo nano /boot/config.txt'
At the bottom of this file, add:
#Wait for x seconds in start.elf before loading kernel.
#Default 1
boot_delay=15
In this case, I have added a delay of 15 seconds. Change as you need.
Again, 'Ctrl + x ' to save, the 'y' and enter to close.
4) Now reboot with 'sudo reboot'
ONE more IMPORTANT thing:
The jumpers on the x708. I have the 'PLD' and 'AON' shortened. NOT 'ASD'!
5) To test the system:
On powering up, the x708 will start and shortly after the Rpi will start.
If the power is removed, the Rpi will shut down (after a few seconds) followed by the x708.
I know the code is not the most beautiful, and could do with further improvements, but it does the job.