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

Raspberry Pi related support

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

Postby pez » 23 Dec 2017, 00:36

Well, i am making progress, but still have weird behaviour.
At volume 100 everything is OK now, all LEDs are on.
But still: at volume 8 three LEDs are on, as if it would be between 75 and 87.
Maybe it's a python vs. python2 thing?
I am really curious about what's wrong here!

That's my current script and wiring diagramm for the volume LEDs (see attachment):

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

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

from mpd import (MPDClient)

#GPIO Setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

LED1 = 05
LED2 = 06
LED3 = 12
LED4 = 13

GPIO.setup(LED1,GPIO.OUT, initial=GPIO.LOW) #1st LED
GPIO.setup(LED2,GPIO.OUT, initial=GPIO.LOW) #2nd LED
GPIO.setup(LED3,GPIO.OUT, initial=GPIO.LOW) #3rd LED
GPIO.setup(LED4,GPIO.OUT, initial=GPIO.LOW) #4th LED

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

# Poll the volumelevel and set GPIO.output accordingly

while True:

    status = client.status()
    volume = status['volume']

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

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

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

    if status['volume'] > '87': # 4th LED
        GPIO.output(LED4,GPIO.HIGH)
        print('volume = ' + str(volume))
    else:
        GPIO.output(LED4,GPIO.LOW)

    if status['volume'] == '100': # ALL LEDs
        GPIO.output(LED1,GPIO.HIGH)
        GPIO.output(LED2,GPIO.HIGH)
        GPIO.output(LED3,GPIO.HIGH)
        GPIO.output(LED4,GPIO.HIGH)
        print('volume = ' + str(volume))

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

# disconnect
client.disconnect()

# end of programm
Attachments
RaspiHifBox03.png
RaspiHifBox03.png (8.82 KiB) Viewed 2326 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 » 23 Dec 2017, 22:07

OK, i tried increasing volume by 1 and it's like that from 1 to 9 the script behaves like from 10 to 90 in steps of 10.
That's why at 8 it thinks it is at 80.
What can i do about that?
Any ideas?

Cheers and merry christmas, P
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 » 24 Dec 2017, 00:12

Hi pez,

Any ideas?


You are comparing strings that means
Code: Select all
>>> '9' > '12'
True
>>> '7777' > '8'
False
>>>


You have to convert the string to an int first.
Code: Select all
#!/usr/bin/env python2
# LED_volume.py

# IMPORTS
import sys
import time
import sys
import RPi.GPIO as GPIO
from mpd import MPDClient

#GPIO Setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

LED1 = 05
LED2 = 06
LED3 = 12
LED4 = 13

GPIO.setup(LED1,GPIO.OUT, initial=GPIO.LOW) #1st LED
GPIO.setup(LED2,GPIO.OUT, initial=GPIO.LOW) #2nd LED
GPIO.setup(LED3,GPIO.OUT, initial=GPIO.LOW) #3rd LED
GPIO.setup(LED4,GPIO.OUT, initial=GPIO.LOW) #4th LED

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

# Poll the volumelevel and set GPIO.output accordingly

while True:

    status = client.status()
    volume = int(status['volume'])

#    print('volume = ' + str(volume))

    if volume == 0 : # 1st LED
        GPIO.output(LED1,GPIO.LOW)
        GPIO.output(LED2,GPIO.LOW)
        GPIO.output(LED3,GPIO.LOW)
        GPIO.output(LED4,GPIO.LOW)

    if volume > 0 and volume <= 50: # 1st LED
        GPIO.output(LED1,GPIO.HIGH)
        GPIO.output(LED2,GPIO.LOW)
        GPIO.output(LED3,GPIO.LOW)
        GPIO.output(LED4,GPIO.LOW)

    if volume > 50 and volume <= 75: # 2nd LED
        GPIO.output(LED1,GPIO.LOW)
        GPIO.output(LED2,GPIO.HIGH)
        GPIO.output(LED3,GPIO.LOW)
        GPIO.output(LED4,GPIO.LOW)

    if volume > 75 and volume <= 87: # 3rd LED
        GPIO.output(LED1,GPIO.LOW)
        GPIO.output(LED2,GPIO.LOW)
        GPIO.output(LED3,GPIO.HIGH)
        GPIO.output(LED4,GPIO.LOW)

    if volume > 87 and volume < 100: # 4th LED
        GPIO.output(LED1,GPIO.LOW)
        GPIO.output(LED2,GPIO.LOW)
        GPIO.output(LED3,GPIO.LOW)
        GPIO.output(LED4,GPIO.HIGH)

    if volume == 100: # ALL LEDs
        GPIO.output(LED1,GPIO.HIGH)
        GPIO.output(LED2,GPIO.HIGH)
        GPIO.output(LED3,GPIO.HIGH)
        GPIO.output(LED4,GPIO.HIGH)

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

# disconnect
client.disconnect()

# end of programm


This script gives you the following behaveor:
- volume = 0 all LEDs off
- volume between 0 and 50 only LED1 is on
- volume between 50 and 75 only LED2 is on
- volume between 75 and 87 only LED3 is on
- volume between 87 and 100 only LED4 is on
- volume = 100 all LEDs on

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 » 24 Dec 2017, 12:41

Awsome, that's it, thank you, Frank!

Only when i try to start that at boot time with a service as i did with the other scripts, i get following error.
When i start the service manually it works!
Again, do you know what's wrong?

Code: Select all
root@runeaudio(rw):~# systemctl status LED_volume
● LED_volume.service - RuneAudio LED_volume
   Loaded: loaded (/usr/lib/systemd/system/LED_volume.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2016-02-22 15:40:17 CET; 28s ago
 Main PID: 297 (code=exited, status=1/FAILURE)

Feb 22 15:40:17 runeaudio python2[297]:   File "/home/root/hw-control/LED_volume.py", line 26, in <module>
Feb 22 15:40:17 runeaudio python2[297]:     client.connect('localhost', 6600)
Feb 22 15:40:17 runeaudio python2[297]:   File "/usr/lib/python2.7/site-packages/mpd.py", line 413, in connect
Feb 22 15:40:17 runeaudio python2[297]:     self._sock = self._connect_tcp(host, port)
Feb 22 15:40:17 runeaudio python2[297]:   File "/usr/lib/python2.7/site-packages/mpd.py", line 403, in _connect_tcp
Feb 22 15:40:17 runeaudio python2[297]:     raise err
Feb 22 15:40:17 runeaudio python2[297]: socket.error: [Errno 111] Connection refused
Feb 22 15:40:17 runeaudio systemd[1]: LED_volume.service: Main process exited, code=exited, status=1/FAILURE
Feb 22 15:40:17 runeaudio systemd[1]: LED_volume.service: Unit entered failed state.
Feb 22 15:40:17 runeaudio systemd[1]: LED_volume.service: Failed with result 'exit-code'.


The service file ist this:

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

[Service]

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

[Install]
WantedBy=multi-user.target


Cheers, Pe
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 » 24 Dec 2017, 12:57

Hi,

this is a networking issue.

You can try:
Code: Select all
client.connect('127.0.0.1', 6600)

instead off:
Code: Select all
client.connect('localhost', 6600)


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 » 24 Dec 2017, 23:32

Hi Frank,
i just tried that with 127.0.0.1, runeaudio.local, IPadress but nothing helped.
I even changed the service to this but it didn't help either. Still when i start that manually, everything works fine...

Code: Select all
[Unit]
Description=RuneAudio LED_volume
After=network.target mpd.service

[Service]

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

[Install]
WantedBy=multi-user.target
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 » 26 Dec 2017, 23:57

Hi pez,

please try something like this:
Code: Select all
[Unit]
Description=RuneAudio LED_volume
After=mpd.service network.target

[Service]
ExecStart=/home/root/hw-control/LED_volume.py
ExecReload=/usr/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
RestartSec=10
StartLimitInterval=30
StartLimitBurst=20

[Install]
WantedBy=multi-user.target


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 » 27 Dec 2017, 12:04

Hey Frank, you are my personal hero.
That was the last bit of code needed to get that running.
I am currently working on the design and circuit layout for everything i need to get started to actually build that thing.
Chassis and parts for crossover are on their way, I'll start off with a mockup speakerbox just to check audio quality.
I'll post my progress, maybe it's better to move to another section of the forum, since everything seems to work fine now?
Cheers and mille gracie, 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 breizheau » 29 Dec 2017, 20:42

Hi there.

I have just subscribed to this board and this thread is exactly what i needed !!!!!

I bought a rpi3 ( already got it and use Runeaudio with my headphones plugged on jack pkug) and the new Amp2 from Hifiberry that i hope to receive soon...

I wanted to have a on/off switch and a rotary encoder !
I have relays, momentary switches, resistors, +5 DC power supply, prototyping boards and......time !

I think i'll have work to do soon :D
My encoder has the momentary switch built-in. 8-)

Do you use a specific encoder or not ?

Cheers.

Olivier
Rpi 3 B V2 (2015)
RuneAudio last Beta 0.4b version (2017)
NAS Synology
breizheau
 
Posts: 4
Joined: 29 Dec 2017, 17:52

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

Postby pez » 30 Dec 2017, 12:14

Hey Oliver,
in my first post i included a link to the encoder i'm using.
For the AMP2 you will need a power supply between 12 and 24V (depending on your loudspeakers): no 5V PS needed, the AMP supplies the RPi!
I'm using the included switch of the encoder to play/pause and a seperate one for I/O.
Ony the relay is controlled by the USB 5V so it switches depending on on/off state of the RPi. It controls the two functions of the I/O switch and the red/green status LED. Attached you will find my final electric scheme of all that.
pez
Attachments
relais_simple.png
relais_simple.png (23.74 KiB) Viewed 2094 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

support RuneAudio Donate with PayPal

PreviousNext

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 8 guests