Skip to content
x708 software power shutdown | Geekworm

x708 software power shutdown


  • Hi

    I have the x708 (but waiting for a battery to arrive).

    I am trying to shut down the RPI by doing the following:

    cd /

    cd x728

    x728off

    All I am getting is:

    on the first try - "X728 Shutting down... "

    and on the next attempts - "/usr/local/bin/x728softsd.sh: line 5: echo: write error: Device or resource busy
    X728 Shutting down..."

    But, in both cases, nothing happens!

    Please help.



  • @George Feichter 

    Hi,

     

    sudo nano x728.sh
    #because X708 doesn't support RTC function, so we need to remove or comment out (add # at the beginning of the line) the following 4 lines
    #X728 RTC setting up
    #sudo sed -i '$ i rtc-ds1307' /etc/modules
    #sudo sed -i '$ i echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /etc/rc.local
    #sudo sed -i '$ i  hwclock -s' /etc/rc.local


    or refer to: https://raspberrypiwiki.com/X708-Software

  • @Harry Huang 

    Thanks Harry.

    I have commented the four lines and rebooted, but it still does not shut down the pi or the hat.

    Please advise.

    Many thanks.


  • @George Feichter 

    Do you try to burn the last raspbian image os? 


  • Yes, I have buster and all updated.


  • @George Feichter 

    Hi, could you tell me which reaspberry pi image os? pi 3 or pi 4?

    We don't found some error about script, it's very strange 


  • @George Feichter 

    Hi

    Try to insert a new line, and type 'sleep 1s' before '#x728 Powering on /reboot /full shutdown through hardware' line;

     


  • Hi Harry

    I now have added 1 second sleep to:

    x728softsd.sh

    #X728 RTC setting up
    #sudo sed -i '$ i rtc-ds1307' /etc/modules
    #sudo sed -i '$ i echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /$
    #sudo sed -i '$ i hwclock -s' /etc/rc.local
    #sudo sed -i '$ i #Start power management on boot' /etc/rc.local
    
    sleep 1s
    
    #x728 Powering on /reboot /full shutdown through hardware
    #!/bin/bash
    
        sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
    
        echo '#!/bin/bash
    
    SHUTDOWN=5
    REBOOTPULSEMINIMUM=200
    REBOOTPULSEMAXIMUM=600
    echo "$SHUTDOWN" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
    BOOT=12
    echo "$BOOT" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio$BOOT/direction
    echo "1" > /sys/class/gpio/gpio$BOOT/value
    
    echo "X728 Shutting down..."
    
    while [ 1 ]; do
      shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
      if [ $shutdownSignal = 0 ]; then
        /bin/sleep 0.2
      else
        pulseStart=$(date +%s%N | cut -b1-13)
        while [ $shutdownSignal = 1 ]; do
          /bin/sleep 0.02
          if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM $
            echo "X728 Shutting down", SHUTDOWN, ", halting Rpi ..."
            sudo poweroff
            exit
          fi
          shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
        done
        if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ];$
          echo "X728 Rebooting", SHUTDOWN, ", recycling Rpi ..."
          sudo reboot
          exit
        fi
      fi
    done' > /etc/x728pwr.sh
    sudo chmod +x /etc/x728pwr.sh
    sudo sed -i '$ i /etc/x728pwr.sh &' /etc/rc.local
    
    

    AND TO

    x728.sh

    #X728 RTC setting up
    #sudo sed -i '$ i rtc-ds1307' /etc/modules
    #sudo sed -i '$ i echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /$
    #sudo sed -i '$ i hwclock -s' /etc/rc.local
    #sudo sed -i '$ i #Start power management on boot' /etc/rc.local
    
    sleep 1s
    
    #x728 Powering on /reboot /full shutdown through hardware
    #!/bin/bash
    
        sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
    
        echo '#!/bin/bash
    
    SHUTDOWN=5
    REBOOTPULSEMINIMUM=200
    REBOOTPULSEMAXIMUM=600
    echo "$SHUTDOWN" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
    BOOT=12
    echo "$BOOT" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio$BOOT/direction
    echo "1" > /sys/class/gpio/gpio$BOOT/value
    
    echo "X728 Shutting down..."
    
    while [ 1 ]; do
      shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
      if [ $shutdownSignal = 0 ]; then
        /bin/sleep 0.2
      else
        pulseStart=$(date +%s%N | cut -b1-13)
        while [ $shutdownSignal = 1 ]; do
          /bin/sleep 0.02
          if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM $
            echo "X728 Shutting down", SHUTDOWN, ", halting Rpi ..."
            sudo poweroff
            exit
          fi
          shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
        done
        if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ];$
          echo "X728 Rebooting", SHUTDOWN, ", recycling Rpi ..."
          sudo reboot
          exit
        fi
      fi
    done' > /etc/x728pwr.sh
    sudo chmod +x /etc/x728pwr.sh
    sudo sed -i '$ i /etc/x728pwr.sh &' /etc/rc.local
    
    ... code continues here

    I have then rebooted.

    Then I type; x728off

    And the reply is 'X728 Shutting down...'

    I can see that on the x708 the 'BAT LED' comes on briefly and the green light on the pi comes on briefly, but nothing else happens. Both the pi and the x708 are still on and working.

    Pi info:

    Raspberry Pi 3 Model B Plus Rev 1.3

    Distributor ID: Raspbian
    Description:    Raspbian GNU/Linux 10 (buster)
    Release:        10
    Codename:       buster

    Thanks for your help so far...

     

     

     


  • hi,

     

    i would like a follow up with this too! i have my setup with the UPS installed and i'm currently testing to see how long the batteries last before shutdown or complete turn off. Will this automatically shut down itself when no power is detected? i thought the whole point of UPS was to avoid losing data or corrupting data. I have this setup along with my Pi4b....X829 Dual 2.5" SATA HDD/SSD Shield+X829-C1 Case+X708 UPS&Power Mgnt Board.

    The only way i can turn off my case is to hold it for about 5-8 seconds. It does not shutdown properly like others have stated.

    I am confused :/


  • @james mutch 

    Hi James

    I've written a small script that checks the supply voltage and powers down the pi if the voltage drops to below 3.6V.  This is just one approach. Other are possible.

    I've commented, in capital letters, variables that can be changed to suit.

    Copy and paste the code below into a python script anywhere on your pi. Name the file anything you want and make it executable (sudo chmod 755 yourfile.py).

    You can then run this file automatically at boot time with a cron job.

    Hope this helps.

    Unfortunately, this does not solve the problem of the x708 still running off the battery after the pi has been turned off and therefore draining the battery.

    Hopefully the guys at geekworm will come up with a solution.

    #!/usr/bin/env python
    
    import struct
    import smbus
    import sys
    import time
    
    def readVoltage(bus):
    
         address = 0x36
         read = bus.read_word_data(address, 2)
         swapped = struct.unpack("<H", struct.pack(">H", read))[0]
         voltage = swapped * 1.25 /1000/16
         return voltage
    
    def readCapacity(bus):
    
         address = 0x36
         read = bus.read_word_data(address, 4)
         swapped = struct.unpack("<H", struct.pack(">H", read))[0]
         capacity = swapped/256
         return capacity
    
    bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
    
    while True:
    
     time.sleep(3) # CHANGE FREQUENCY OF TESTING THE VOLTAGE
     print "Voltage:%5.2fV" % readVoltage(bus)
    
     print "Battery:%5i%%" % readCapacity(bus)
    
    # if readCapacity(bus) <= 75:
     if readVoltage(bus) <= 3.6: # CHANGE THIS VALUE IF REQUIRED
        print "Shutting down"
    
        from subprocess import call
        call("sudo shutdown -P +1", shell=True) # CHANGE THE TIME DELAY TO SHUTDOWN (IN MINUTES)
    
        break
    

  • I've created the script and done as you've said however, how will i know if it works or not if the normal shutdown or auto-Shutdown is not working?

    Thanks


  • @james mutch 

    Sorry about the delay in replying.

    You will be able to know that the pi has stopped working (i.e. is turned off) because you will no longer be able to get a response from the command line (if you are using putty, it will disconnect).

    The problem, as stated before, is that the x708 does not appear to be shutting down at all. The fan is still going and it's running from the battery.

    Hope this helps.


  • Hi George,

    I'm assuming this is the same for when the power supply is still on and connected too much like the battery backup?

    It really renders my whole setup useless as it doesn't function as an auto shutdown when the battery runs out or when it's mains powered. The main reason i wanted this setup was to created a NAS storage for my photos/videos all stored within the confinements of this box.

    If i wanted a different configuration then i would've bought a pi with an alternative case and used an external hard drive for storage albeit with a horrible cosmetic feel to it.

    I hope this is sorted soon because it seems that this is not fit for purpose on what i want it to do. What's your opinion on this?

     

    Cheers


  • George.... how long a runtime are you getting with just 2 batteries connected? What is the average time of 2 x 2000mah batteries on this setup running before shutdown?


  • Apollogies for constant replies as i can't seem to amend my post above to add stuff :/ 

    Anyways... when you said that it has shutdown and won't get a response from command line does this mean that i don't have to worry about corrupt data or loss etc... 

    Also, I'm still new to the pi scene and pi terminology is all new to me so please forgive me if i don't understand a few things. All i can say is that for now i'm using a pi 4b 4gb using openmediavault to get my NAS up and running so i'm not sure if i'm using putty or not.

    Lets hope geekworm get this sorted soon and not several months later :)

     


  • @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.

     

     


  • Hi George,

    I did everything you said in the post but for some reason when i run the test and pull the power plug when the pi logo shows it shows a black/blank screen for about 30+ seconds and boots to desktop but the fan and everything else remains on (i'm not sure if this is the normal bootup sequence as i can't remember how long it takes to boot to dekstop). 

    When i click on the shutdown.sh file in home/pi folder it executes and shuts down manually. 

    Where should i start to troubleshoot? I've check my script files and they are ok and they when i reopen them. 

    I named my file shutdown.sh and is stored in the /home/pi folder i've checked both the rc.local and shutdown.sh files and they seem ok

    I'm puzzled lol

     


  • here is my code for rc.local

    #!/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

    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
    hwclock -s
    #Start power management on boot
    #/etc/x728pwr.sh &
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
    #Start power management on boot
    #/etc/x728pwr.sh &
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s
    #Start power management on boot
    #/etc/x728pwr.sh &
    #/etc/x728pwr.sh &

    /home/pi/shutdown.sh
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s

    exit 0


  • Hi James

    Regarding the rc.local file:

    As you can see, the same lines are repeated over and over again. I would place a '#' in front to make them comments. The only lines you need are the last three before the 'exit 0'

    I am posting a revised file. You called it shutdown.sh.

    #!/bin/bash
    
    # Clean up
    sudo echo "6" > /sys/class/gpio/unexport
    sudo echo "5" > /sys/class/gpio/unexport
    sudo echo "13" > /sys/class/gpio/unexport
    
    POWERLOSS=6
    
    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 "X728 Shutting down started"
        /bin/sleep 30
    
        SHUTDOWN=5
    
        REBOOTPULSEMINIMUM=200
        REBOOTPULSEMAXIMUM=600
        sudo echo "$SHUTDOWN" > /sys/class/gpio/export
        echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
    
        shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
        echo "shutdownSignal $shutdownSignal"
    
        pulseStart=$(date +%s%N | cut -b1-13)
    
        while [ $shutdownSignal = 0 ]; do
          /bin/sleep 0.02
          if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
    
            echo "X728 Shutting down on pin $SHUTDOWN, halting Rpi ..."
    
            BOOT=13
    
            sudo echo "$BOOT" > /sys/class/gpio/export
            echo "out" > /sys/class/gpio/gpio$BOOT/direction
            echo "1" > /sys/class/gpio/gpio$BOOT/value
    
            sudo poweroff
    
            exit
          fi
          shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
        done
    
      else
        echo "Power on"
        /bin/sleep 5
      fi
    done
    
    

    There is a sleep 30 in there. So, it will pause for 30 seconds before shutting down.

    Try this.

    Hope it helps.


  • so the text line that contains

    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    hwclock -s

    i need to put a hashtag infront of all of these? Any idea what these are? I recently installed the openmediavault so i could use NAS and watched and followed a youtube tutorial and setup, which seems to be working ok as i can see my pi on the network and can access it from my MAC or PC.

    I guess i was expecting too much thinking this was going to be a plug and play system. Didn't realise that i would need to learn scripts etc... lol

    Apart from the scripts and coding it will eventually sink in with time.

    This aside, does your power button on your case switch the whole system off with your script? 

    Thanks for your time :)


  • 1 / 2
  • 2
Please login to reply this topic!