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 6829 times
RPi 3 model B
Hifiberry AMP2
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
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
User avatar
janui
 
Posts: 699
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
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
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: 3042
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
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
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: 3042
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
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
Joined: 19 Nov 2017, 18:20
Location: Austria

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

Postby pez » 17 Dec 2017, 22:09

Hi everybody,
i am more the soldering type of guy rather than coding, so i've figured out a hardware based solution for a combined shutdown/startup switch with status LED, see attachment. It works perfectly fine with my script to shutdown via GPIO-26. It uses a relais to switch between RUN and GPIO on one side and red/green LED color on the other.
I'm planning to remove one USB connector and use it's 5V pin to control the "primary" relais circuit.
Does anybody have an idea about how to get three LEDs controlled based on the volume level as a simple volume indicator?
0%: no LED
1-33%: 1 LEDs
34-67%: 2 LEDs
68-100%: 3 LEDs

Any help is appreciated,
greetings,
P
Attachments
RaspiHifiBox02.png
RaspiHifiBox02.png (19.74 KiB) Viewed 6633 times
RPi 3 model B
Hifiberry AMP2
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
Joined: 19 Nov 2017, 18:20
Location: Austria

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

Postby pez » 18 Dec 2017, 23:20

OK, based on some other threads i got that script set up but is that the correct way to do it?
To be honest i hardly understand what i'm doing :-)
Testing it without LEDs connected it puts out the actual volume correctly but i get some warning in the beginning (see below):

Code: Select all
#!/usr/bin/env python2
# LED_volume.py

# IMPORTS
import sys
import time
import RPi.GPIO as GPIO

from mpd import (MPDClient)

#GPIO Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(05,GPIO.OUT)
GPIO.setup(06,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)

#definitions
client = MPDClient()
client.connect('localhost', 6600)

# Poll the volumelevel and set GPIO.output accordingly

while True:

    status=client.status()

    if status['volume'] > '0': # 1st LED
        GPIO.output(05,GPIO.HIGH)
        print('volume > 0')
    else:
        GPIO.output(05,GPIO.LOW)
        print('volume = 0')

    if status['volume']>'50': # 2nd LED
        GPIO.output(06,GPIO.HIGH)
        print('volume > 50')
    else:
        GPIO.output(06,GPIO.LOW)

    if status['volume']>'75': # 3rd LED
        GPIO.output(12,GPIO.HIGH)
        print('volume > 75')
    else:
        GPIO.output(12,GPIO.LOW)

    if status['volume']>'88': # 4th LED
        GPIO.output(13,GPIO.HIGH)
        print('volume > 88')
    else:
        GPIO.output(13,GPIO.LOW)

    time.sleep(0.2) # Delay loop for 0.2 second

# disconnect
client.disconnect()

# end of programm


This is the warning for each GPIO:
Code: Select all
LED_volume.py:13: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(05,GPIO.OUT)
...


Do you coding guys have any input for me?
RPi 3 model B
Hifiberry AMP2
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
Joined: 19 Nov 2017, 18:20
Location: Austria

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

Postby pez » 22 Dec 2017, 00:04

Hey guys,
i have connected a LED to the 4th GPIO output and it acts as expected EXCEPT:
At volume level 8 the pythonscript outputs
Code: Select all
Volume > 0
Volume > 50
Volume > 75

and at Volume level 100 the LED goes off!

Do you have an idea what's going on?
Could you give some hints what to do?
RPi 3 model B
Hifiberry AMP2
runeaudio 0.4 beta
work on autonomous hifibox in progress:
Rotary encoder, I/O witch, status and volume LEDs: DONE
design of housing: almost done
pez
 
Posts: 20
Joined: 19 Nov 2017, 18:20
Location: Austria

support RuneAudio Donate with PayPal

Next

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 6 guests