Shutdown and restart

Raspberry Pi related support

Re: Shutdown and restart

Postby Tall Person » 06 Nov 2017, 01:38

Hi guys,
So in Raspian i managed to add a switch to a GPIO port and a python script to shutdown or restart the Pi. I found a script that worked and then modified it to suit my needs.

Code: Select all
sudo apt-get install python-dev 
sudo nano /home/pi/offswitch.py


Then paste in the following text:

Code: Select all
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.IN,pull_up_down=GPIO.PUD_UP)
btn_timer=0
while True:
    if ( GPIO.input(25) == False ) :
        btn_timer = btn_timer+1
    else:
        if ( btn_timer > 5 ) :
            os.system('sudo shutdown -h now &')
        elif (btn_timer > 2) :
            os.system('sudo reboot &')
        elif (btn_timer > 0.5) :
            os.system('sudo ifup wlan0 &')
        btn_timer = 0
    sleep(0.5);


CtrlX to save and exit

Code: Select all
sudo chmod -x offswitch.py
sudo python /home/pi/offswitch.py


The possibilities could be extended based on the duration of button press to activate other commands like restarting the wireless controller as per my last command. Now in the Runeaudio build I am struggling to know how I could implement the same procedure. Could someone please advise if the python install and script above would still work, the locations to install, etc?
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

Re: Shutdown and restart

Postby hondagx35 » 07 Nov 2017, 08:57

Hi Raoul,

on my latest images python is installed by default.
If this is not the case for the image you are using, you have to install it with pacman.
Pacman is the packet manager for ArchLinux.

Code: Select all
pacman -S python2


See also this.

Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Shutdown and restart

Postby Tall Person » 07 Nov 2017, 20:41

Thanks Frank. Do I save my files in the same locations as above?. I'm using the 0.4 version from janui and the command is not recognised so I assume it is not installed. I did find a python3.4 folder but the command did not run.within the folder either.
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

Re: Shutdown and restart

Postby janui » 09 Nov 2017, 10:44

Frank & Raoul,
I have tested python2 with the zero build. It works fine, install with:
Code: Select all
pacman -S python2
janui
User avatar
janui
 
Posts: 704
Joined: 20 Dec 2014, 12:55
Location: Ollanda

Re: Shutdown and restart

Postby rern » 09 Nov 2017, 13:36

RuneAudio 0.3:
python 2.7 - symlink: python2

fix 'python: command not found'
Code: Select all
ln -s /usr/bin/python2.7 /usr/bin/python

RuneAudio 0.4b:
python 2.7 - symlink: python2
python 3.6 - symlink: python, python3
rAudio @ https://github.com/rern/rAudio-1
Raspberry Pi 4B
Raspberry Pi 3B > SMSL M8 DAC
Raspberry Pi Zero W
User avatar
rern
 
Posts: 723
Joined: 14 Jul 2016, 08:15

Re: Shutdown and restart

Postby Tall Person » 10 Nov 2017, 00:53

I do not appear to be able to get my original code to run with Python2.

Code: Select all
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.IN,pull_up_down=GPIO.PUD_UP)
btn_timer=0
while True:
    if ( GPIO.input(25) == False ) :
        btn_timer = btn_timer+1
    else:
        if ( btn_timer > 5 ) :
            os.system('sudo shutdown -h now &')
        elif (btn_timer > 2) :
            os.system('sudo reboot &')
        elif (btn_timer > 0.5) :
            os.system('sudo ifup wlan0 &')
        btn_timer = 0
    sleep(0.5);


This file is called offswitch.py and saved in the root folder. When I run
Code: Select all
python2 /root/offswitch.py

i get the following message when i press my button on the pi:
sh: sudo: command not found

Does this mean that the code is recognising the button press, but not the code above? What do I need to change for Python2?
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

Re: Shutdown and restart

Postby hondagx35 » 10 Nov 2017, 11:14

Hi Tall Person,

i get the following message when i press my button on the pi:
sh: sudo: command not found


On Rune there is no "sudo" installed and also not needed.
Remove "sudo" from your code:
Code: Select all
os.system('sudo shutdown -h now &')

to
Code: Select all
os.system('shutdown -h now &')


Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Shutdown and restart

Postby Tall Person » 15 Nov 2017, 09:32

Frank, thanks. I can't believe I missed the sudo command was still in the code from my earlier working version with raspian debian....!
I'll have a look later and see if that solves the problem.
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

support RuneAudio Donate with PayPal

Previous

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 3 guests