Momentary On/Off Switch with soft shutdown

Raspberry Pi related support

Re: Momentary Contact Switch

Postby hondagx35 » 13 Apr 2016, 20:03

Hi all,

ingohz wrote:Of course you can implement this in the quick (and dirty) way with a little scripting here and there.

I also think, that is not the best way.

ingohz wrote:I thought of something that is not resource hungry and should be configurable via the Rune interface.

Yes, resource-saving is an absolute must.

I can help with the services and programming.
So let us do some more brainstorming first.

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

Re: Momentary Contact Switch

Postby ingohz » 13 Apr 2016, 20:47

  • daemon to watch for rising/falling edge on gpio via interrupt. Could be done in python or C AFAIK.
  • execute rune shutdown
  • gpio configurable via rune interface/menu, saved via redis?
  • service (start/stop/status) via systemctl
  • later: signal shutdown in progress/complete to additional hardware ala mausberry or DIY. On the same port if possible.
  • HiFi 1: Alix/voyage-mpd, MusicalFidelity M1 DAC (RP2/HifiBerry Digi+), Accuphase DP-67, Accuphase E-212, Infinity Kappa 8.2i
  • HiFi 2: RP2/7" Touchscreen/IQaudIO Pi-DAC+, Philips Fidelio X1
ingohz
 
Posts: 45
Joined: 22 Feb 2016, 12:11

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 00:19

I would be ok with either Python or C, but I would suggest using Python2. Once installed, we know it will work because member XploD already has a tutorial here, which is easy to follow, and other people have used. And Python is very easy to read, for me anyway, ;)

Here is an example which works, and avoids GPIO pins in use by the HiFiBerry boards, a few lines of Python code (not including using interrupts):

Code: Select all
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)

# Select unused GPIO header pin to be used for shutdown
InputPin = 13

# Set selected pin to input, and enable internal pull-up
GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Look for a button press 4x per second
while GPIO.input(InputPin):
    time.sleep(0.25)

# When pressed, execute a shutdown, or any other command
print "*** Soft shutdown activated ***"
subprocess.call("/sbin/shutdown now", shell=True, stdout=subprocess.PIPE, stder$


After installing Python and the requisite libraries (using the tutorial posted by XploD, it can be run from the command line to test. Only needs a single free GPIO pin (I am using pin 13), any kind of momentary pushbutton, two wires, and a ground pin.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 01:06

And... it looks like the library I loaded from XploD's tutorial has interrupts, even better!
(why don't people just tell me these things up front... :roll: )

So, even easier with interrupts (I think):

Code: Select all
!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)

# Select unused GPIO header pin to be used for shutdown
InputPin = 13

# Set selected pin to input, and enable internal pull-up
GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Wait for a button press on the selected pin (pulled to ground, falling edge)
GPIO.wait_for_edge(InputPin, GPIO.FALLING)

# When pressed, execute a shutdown, or any other command
print "*** Soft shutdown activated ***"
subprocess.call("/sbin/shutdown now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


edit:

I managed to get this to run as a service, using the method posted by Frank for MPD-watchdog.

As I thought, there was very little original programming (much to my relief). The effort consisted of research mostly, and some cut and paste.

1) Looking up which GPIO pins remain free once my HiFiBerry DAC+ is installed.
2) Finding a suitable Python example to activate shutdown
3) Installing Python 2 and associated libraries using tutorial posted by XploD:
http://www.runeaudio.com/forum/post11623.html#p11623 with RPi.GPIO Version 0.5.11
4) Modifying the example to use my GPIO pin of choice, and interrupts
5) Testing using command line
6) Installing and running as a service, using method posted by Frank for MPD-watchdog, here:
http://www.runeaudio.com/forum/auto-reconnect-for-web-radio-t3278.html

I can't say how well it fits into the RuneAudio scheme of things, but it runs, Top shows no added CPU load, and shutdown in response to a button press is essentially instantaneous.
Last edited by PeteB on 14 Apr 2016, 06:13, edited 1 time in total.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby hondagx35 » 14 Apr 2016, 06:13

Hi Pete,

- Python2 is installed on the newer versions by default
- you should call "/var/www/command/rune_shutdown poweroff" or
"/var/www/command/rune_shutdown reboot" before systemctl shutdown or reboot.

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

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 06:19

@Frank:

I assumed I had the newer version, so perhaps I just installed it over or parallel to what I already had (?) I will look into that tomorrow.

Do you mean call both the Rune shutdown, AND systemctl shutdown, one after the other, or, instead of?

Thanks.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby ingohz » 14 Apr 2016, 08:09

Looks good so far and RPi.GPIO seems to be fine. I'll install it and try it out. It also supports threaded callbacks and has software debouncing integrated. Very useful to support more than one button for different tasks - e.g. FWD/BACK, PAUSE, MUTE etc.

Frank, is there a "rune way" to send commands to mpd or is it save to use e.g. python-mpd2?
  • HiFi 1: Alix/voyage-mpd, MusicalFidelity M1 DAC (RP2/HifiBerry Digi+), Accuphase DP-67, Accuphase E-212, Infinity Kappa 8.2i
  • HiFi 2: RP2/7" Touchscreen/IQaudIO Pi-DAC+, Philips Fidelio X1
ingohz
 
Posts: 45
Joined: 22 Feb 2016, 12:11

Re: Momentary Contact Switch

Postby hondagx35 » 14 Apr 2016, 08:39

Hi,

PeteB wrote:Do you mean call both the Rune shutdown, AND systemctl shutdown, one after the other, or, instead of?

- you should call "/var/www/command/rune_shutdown poweroff" or
"/var/www/command/rune_shutdown reboot" before systemctl shutdown or reboot.


ingohz wrote:Frank, is there a "rune way" to send commands to mpd or is it save to use e.g. python-mpd2?

python-mpd2 is save as long as you do not use Spotify and Airplay.

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

Re: Momentary Contact Switch

Postby thedman! » 14 Apr 2016, 19:19

PeteB wrote:And... it looks like the library I loaded from XploD's tutorial has interrupts, even better!
(why don't people just tell me these things up front... :roll: )

So, even easier with interrupts (I think):

Code: Select all
!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)

# Select unused GPIO header pin to be used for shutdown
InputPin = 13

# Set selected pin to input, and enable internal pull-up
GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Wait for a button press on the selected pin (pulled to ground, falling edge)
GPIO.wait_for_edge(InputPin, GPIO.FALLING)

# When pressed, execute a shutdown, or any other command
print "*** Soft shutdown activated ***"
subprocess.call("/sbin/shutdown now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


edit:

I managed to get this to run as a service, using the method posted by Frank for MPD-watchdog.

As I thought, there was very little original programming (much to my relief). The effort consisted of research mostly, and some cut and paste.

1) Looking up which GPIO pins remain free once my HiFiBerry DAC+ is installed.
2) Finding a suitable Python example to activate shutdown
3) Installing Python 2 and associated libraries using tutorial posted by XploD:
http://www.runeaudio.com/forum/post11623.html#p11623 with RPi.GPIO Version 0.5.11
4) Modifying the example to use my GPIO pin of choice, and interrupts
5) Testing using command line
6) Installing and running as a service, using method posted by Frank for MPD-watchdog, here:
http://www.runeaudio.com/forum/auto-reconnect-for-web-radio-t3278.html

I can't say how well it fits into the RuneAudio scheme of things, but it runs, Top shows no added CPU load, and shutdown in response to a button press is essentially instantaneous.


I was about to start a new thread trying to hack some scripts elsewhere to get a momentary switch working, but it seems to have been done here...

I will attempt this tonight, if the mrs gives me some time... is step two in the process above accomplished by the script above that starts "!/usr/bin/env python" if so, what should I name this and where should I save it?

Thanks a million,

Dave.
thedman!
 
Posts: 62
Joined: 24 Mar 2016, 15:45

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 19:35

thedman! wrote:...is step two in the process above accomplished by the script above that starts "!/usr/bin/env python" if so, what should I name this and where should I save it?...

Hi Dave,
Yes, that was the intent. Cut and paste from other scripts, very little original programming on my part. Lots of good discussion in the old thread with the tutorial by XploD, I spent more time reading that than anything else.

Frank posted we already have Python installed, so that saves some steps.

I need to add one line from Frank's post, above, I will try to do that over lunch, but will not have time to test it again until this evening.

I would guess that Frank would tell us to save it in the same place as the script for MPD Watchdog (mpd-watchdog), so in that case, that would be in /var/www/command. While working on it, I put it in /home/pi to avoid any mishaps.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

support RuneAudio Donate with PayPal

PreviousNext

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 17 guests