***********************UPDATE OCT 10 2015
I left my original write up unedited. But made quite substantial changes. Instead of using IFTTT to switch on/off WeMo if the Sonos state changes, I'm using direct SOAP commands for the particle to accomplish that. This approach is much more robust, as:
- Avoids delays, that could be 5+ seconds using IFTTT
- There was a circular reference, that in some circumstances made the previous solution unstable (a change of the WeMo switch triggered a function on the Particle through IFTTT, that in turn triggered the WeMo switch)
I'm not editing all the write-up, but will point out to some of the major differences. The new SW is on github.
The high level system view is on the picture below.
Only this IFTTT recipe is needed (please note ON is shown, OFF is as well required, just swap ON with OFF in all fields). If you have created the other one as per my original write-up (IF Particle THEN WeMo), please delete or disable that one.
I did some overall changes to the software, but the main difference is adding the SOAP calls to start/stop the WeMo switch), example below
For the SW use the NewController file
Please note that with this configuration you also need to create a static address for the WeMo Switch. Follow the instructions for "How to get and lock the Sonos IP address"
***********************ORIGINAL UNEDITED PROJECT
One of my Sonos'es at home is a CONNECT, source of my music for the home amplifier. The challenge is to switch the amp on/off when playing Sonos. The need to reach out for the remote removes largely the convenience of having the always-on Sonos available at the tap of my app. Of course the CONNECT:AMP is meant to be the component to be an always-on integrated amplifier. But there might be various reasons why that's not the option:
- the stereo at home might be just superior, with audiophile grade amp and speakers; one just needs the source to drive it (BTW- have also an installation like that; Sonos is bit perfect, but it has quite high jitter; this can be overcame by a jitter removal band DAC device in between, effectively making Sonos a high quality networked CD; I just wish would have support above 44.1kHz airstream)
- the installation supports multiple zones with ceiling speaker; one could theoretically replace with multiple CONNECT:AMP(s) but that's too much of a hassle
The amp is usually not meant to be left always-on. Either it consumes too much energy, or like in my case additionally it is housed in a cabinet, so having it powered 24/24 just generates quite a lot of heat.
I looked at the solutions on market and there are analog switches that detect music signal and are able to power on/off a switch. Beside not really being cheap, I found it quite limiting and started looking at a better way.
My solution is to use a Particle Photon to detect Sonos state and do something based on that. Basically, the Photon pings Sonos every couple of seconds and detects whether there has been a change of state (could be PLAYING, PAUSED_PLAYBACK, STOPPED, TRANSITIONING).
Once I have that one, different options are possible. One solution would have been to add a relay to a GPIO of the Photon. While probably the least expensive and less complicated (which could translate to better reliability), I took slightly different route with a WeMo switch integration. The biggest advantage from my POV was the ability to add a simple Alexa Echo integration. Some PROS and CONS of the approach I chose:
PROS:
- no soldering; I just use out of box components
- can do more; I can not only switch the system through Sonos app, but as well WeMo app or just press the button on the WeMo switch, which will also activate Sonos
- last but not least, indirectly but in a simple way I can integrate Amazon Echo, and power on the amp and Sonos through a voice command
CONS:
- probably more expensive, the WeMo switch is around $50
- complex cloud solution; I'm experiencing delays of 5+ seconds for the IFTTT event to be detected; generally, more things can go wrong; having said that, seems to be working fine so far
Amazon Echo integration
This is the nicest part- after you've done everything else in the project, this part is trivial. Just go to the Alexa app, in the Settings part find Connected Home and add the WeMo switch. Name it in some distinct but recognizable way, so Echo can understand. I have it named as "speaker", so when I say "Alexa speaker on", the WeMo switch is activated, which in turn puts Sonos in play.
IFTTT integration
This is not difficult, but if you are new to IFTTT you might want first to go through their tutorials. There are actually two different sets of commands: triggered by Particle Photon, doing something open the WeMo switch and the reverse.
Before you start creating the recipes, you need to connect to the Particle and WeMo Switch channels. In the ifttt.com, after registering/ logging in, go to Channels, search for the above ones (separately), press Connect and follow the instructions:
- for Particle, you need to first have an account registered on particle.io and the Photon that you are going to use registered to your account
- For WeMo Switch, you need the switch to be activated; in the Setting part of the WeMo app on your phone you need to generate a Pin that's going to be used for IFTTT registration
Particle Photon triggering WeMo switch
Create two recipes. One shown below, the other one analogous (replace OFF with ON)
WeMo Switch triggering Particle Photon cloud function
Create two recipes. One shown below, the other one analogous (replace ON with OFF). Please note that the cloud function is NOT visible in IFTTT until you run the program on the Photon.
Program highlights
Will not explain line by line, just the most important sections
Cloud function
The function is Sonos is registered in the setup and called when the WeMo switch is either turned ON or OFF. Please note I created it not symmetrical. If the Switch is turned on (and that can be manual, via WeMo app or by Amazon Echo), it starts playing Sonos. Please note I did not try to make it too complicated, it just starts where it stoped. For instance in my case I'm listening to TuneIn Radio, so it will start that station. I can tell in the morning "Alexa speaker on". If one wants to select the music to play, go to Sonos app. If the switch is turned off, I do not send the stop command to Sonos. The lack of symmetry is intentional. If one has multiple Sonos installations and they are grouped, maybe the intention was to switch off Sonos connected to the amp.
Main loop
In the main loop the Sonos status is requested every couple of seconds and the response processes. If there was a change, an IFTTT trigger is generated to either turn on or off the WeMO Switch.
The on-board LED is used for rudimentary status indication. If Sonos is playing is on, otherwise off. Additionally, there is an overlay short blink ("heartbeat"), indicating the unit is working.
I also added what I called redundancy check. Might be completely unnecessary, but given the system complexity, I figured out better do an additional check. So every 5 min, if Sonos is not playing, a command is sent to the WeMo Switch (through IFTTT) to switch it off. Normally, that situation should not occur but in case a command was lost, this prevents the amplifier to stay on.
SOAP API to Sonos
Sonos can be controlled through SOAP API calls. I have implemented 3 basic ones: check Sonos status (is it playing or not), start and stop at whatever was playing last. For my project this was enough, I figured out I need more complex interaction I would revert back to the Sonos app. But theoretically, more complex solutions are feasible.
How to get and lock the Sonos IP address
Line 6:
byte server[] = { 192, 168, 0, xxx }; //put your Sonos_IP_address
Needs to be changed to reflect your system IP address. Depending on your home router, it might require looking at the manual.
Generally, if you have not changed the admin password, typically on the label on the router there are instructions. For instance for TP-Link routers one would go from the web browser to 192.168.0.1 and login with admin as username and password as well.
Once you are in the router interface, look at the DHCP client list and find the Sonos, might have an address like 192.168.0.173.
To make sure the address does not change, put it as reserved address, basically locking it to Sonos. Instructions for TP-Link, for other routers do a search:
http://www.tp-link.com/en/article-182.html
Particle Photon circuit
Well, there is none. As I mentioned in the intro, one could build this project with a relay and in such case use a GPIO on the Photon to control a relay. With the setup I did, only installation required is to connect the Photon to a power supply. Any micro USB phone charger would do. As for the Photon it is a good idea to somehow protect it. Could leave on a breadboard. Or buy a small container. Myself I used Loctite Insulating and Sealing Wrap that you can get off Amazon or in Home Depot. It has the advantage of being transparent- so LED status is visible, as well as flexible, so the reset/ restart buttons can be pressed if that becomes necessary.
Comments