RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Raspberry Pi related support

RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby pez » 07 Dec 2017, 20:04

Hi Everybody,
i have managed to use a rotary encoder to change volume and play/pause, was inspired by this here:
hardware-play-pause-button-t3665-10.html
and got some help from Bob:
https://github.com/bobrathbone/piradio

I am using the original rotary_class.py from Bob.

I am using this encoder and these pins (attachment):
https://www.conrad.at/de/encoder-5-vdc- ... 53405.html

I used this codes here to start:

Code: Select all
pacman -Syy
pacman -S python2
pip install RPi.GPIO


That's the script for controlling runeaudio (mpd):
Code: Select all
#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
# $Id: rotary_switch.py,v 1.4 2014/01/31 14:06:26 bob Exp $
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# Adjusted to be used as single hardwarecontroller for rune audio and Hifiberry Amp2 by Petrus Gartler
# Date   : 2017/12/2
#
# This class uses standard rotary encoder with push switch
#

import sys
import time
import sys,os

from rotary_class import RotaryEncoder

# Switch definitions
ROT_A = 24
ROT_B = 25
ROT_BUTTON = 23

# Execute system command
def execCommand(cmd):
   p = os.popen(cmd)
   return  p.readline().rstrip('\n')

# This is the event callback routine to handle knob events
def volume_knob_event(event):
   handle_event(event,"Volume knob")   
   return

# This is the event callback routine to handle events
def handle_event(event, name):
   if event == RotaryEncoder.CLOCKWISE:
#      print name, "Clockwise event =", RotaryEncoder.CLOCKWISE
      execCommand("mpc volume +5"), RotaryEncoder.CLOCKWISE
   elif event == RotaryEncoder.ANTICLOCKWISE:
#      print name, "Anticlockwise event =", RotaryEncoder.ANTICLOCKWISE
      execCommand("mpc volume -10"), RotaryEncoder.CLOCKWISE
   elif event == RotaryEncoder.BUTTONDOWN:
#      print name, "Button down event =", RotaryEncoder.BUTTONDOWN
      execCommand("mpc toggle"), RotaryEncoder.BUTTONDOWN
#   elif event == RotaryEncoder.BUTTONUP:
#      print name, "Button up event =", RotaryEncoder.BUTTONUP
   return

# Define knob
volumeknob = RotaryEncoder(ROT_A,ROT_B,ROT_BUTTON,volume_knob_event,2)

# Wait for events
while True:
   time.sleep(0.5)

# End of program


Now i am trying to make an on/of switch but unfortunately i somehow messed around the poweroff command and can't even shutdown rune using the webinterface or terminal.
I am afraid, i deleted a file called poweroff-target. Is that something i shouldn't have done?

I hope someone can get me out without reinstalling the whole thing.

Cheers,
Pe
Attachments
GPIO_Pi3.png
GPIO_Pi3.png (90.61 KiB) Viewed 189 times
RPi 3 model B
Hifiberry AMP2
work on autonomous hifibox in progress:
Rotary encoder, On/Off witch, status LED
pez
 
Posts: 5
Joined: 19 Nov 2017, 18:20
Location: Austria

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby janui » 08 Dec 2017, 10:33

Hi pez,
pip install RPi.GPIO
This is not the way to install a package or library on an Arch Linux system. I would expect you to do something like this:
Code: Select all
cd /home
wget https://aur.archlinux.org/packages/ra/raspberry-gpio-python/raspberry-gpio-python.tar.gz
tar xf raspberry-gpio-python.tar.gz
cd raspberry-gpio-python
makepkg -Acs --asroot
pacman -U raspberry-gpio-python
You may get some errors with the makepkg command because Rune is a striped down version of Arch Linux. I use the following to install the extra packages needed to build packages:
Code: Select all
pacman -Syy make gcc
And afterwards remove the extra packages with:
Code: Select all
pacman -Rs make gcc
Finally to clean up the package build (you may want to save the files '/home/raspberry-gpio-python/*' first, with these you can reinstall with the pacman command after restoring the files without needing to use makepkg):
Code: Select all
cd /home
rm -rf raspberry-gpio-python

I think you should also know about the overlay 'rotary-encoder' open the file '/boot/overlays/README' and search for 'rotary-encoder'. I have not used this and am not sure how it works.

And, yes, you need the file '/usr/lib/systemd/system/poweroff.target', it should contain:
Code: Select all
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Power-Off
Documentation=man:systemd.special(7)
DefaultDependencies=no
Requires=systemd-poweroff.service
After=systemd-poweroff.service
AllowIsolate=yes
JobTimeoutSec=30min
JobTimeoutAction=poweroff-force

[Install]
Alias=ctrl-alt-del.target

janui
janui
 
Posts: 106
Joined: 20 Dec 2014, 12:55
Location: Ollanda

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby pez » 08 Dec 2017, 14:19

Hi janui,
thanks for that, i created that poweroff.tagret file and it shuts down properly now when i use python to run the script.

All the GPIOs work well, so i will not reinstall that, but next time i'll do it as you recommend.

Only when i try to create a service running that script at boot time i get the following error:

Code: Select all
root@runeaudio(rw):~/hw-control# systemctl enable hw_poweroff
root@runeaudio(rw):~/hw-control# systemctl start hw_poweroff
root@runeaudio(rw):~/hw-control# systemctl status hw_poweroff
● hw_poweroff.service - RuneAudio Poweroff
   Loaded: loaded (/usr/lib/systemd/system/hw_poweroff.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2017-12-08 14:10:40 CET; 4s ago
  Process: 1531 ExecStart=/home/root/hw-control/hw_poweroff.py (code=exited, status=203/EXEC)
 Main PID: 1531 (code=exited, status=203/EXEC)

Dec 08 14:10:40 runeaudio systemd[1]: Started RuneAudio Poweroff.
Dec 08 14:10:40 runeaudio systemd[1]: hw_poweroff.service: Main process exited, code=exited, status=203/EXEC
Dec 08 14:10:40 runeaudio systemd[1]: hw_poweroff.service: Unit entered failed state.
Dec 08 14:10:40 runeaudio systemd[1]: hw_poweroff.service: Failed with result 'exit-code'.


My service file is this here:

Code: Select all
[Unit]
Description=RuneAudio Poweroff
After=mpd.service

[Service]

ExecStart=/home/root/hw-control/hw_poweroff.py

[Install]
WantedBy=multi-user.target


Can you tell what's going wrong?
RPi 3 model B
Hifiberry AMP2
work on autonomous hifibox in progress:
Rotary encoder, On/Off witch, status LED
pez
 
Posts: 5
Joined: 19 Nov 2017, 18:20
Location: Austria

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby hondagx35 » 08 Dec 2017, 15:54

Hi janui and pez,

Code: Select all
pip install RPi.GPIO

This is not the way to install a package or library on an Arch Linux system. I would expect you to do something like this:

RPi.GPIO is not a Linux package it is a python library!
To install python libraries with pip is totally fine on Arch and Rune.

Code: Select all
[Unit]
Description=RuneAudio Poweroff
After=mpd.service

[Service]

ExecStart=/home/root/hw-control/hw_poweroff.py

[Install]
WantedBy=multi-user.target


Python scripts cannot be started directly, you have to use something like:
Code: Select all
ExecStart=/usr/bin/python2 /home/root/hw-control/hw_poweroff.py


BTW:
@pez: I have no idea why your posts have to be approved.

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

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby pez » 08 Dec 2017, 16:23

Hi Frank,
thanks for the clearification, everything runs perfectly now!
In another thread you mentioned to be better using another command to shutdown:
hondagx35 wrote: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


Why is that?

Interesting enough, i have a script running at startup for my rotary encoder which runs OK with this service:
Code: Select all
[Unit]
Description=RuneAudio Rotary Switch
After=mpd.service

[Service]

ExecStart=/home/root/hw-control/rotary_switch.py

[Install]
WantedBy=multi-user.target


I'm fine with that, only wandering why this one doesn't need the "/usr/bin/python2" part and the other one does.

Now to the stauts LED-part,
cheers and thanks,
pez

PS.: Maybe the forum-software thinks i'm a bot, but i can assure you i'm not. :-)
RPi 3 model B
Hifiberry AMP2
work on autonomous hifibox in progress:
Rotary encoder, On/Off witch, status LED
pez
 
Posts: 5
Joined: 19 Nov 2017, 18:20
Location: Austria

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby hondagx35 » 08 Dec 2017, 16:41

Hi pez,

I'm fine with that, only wandering why this one doesn't need the "/usr/bin/python2" part and the other one does.

Compare your python scripts, i bet the working one has this at the top:
Code: Select all
#!/usr/bin/python2

and / or
it is made executable with:
Code: Select all
chmod +x filename.py

This can be checked with:
Code: Select all
ls -la /path-to-your-script/scriptname


In another thread you mentioned to be better using another command to shutdown:

I mentioned to call rune_shutdown before shutting down with your favorite method.
This is because the script stops MPD and unmounts your shares.
- 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: 2834
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: RPi 3, Hifiberry AMP2, rotary encoder, on/off switch

Postby pez » 08 Dec 2017, 17:10

The python script wasn't executable, that was it!
I included that call ".../rune_shutdown poweroff" before shutdown.
By the way, both of them have
Code: Select all
#!/usr/bin/env python2
at the beginning but it works anyway.

Cheers, i'll keep this thread updated with my progress.
pez
RPi 3 model B
Hifiberry AMP2
work on autonomous hifibox in progress:
Rotary encoder, On/Off witch, status LED
pez
 
Posts: 5
Joined: 19 Nov 2017, 18:20
Location: Austria

support RuneAudio Donate with PayPal


Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 1 guest