Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to Get Python Shutdown GPIO/Button Script to Start at Boot on Orange Pi Zero

Featured Replies

Hi,

 

I am a newbie to Armbian so I apologize if this is a stupid question. I am running Armbian legacy.

 

I think I have successfully programmed a GPIO shutdown button for the Orange Pi Zero by cobbling together information from this and the Orange Pi forum. My script goes like this, in file hw_shutdown.py:

 

#!/usr/bin/env python

import os
import sys
import subprocess

if not os.getegid() == 0:
    sys.exit('Script must be run as root')

from time import sleep
from subprocess import *
from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port


button_shutdown = port.PA2  #PIN 20

"""Init gpio module"""
gpio.init()

"""Set directions"""
gpio.setcfg(button_shutdown, gpio.INPUT)


"""Enable pullup resistor"""
gpio.pullup(button_shutdown, gpio.PULLUP)

def delay_milliseconds(milliseconds):  
    seconds = milliseconds / float(1000)
    sleep(seconds)  

def run_cmd(cmd):  
   p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)  
   output = p.communicate()[0]  
   return output  

shutdown_pressed = 1
while True:
   value_out = 0
   shutdown_pressed = gpio.input(button_shutdown)
   
   if (shutdown_pressed == 0):
      value_out = 1
      run_cmd("shutdown now")

   delay_milliseconds(100)

What I would like is to have this script run at boot. I tried adding 

python /root/hw_shutdown.py

before exit 0 in /etc/rc.local, but nothing happened.

 

In arch linux on the raspberry pi I created a service to run this type of script, but I don't know how to do that on Armbian.

Any ideas what I should do?

 

Thanks,

Rod

Edited by rlsten
errors in code

  • Author
On 27.03.2017 at 8:59 PM, martinayotte said:

 

When adding any thing in /etc/rc.local, you need to make sure it done as a subprocess, not block /etc/rc.local from exiting.

So, you should call your script similar to :

 


nohup /usr/bin/python /root/hw_shutdown.py &

 

Thanks very much,

Rod

Hello Rod,

can you please describe how you setup the power button to the different ports?

Thanks

 

Fine

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.