Automatic shutdown at powerloss

Post here your applications done based on UPiS
Share your ideas about new possible applications

Automatic shutdown at powerloss

Postby c-graeser » Thu Apr 10, 2014 2:12 pm

I am trying to find a solution to automatic power down the RasPi and the UPiS board if the main USP power source is not available.
I do not want to wait until the LiPo is emty to automatically shut down the RasPi.
Unfortunately I find no way for a shutwown except using the button on the UPiS board.
A software command for the same function which is provided by the button would be very helpful.

Do you have othe suggestions for solving my problem?
c-graeser
 
Posts: 5
Joined: Thu Apr 10, 2014 1:26 pm

Re: Automatic shutdown at powerloss

Postby Pimaster » Thu Apr 10, 2014 3:45 pm

Hi,

Please find my answer in the text

I am trying to find a solution to automatic power down the RasPi and the UPiS board if the main USP power source is not available.
OK
I do not want to wait until the LiPo is emty to automatically shut down the RasPi.
It is not needed

Unfortunately I find no way for a shutwown except using the button on the UPiS board.
Use the @SDWN command existing on the firmware V1.02 on this forum
A software command for the same function which is provided by the button would be very helpful.
The new version of the firmware will have improved this feature

Do you have othe suggestions for solving my problem?

You can use @PM command and the will have automatic indication when system is going to be powered via BAT, so using a simple script (reading the RPi serial Port) you can handle it by yourself


Another solution is to use the PICo interface where the same capabilities are available and by simple reading of the I2C registers you can do more or less the same.

And the final solution is to wait 1-2 days more for the version 1.04 where this command will be full implemented
Warmest Regards
Pi Master
Warmest Regards
PiM
---
Designing with Mentor Graphics PADS - www.pads.com
Please read and follow the PiForum rules
http://www.forum.pimodules.com/viewtopic.php?f=13&t=196
---
Pimaster
Site Admin
 
Posts: 1582
Joined: Fri Sep 14, 2012 7:50 am

Re: Automatic shutdown at powerloss

Postby maestro » Thu Apr 10, 2014 3:53 pm

Hi,

basically you have two options:
1) Passive -> Run a cron job every (lets say) 5 minutes to run command @pm via serial or I2C interface and in case of the "BAT" result you can:
a) invoke @SDWN command via Serial/I2C that will inform the UPiS to shutdown the Pi and switch to LPR mode
b) run poweroff command that should switch UPiS to LPR mode automatically (there are a known limitations in this feature for now)
2) Active -> Make sure you have the "@PWRINFO" feature enabled, setup a daemon to listen on Serial interface for the power change notifications and in case you receive "switch to BAT" event, launch the action mentioned in the point 1)

:) Easy as that :) Just one note though. It's not ideal to start switching off right at the moment you are on BAT source. I'd suggest to wait 1-5 minutes more based on the battery capacity before staring the shutdown procedure. This way you will prevent minor power fluctuations, moving the device before power sockets,really dark cloud on the sky passing by, etc...

BR
Vit
maestro
 
Posts: 32
Joined: Tue Mar 11, 2014 7:07 pm

Re: Automatic shutdown at powerloss

Postby c-graeser » Tue Apr 15, 2014 7:22 pm

I SOLVED the automatic shutdown after detection of power loss with the following script:

Code: Select all
#!/bin/bash
# Power detection at UPiS PiCO interface with shutdown of Raspberry Pi after a definable time of power loss

# Set the shutdown delay after power loss to 40 seconds:
DELAY=40

# Initialize serial controller (not needed anymore due to the use of the I2C Interface
#DEVICE=/dev/ttyAMA0
#stty -F $DEVICE 1:0:cbf:0:0:0:0:0:0:5:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
#stty -F $DEVICE -ignbrk


STATE=kein

# Endless loop
while true
do

# Read power status
POWERSTAT=`i2cget -y 1 0x6A 0`
sleep 1
case "$POWERSTAT" in

  0x02)# USB power is available at the UPiS Module
       if [ $STATE = "lost" ]; then
         echo "`date` Power is back"
         STATE="kein"
       fi
       ;;

  0x04)# The system is running on battery
       if [ $STATE = "kein" ]; then
         AUSFALL=`date +%d%H%M%S+0|bc`
         echo "`date` Power is lost!"
         STATE="lost"
       else
         # Test ob laenger als eine Minute der Strom aus war
         if (( `date +%d%H%M%S+0|bc` > `echo $AUSFALL+$DELAY|bc` )) && [ $STATE != "shutdown" ]; then
           echo "Shutdown!"
#           echo "@sdwn" > $DEVICE
           shutdown -h now
           STATE="shutdown"
           exit 0
         fi
       fi
       ;;
  *)   ;;

  esac
:
done
c-graeser
 
Posts: 5
Joined: Thu Apr 10, 2014 1:26 pm

Re: Automatic shutdown at powerloss

Postby LyMar17 » Sat Jun 28, 2014 1:13 pm

Hi there,

I'm working on a script to initiate a (delayed) soft shutdown procedure of the Raspberri Pi in case of mains faillure.

The script above doesnt work for me...it returns an error after "while true" :-(

I understand the best way to monitor the mains is via the PiCo interface.

Can anyone tell me which command to use or which pin to monitor in the script to "see" if the mains is up or down?

I read the script example in the UPiS manual, but that describes the process of monitoring the shutdown button.

Thanks for the help.

Regards,
Martijn
LyMar17
 
Posts: 16
Joined: Thu Jun 26, 2014 8:20 pm

Re: Automatic shutdown at powerloss

Postby Pimaster » Sat Jun 28, 2014 1:24 pm

Hi,
there is already implemented PICo command which define the time you need to automatic shutdown when on battery powering

5 or 0x05 fssd_batime Byte Common R/W File Safe Shut Down Timer for Unconditional Automatic File Safe Shutdown when UPiS is in BAT PWR Mode (loose the cable power) in seconds in HEX format

Read: Anytime, Return actual fssd_batime value

Write: 0xff – Disable the timer, default value

Write: 0x01 – 0xfe time in seconds when UPiS+RPi shutdown after cable power will go out (EPR or USB)

it is tested and working

just use it

warmest regards
Pi Master
Warmest Regards
PiM
---
Designing with Mentor Graphics PADS - www.pads.com
Please read and follow the PiForum rules
http://www.forum.pimodules.com/viewtopic.php?f=13&t=196
---
Pimaster
Site Admin
 
Posts: 1582
Joined: Fri Sep 14, 2012 7:50 am

Re: Automatic shutdown at powerloss

Postby maestro » Sat Jun 28, 2014 1:32 pm

Hi Martijn,

the script is working just fine :) Please post a specific error message you are getting, in order to find out the reason for its mulfunction :)

BR

Vit
maestro
 
Posts: 32
Joined: Tue Mar 11, 2014 7:07 pm

Re: Automatic shutdown at powerloss

Postby maestro » Sat Jun 28, 2014 2:04 pm

you can find below a bit more optimized script to use:

Code: Select all
#!/bin/bash
# Power detection at UPiS PiCO interface with shutdown of Raspberry Pi after a definable time of power loss

# Set the shutdown delay after power loss to 40 seconds:
DELAY=40

# Tmeout counter
COUNTER=0
# Endless loop
while true; do

  # Read power status
  POWERSTAT=`i2cget -y 1 0x6A 0`
  case "$POWERSTAT" in
    # The system is running on battery
    0x04)
      if [ $COUNTER -eq 0 ]; then
        logger -s "`date` Power is lost!"
      fi
      let COUNTER++     
      # Test ob laenger als eine Minute der Strom aus war
      if [ $COUNTER -gt $DELAY ]; then
        logger -s  "`date` Shutdown!"
        shutdown -h now
        STATE="shutdown"
        logger -s  "`date` Exitting..."
        exit 0
      fi
      ;;
    # The system is not running on baterry
    *)   
      if [ $COUNTER -ne 0 ]; then
        logger -s  "`date` Power is back"
      fi
      COUNTER=0     
      ;;
    esac
    sleep 1

done
maestro
 
Posts: 32
Joined: Tue Mar 11, 2014 7:07 pm

Re: Automatic shutdown at powerloss

Postby LyMar17 » Sat Jun 28, 2014 9:13 pm

Hello maestro and PiMaster,

I put your revised script on the Pi.

below is the result.


root@raspbmc:/home/pi# sudo python /home/pi/upis/fshut.py
File "/home/pi/upis/fshut.py", line 10
while true; do
^
SyntaxError: invalid syntax
root@raspbmc:/home/pi#
LyMar17
 
Posts: 16
Joined: Thu Jun 26, 2014 8:20 pm

Re: Automatic shutdown at powerloss

Postby maestro » Sat Jun 28, 2014 9:48 pm

Hello,

you are tying to execute shell script using python interpreter:) not gonna work :) Please run:
Code: Select all
mv /home/pi/upis/fshut.py /home/pi/upis/shutdown.sh
chmod 0777 /home/pi/upis/shutdown.sh
and run it using
Code: Select all
/home/pi/upis/shutdown.sh


BR

Vit
maestro
 
Posts: 32
Joined: Tue Mar 11, 2014 7:07 pm

Next

Return to UPiS Users Application

Who is online

Users browsing this forum: No registered users and 1 guest

cron