Hardware button for Shutdown

Discussion in 'UDOO 101' started by petfr, Sep 20, 2014.

  1. petfr

    petfr New Member

    Joined:
    Jan 27, 2014
    Messages:
    2
    Likes Received:
    0
    Hello

    This is my first post in this forum. My operating system is Volumio 1.0 based on arch-linux (http://volumio.org/project/). Is there a chance to shut down properly my Udoo-quad with the help of a real push-button ? The background is , that I hear most of the time the same internet radio station. It is starting automatically and I can hear radio by pressing the power- button present on the mainboard . I still have in a other room a RaspberryPi with the same operating system and this expansion:

    http://www.forum-raspberrypi.de/Thread- ... status-led

    Is there something similar for the Udoo-quad?

    My programming capabilities are very modest.

    Thank you and greetings from germany

    Peter
     
  2. mikelangeloz

    mikelangeloz Member

    Joined:
    Jul 9, 2013
    Messages:
    129
    Likes Received:
    1
    Hi Peter,
    first, Volumio is Debian Based. Then, sure you can achieve what you want. You can establish a process which is looking for a GPIO pin, or an arduino output on serial. When this happens, you'll launch the shutdown now command.
    You could use this script:

    https://github.com/fukayatsu/pintercom/blob/master/shutdown-daemon.sh

    Code:
    #!/bin/sh
    BUTTON=25 # shutdown button     
    BUZZER=4
    LED=17
    
    PUSHTIME=3
    
    echo "$BUTTON" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio$BUTTON/direction
    echo "high" > /sys/class/gpio/gpio$BUTTON/direction
    
    echo "$LED" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio$LED/direction
    
    echo "$BUZZER" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio$BUZZER/direction
    
    echo 1 > /sys/class/gpio/gpio$BUZZER/value
    sleep 0.2
    echo 0 > /sys/class/gpio/gpio$BUZZER/value
    
    cnt=0
    while [ $cnt -lt $PUSHTIME ] ; do
      data=`cat /sys/class/gpio/gpio$BUTTON/value`
      if [ "$data" -eq "0" ] ; then
        cnt=`expr $cnt + 1`
      else
        cnt=0
      fi
      sleep 1
    done
    
    echo 1 > /sys/class/gpio/gpio$BUZZER/value
    sleep 0.5
    echo 0 > /sys/class/gpio/gpio$BUZZER/value
    
    echo 1 > /sys/class/gpio/gpio$LED/value
    
    shutdown -h now
    
    And choose the GPIO you want, more info here:
    http://elinux.org/UDOO_GPIO_Pinout

    Let me know about your progress.
     
    sombunall likes this.
  3. askywalker

    askywalker New Member

    Joined:
    Oct 29, 2014
    Messages:
    7
    Likes Received:
    0
    How do you set up the script to run automatically?
    Thanks!
     
  4. mikelangeloz

    mikelangeloz Member

    Joined:
    Jul 9, 2013
    Messages:
    129
    Likes Received:
    1
    You can insert in in /etc/rc.local file, this way the script will be run at every boot.

    Name this script whatever you want, like /home/udoobuntu/myscript.sh

    Give it execution permissions

    Code:
    chmod a+x /home/udoobuntu/myscript.sh
    and place it in /etc/rc.local

    Code:
    sh /home/udoobuntu/myscript.sh &&
    before exit 0

    let me know!
     
  5. sombunall

    sombunall Member

    Joined:
    Jul 16, 2016
    Messages:
    31
    Likes Received:
    4
    Hi,

    First time poster here. How do you know which GPIO these pins are? From what I read there is a mapping between BUTTON=25 and what pin it is. I also looked at your link http://elinux.org/UDOO_GPIO_Pinout but it doesn't help me with that.

    Also if I started using a logic probe I put it in CMOS mode but would I be probing for 5V or 3.3V? There seems to be both kinds of voltages in the udoo quad. I know where to connect the aligator clip for ground (HDMI shield) but not the high one.

    Also I have a piezo buzzer that was used in 5 volts project before. Can I use that with no resister? What about a LED do I need a resistor? I am planning to use the push button and LEDs of an ATX case I put my udoo in.
     
  6. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Hello, the elinux.org information is outdated and will not be updated. You have to go the the Udoo dual/Quad documentation here http://www.udoo.org/docs/Introduction/Introduction.html

    Gpio pin out and usage can be found here http://www.udoo.org/docs/Hardware_&_Accessories/GPIO_Pinout.html.
    All gpio is 3.3V, there is a 5V out but only for reference. So do not use 5V in, also not on for example Serial lines. The 5V buzzer will get 3.3V so will probably make another sound.

    LEDs always have to be connected with a resistor. Push buttons need to be connected so that they have a configured High 3.3V and Low 0V output. Not just put it as a switch between the 3.3V out and gpio pin in. But there are lots of examples to find.
    [​IMG]
    Edit, picture added
     
    Last edited: Jul 17, 2016
    sombunall likes this.
  7. sombunall

    sombunall Member

    Joined:
    Jul 16, 2016
    Messages:
    31
    Likes Received:
    4
    EDIT: Thanks waltervl that helped a lot.

    I am having trouble with this. First it is a single & not a double && for forking a process (EDIT: but it seems to work anyway for some reason, wierd!). Second it only executes when I login as any user. The user doesn't have to be the same one as the user home directory the script is in. To say it another way what is happening is I am rebooting and it doesn't run the script until I login as user1 or user2 when the script is in the home directory of user2. I tried putting a "sleep 1" in the rc.local before the command but that didn't work.
     
  8. sombunall

    sombunall Member

    Joined:
    Jul 16, 2016
    Messages:
    31
    Likes Received:
    4
    I got it working!

    I ended up using /etc/init.d to get it to work. Also regular users can't shutdown the system from the network unless they have privileges so it's secure. It uses the double row header of the udoo quad. There is a power LED and shutdown LED. When you press the button the shutdown light starts blinking a certain way and allows you to abort early and only see pretty blinking. If you continue shutdown light goes solid until shutdown happens then both lights go out. There is also a strange bug that is a feature. The shutdown light will be very dim on boot giving you an idea it is booting. I think this is because it is config'd as an input and provides a tiny amount of floating power for some reason. In this version there is no buzzer yet.

    EDIT:
    NOTES:

    -you will need to run "update-rc.d udoo-power-button defaults" at one point. You should set permissions for root only, read write execute like chmod 700 for the /sbin file. For the /etc/init.d file the perms should look like most others in the directory.
    -the template for the init.d file was taken from "sudo" which I felt should be OK. If it is not... why not? I allowed runlevel 1 for the script too.

    /etc/init.d/udoo-power-button

    Code:
    #!/bin/sh
    
    ### BEGIN INIT INFO
    # Provides:          shutdown-daemon
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:
    # X-Start-Before:    rmnologin
    # Default-Start:     1 2 3 4 5
    # Default-Stop:
    # Short-Description: Provide a shutdown button and friends for udoo
    # Description: Provide a shutdown button and friends for udoo
    ### END INIT INFO
    
    . /lib/lsb/init-functions
    
    N=/sbin/shutdown-daemon
    
    set -e
    
    do_start () {
        $N
    }
    
    case "$1" in
      start)
        do_start
        ;;
      restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
      stop)
        # No-op
        ;;
      *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
    esac
    
    exit 0
    
    /sbin/shutdown-daemon

    Code:
    #!/bin/sh
    SHUTBUTTON=22   # shutdown button   
    SHUTLED=23
    POWERLED=24
    #BUZZER=4       # no buzzer but there could be one...
    
    PUSHTIME=3
    
    # set inputs and outputs
    echo in > /gpio/pin$SHUTBUTTON/direction
    echo out > /gpio/pin$SHUTLED/direction
    echo out > /gpio/pin$POWERLED/direction
    
    # turn power LED on
    echo 1 > /gpio/pin$POWERLED/value
    
    # begin daemon loop
    cnt=0
    while [ $cnt -lt $PUSHTIME ] ; do
        data=`cat /gpio/pin$SHUTBUTTON/value`
        # button is active high
        if [ "$data" -eq "1" ] ; then
            cnt=`expr $cnt + 1`
            sleep 0.8
            echo 1 > /gpio/pin$SHUTLED/value
            sleep 0.2
            echo 0 > /gpio/pin$SHUTLED/value
        else
            cnt=0
            sleep 1
        fi
        #sleep 1
    
        #debug lines
        #sleep 0.8
        #echo 1 > /gpio/pin$SHUTLED/value
        #sleep 0.2
        #echo 0 > /gpio/pin$SHUTLED/value
    done
    
    # turn shutdown SHUTLED on and shutdown
    
    echo 1 > /gpio/pin$SHUTLED/value
    
    shutdown -h now
    
    exit 0
    
     
    Last edited: May 27, 2017
    Andrea Rovai and waltervl like this.
  9. sombunall

    sombunall Member

    Joined:
    Jul 16, 2016
    Messages:
    31
    Likes Received:
    4
    I switch from udoobuntu 2 minimal to udoobuntu 2 desktop and now I can't get my script to execute on boot. I can only run it manually by /etc/init.d/udoo-power-button start. I wonder why?

    Did anyone actually use my script yet and get it working perfectly with the desktop version?
     
  10. sombunall

    sombunall Member

    Joined:
    Jul 16, 2016
    Messages:
    31
    Likes Received:
    4
    AKK! I did something bone-headed. Should have gone to bed. I went and created a symlink manually in rc0.d and from then on it confused update-rc.d whenever it tried to do anything.

    I also fixed a non-fatal typo in my script and changed it in my post above. There was a space between #! and /bin/sh. Funny it ran anyway.
     

Share This Page