help in getting juice4halt hat working on Pi 3+

  • I am trying to get a juice4halt hat working with my Pi 3+ running LibreELEC. A script makes the hat work. It is suppose to safely shutdown the OS after power is lost, then power off the Pi. It works correctly with the original script running under Raspbian. I modified the script to move the directory structure to storage which is writable under LibreELEC. The modified script works as if it does not see the juice4halt hat and shuts down the OS at the completion of booting, which is what is suppose to happen when the juice4halt hat is not detected.

    Below are the original script and the modified script

    ORIGINAL

    Original

    #!/bin/bash

    #title :shutdown_script

    #description :This script runs on Raspberry Pi equipped with the Juice4Halt module.

    #author :Nelectra s.r.o.

    #date :20190415

    #version :1.2 (bug fixed: collision with the rebootj4h script solved by reading the GPIO25 pin 2x)

    #usage :copy this file to /home/pi/juice4halt/bin/

    # and make it executable: sudo chmod 755 shutdown_script

    #notes :Juice4halt – Supercapacitor UPS for RPi

    #copyright :GNU GPL v3.0

    #==============================================================================

    #create directory for working with GPIO and wait

    echo "25" > /sys/class/gpio/export

    sleep 0.2s

    # use the pin as an open-drain output, send a short LOW pulse (= Booting finished, shutdown_script runs, automatic shutdown enabled)

    # then change to input and listen

    # if a LOW is sent from the J4H then start with the shutdown

    # set GPIO25 as output and set output to LOW

    echo "out" > /sys/class/gpio/gpio25/direction

    echo "0" > /sys/class/gpio/gpio25/value

    sleep 0.1s

    echo "in" > /sys/class/gpio/gpio25/direction

    #wait until pin rises to HI

    sleep 0.1s

    pinval1="1" # 1st reading

    pinval2="1" # 2nd reading

    #to be resistant to short pulses (e.g. 100ms pulse generated by the rebootj4h script) two readings 200ms apart from each other are required

    while [ "$pinval1" != "0" -o "$pinval2" != "0" ]

    do

    #reading the value of the input pin

    pinval1=$(cat /sys/class/gpio/gpio25/value)

    #wait

    sleep 0.2s

    #reading the value of the input pin again 0.2s later

    pinval2=$(cat /sys/class/gpio/gpio25/value)

    done

    # set GPIO25 as output and set output to LOW

    echo "out" > /sys/class/gpio/gpio25/direction

    echo "0" > /sys/class/gpio/gpio25/value

    # wait for system halt

    # after system halted the pin will be automatically switched to input and the level will be pulled up to HI

    # a LOW to HI transition signals to the J4H to turn the power off

    echo ""

    echo "Juice4halt: Power failure, RPi will now shut down."

    sudo halt

    --------------------------------------------

    MODIFIED

    Modified

    #!/bin/bash

    #title :shutdown_script

    #description :This script runs on Raspberry Pi equipped with the Juice4Halt module.

    #author :Nelectra s.r.o.

    #date :20190415

    #version :1.2 (bug fixed: collision with the rebootj4h script solved by reading the GPIO25 pin 2x)

    #usage :copy this file to /home/pi/juice4halt/bin/

    # and make it executable: sudo chmod 755 shutdown_script

    #notes :Juice4halt – Supercapacitor UPS for RPi

    #copyright :GNU GPL v3.0

    #==============================================================================

    #create directory for working with GPIO and wait

    echo "25" > /sys/class/gpio/export

    sleep 0.2s

    # use the pin as an open-drain output, send a short LOW pulse (= Booting finished, shutdown_script runs, automatic shutdown enabled)

    # then change to input and listen

    # if a LOW is sent from the J4H then start with the shutdown

    # set GPIO25 as output and set output to LOW

    # echo "out" > /sys/class/gpio/gpio25/direction

    echo "out" > /storage/home/pi/juice4halt/gpio/gpio25/direction

    # echo "0" > /sys/class/gpio/gpio25/value

    echo "0" > /storage/home/pi/juice4halt/gpio/gpio25/value

    sleep 0.1s

    # echo "in" > /sys/class/gpio/gpio25/direction

    echo "in" > /storage/home/pi/juice4halt/gpio/gpio25/direction

    #wait until pin rises to HI

    sleep 0.1s

    pinval1="1" # 1st reading

    pinval2="1" # 2nd reading

    #to be resistant to short pulses (e.g. 100ms pulse generated by the rebootj4h script) two readings 200ms apart from each other are required

    while [ "$pinval1" != "0" -o "$pinval2" != "0" ]

    do

    #reading the value of the input pin

    pinval1=$(cat /storage/home/pi/juice4halt/gpio/gpio25/value)

    #wait

    sleep 0.2s

    #reading the value of the input pin again 0.2s later

    pinval2=$(cat /storage/home/pi/juice4halt/gpio/gpio25/value)

    done

    # set GPIO25 as output and set output to LOW

    # echo "out" > /sys/class/gpio/gpio25/direction

    echo "out" > /storage/home/pi/juice4halt/gpio/gpio25/direction

    # echo "0" > /sys/class/gpio/gpio25/value

    echo "0" > /storage/home/pi/juice4halt/gpio/gpio25/value

    # wait for system halt

    # after system halted the pin will be automatically switched to input and the level will be pulled up to HI

    # a LOW to HI transition signals to the J4H to turn the power off

    echo ""

    echo "Juice4halt: Power failure, RPi will now shut down."

    halt


    Modified

    ----------------------

    I can # out the last line and let LibreELEC boot and start Kodi. Then SSH into the device and see that the script is running. It is creating the files (value and direction) in the correct location.

    I am a novice, but it acts to me like it is not sensing the voltage on gpio25.

    Can someone give me some help on getting this to work?

    Thanks in advance!

  • I not an expert, but the original script should work with the last line altered to just "halt" as sudo is not required.

    You'll need to test the actual values of the GPIO pins with something like a python script using the RPI.GPIO module. (This can be found in the LE repository --> Program add ons --> Raspberry Pi tools.)

  • I don't know how or why, but it looks like it the original script does work by just removing the sudo in the last line. How can it write to the files in the sys/class/gpio directory? I thought that entire drive was read only...?

    Iridium, thanks for your help!

  • Now that that is working, on to the next piece of the puzzle. Juice4halt supplied a script to be used to reboot the pi instead of using the reboot command to keep the juice4halt hat and pi in synch.

    Options:

    1. Backup the reboot file in /sbin and rename the reboot4h script to reboot and copy it to the /sbin directory. The trouble I am having with this option is that I haven't been able to write to that directory.

    2. Change the reboot button to point to the reboot4h script instead of the reboot command. Is this possible?

    Any other suggestions would be welcome!

    Thanks in advance!

  • Some files have to be writable for the system to operate: However, any changes are NOT carried over on a reboot. (With the exception of /storage)

    1) Will not work /sbin is R/O

    2) Depends: If you can change the button to point to

    /storage/bin/rebootj4h

    then it should work.

  • Change systemd's reboot behavior. Copy /usr/lib/systemd/system/reboot.target (or .../systemd-reboot.service) to /storage/.config/system.d and change it to your needs.