Jump to content

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


Recommended Posts

Posted (edited)

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
Posted

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 &

 

Posted
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

Posted

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.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines