Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Instruction Guide Activate relay upon audio output with python
#1
Have an amp that doesn't feature auto power on/off? 
Have a Raspberry Pi with GPIO? Great. All you need now is a relay.

*Note: This only works for amps which are "always on" by a switch.
--------
Requirements:
- Pi w/ a GPIO available.
- Relay compatible with the Pi and your voltage (Typically 120v or 240v)
- A way to connect your relay into the power feed of your amp, and the relay to the Pi.
- Screen package
- Python 2.7 or newer, which is included with moode. (I highly recommend python 2, otherwise you'll need to change the env at the beginning of the script) 

-------
I'm using GPIO 12 for my setup, and an always running loop within python that is running within a screen session.

Now the how-to,
You're going to need screen.
Installing screen: apt install screen

I used GPIO 12 for my setup, you can change in the config what GPIO pin you'd like to use, although it doesn't really matter if you have GPIO 12 open, then it's going to be pretty much a quick install without any modification to the script. 

You'll need to download my script into your home directory. (/home/pi) Otherwise you'll need to modify the crontab directory once we set that up. Here is the download to the script: https://drive.google.com/open?id=1JkVnpr...CwLQV3ndHh 

Now if you choose to, you can edit the script and make the changes needed to your setup, however I've made it work for most. 

That is all for now, I am currently figuring out how to get crontab or systemd to create a screen session at boot so that it auto starts.
First off, we'll have to enable crontabs.
sudo systemctl enable cron.service

Now, we're going to edit the crontab. 
crontab -e
(Select nano)
add to the bottom the following:
@reboot /usr/bin/screen -dmS python /home/pi/allinone.py
(CTRL-X then y then hit enter to save and exit nano)
Now it should automagically start with your pi Smile



Additional info:
If you don't know the default ssh login, here you go:
SSH login user: pi, pwd: moodeaudio

I am running Python 2.7 under
Reply
#2
Not a comment on your instructions but on your recommendation to use Python 2.

Python 2, currently Python 2.7, will be officially deprecated by the Python Software Foundation on 2020/01/01.

From the Debian Python Policy statement:

Quote:Debian currently supports two Python stacks, one for Python 3 and one for Python 2. The long term goal for Debian is to reduce this to one stack, dropping the Python 2 stack at some time.

They haven't said how soon after 2020/01/01 this will occur.

Some Python 2-isms run as-is in Python 3. Some don't. There are porting guides and utilities available to aid in the transition. 

Best to stay ahead of the curve. 

Regards,
Kent

PS - if you like this sort of thing, here's a countdown clock.
Reply
#3
I'll go ahead and test it soon under python 3.
Reply
#4
(05-14-2019, 02:25 AM)nahventure Wrote: I'll go ahead and test it soon under python 3.

Hi! Any luck porting it to python3?
Reply
#5
(11-03-2019, 12:17 PM)mike_d Wrote:
(11-02-2019, 08:35 PM)lirus Wrote:
(05-14-2019, 02:25 AM)nahventure Wrote: I'll go ahead and test it soon under python 3.

Hi! Any luck porting it to python3?

I installed moode today and was looking for the same solution and found this post. 

I made some modifications so I thought I would share.

https://gist.github.com/michaelfdeberry/...1a7e09f3de

Updated to python 3 and added a delay before deactivating the relay to account for track changes.

I am installing it as a service instead of using cron, so you don't need to install screen with this approach. 

There are some basic instructions in the gist.

Thanks mike_d!
Reply


Forum Jump: