Friday, September 11, 2020

LAN/WiFi Speaker Switch

Preamble


I've had a Niles SS-4 Speaker Selection System for some 25 years, which coincided with installing speaker wire into several rooms throughout our multi-level house. At that time there was no Bluetooth speakers or other WHA (Whole Home Automation) equipment to enable remote speaker control. The Niles unit also comes in a 6-speaker configuration as an SS-6 and is built like a brick house, and has performed flawlessly since installation. But this particular piece of hardware has some unique characteristics which make it fairly easy to covert to a LAN/WiFi enabled switch.

Project Requirements, Design and Philosophy


Searching the Internet for a WiFi enabled speaker switch yields lots of discussions about hardware approaches. There are also discussions why this kind of technology is not offered by manufacturers, to which the conclusion is there is not a viable market for this kind of specialty. 

Although I'm good with basic electric circuits and soldering, I am not an Electrical Engineer and most of the projects I discovered involved deep understanding of hardware schematics, specifications and even software development at a machine level. I even dabbled with ESP8266 circuits connected to standard relay boards like the SainSmart Relay Modules. The software support for this was almost non-existent and required an Android phone, which I do not have. I also explored the use of Arduino boards and technology and because I have zero experience with the Arduino platform I got mired in the details of how to build something simple.

So my design approach was to use "off-the-shelf" components at a higher integration level and a controller that I already had experience, which is the Raspberry Pi computer with the Linux O/S. I happened to have a spare Raspberry Pi 3B that was the foundation of this project. Some might say this platform is overkill for something as simple as on-off control of electrical switches, and that it is also not the cheapest, but if you want something simple that just works and doesn't require an EE degree read on.

Parts List


Optional For Testing
  • 5v 1amp power source (USB charger and cord with the phone end cut-off to use power wires) - you could also use batteries that light up a bulb
  • small 5v LED circuit (or a flashlight bulb would also work)

Here's the finished product before it will be installed at the media center with speaker wires:




In the finished photo above note that red/black speaker terminals are old-style permanent mounted type and the newer Niles units appear to have the "pluggable" type of terminals. What should be evident is how compact this design is. Since the Raspberry Pi has onboard WiFi the only cable connection required to the computer is the power to the unit. In my case I also have it hooked up to a TV as an HDMI monitor.

The Raspberry Pi 3B is installed below the PiPlate Relay Plate hat with eight wires running into the switch box to control 2 sets of speakers (the Niles switch box supports 4 speakers but this build only implemented 2 sets of speakers ). The PiPlate Relay Plate hat has 7 relays (other standalone micro relays usually come in sets of 4, 8, 16, etc.). I'm thinking about using the 7th relay to control a very quiet fan to keep the air circulation going with all my components in our furniture. The Raspberry Pi would do this with a thermostat piece of software that reads the CPU and GPU temps of the two RPis I have in the cabinet. But I digress...

Niles Speaker Switch Modifications



This photo shows what made this all work (and I tested manually before I ordered any parts or did any soldering). As noted above, I have an older Niles unit with these kind of physical pushbutton switches permanently attached to the unit's main circuit board. Unfortunately I'm unable to verify if the new units from Niles use the same pushbutton switches that I was able to exploit. Note there are many SS-4/SS-6 used units available on EBay and other sites that are older like my unit (and quite a bit cheaper). The pushbutton switches have these 6 short leads for both left and right channels. When you physically push in the switch, the back four leads are all connected and the circuit engages the both channels of the stereo speakers.




The 6 pins on the pushbutton switches are a bit too short to get a good grip on the female end of the standard 12 inch male/female jumper wires. So we'll use a tiny screwdriver to flip the tab on the plastic cover and slip it off.






















Now we have a bare metal lead that we can push onto the back two pushbutton switch pins and apply a dab of solder (it only takes a little) to adhere them to the switch. Below is the right channel of the first pair. Note this is only one channel of the stereo pair and it's the rear 2 leads that need to have wires attached. The coolest thing is that switches still work manually if needed, but as long as they are "off" the relays do the work instead of the mechanical switch.




This is all eight wires connected and soldered into place (note the color coded wires - makes the easier to identiry when attaching to the Pi Plate relay board).





Raspberry Pi build and preparation



Note the use bottom half of the "Pi-Plates CASEplate" to secure the Raspberry PI



This project assumes you have a working Raspberry Pi running the latest version of the Raspian O/S (recently renamed "Raspberry Pi O/S". It is beyond the scope of this project to give detailed instructions for how to build up your Pi, as there are numerous excellent How-To articles on the Internet that detail a new build from scratch. 

Here are two excellent beginning tutorials provided by the Raspberry Pi Foundation:

What's required is a vanilla standard installation and no special configuration (more on the control software later) except that you need the RPi to run on your local area network either with a WiFi connection or an Ethernet cable. It is also recommended that you assign the Pi a fixed IP address on your LAN.



Pi-Plates RELAYplate preparation


Hardware



The installation of the Pi-Plates RELAYplate is incredibly simple. Since it's a "HAT" you simply plug it into the 40 Raspberry Pi connector. Power to the RELAYplate is also done through the 40-pin connector, so no extra power is required. The Users Guide for the RELAYplate can be found:


Note in the above photo how nicely the standoffs work to layer the Raspberry Pi computer on the bottom and the RELAYpate on top of it. Although this case is optional, it is well worth the investment since it makes a bombproof, compact and tidy installation with plenty of ventilation. 



Next we're ready to hook up the male ends of our wires from the Niles switch to the relay terminals. Make sure you keep the polarity correct for the + and - of your speakers. Note the Pi-Plate RELAYPlate only provides "normally open" relay switching, which in our installation is exactly what we want. The circuit remains open until the relay is activated through the Raspberry Pi and RELAYplate, and at that point the circuit is closed and our speaker signal is passed through the switch just as if we had physically pushed the button.

Testing


Next is my test rig, where I fed the right channel "Speakers In" with 5v power from a USB charger. It's a good idea to test each one of your connections before doing all the final assembly. On the bottom of the photo is an LED plugged into the right channel of the first speaker pair. Notice the green LED on the PiPlate hat is glowing (showing it's on and ready for commands).




Next I pushed the button on my webapp software and voila! The circuit lit up the white test LED - Success!! Notice the 2nd blue LED on the RELAYplate lit up, which is the circuit that was closed and engaged.




Software


One of the key selling points of the Pi-Plate RELAYplate besides its dead simple hardware integration with the Raspberry Pi computer is the software support. They offer an almost turnkey Python app that controls each relay through a web interface. The instructions for installing the Python libraries and the code to run the webapp on the Raspberry Pi can be found:


This is what the default web app looks like in a browser from the code on that examples page. It uses a lightweight web server called "flask" comprised of a Python backend and a standard HTML file to render and enable the buttons






Modifying both the Python code and it's associated web page was easy with a simple text editor (no compiling necessary). Since I was only using 4 of the available 7 relays, I commented out the other 3 HTML buttons and customized the labels on the web page and the python code so they only display the four buttons that's needed to switch 2 sets of speakers. Here's a screenshot of my first webapp that has buttons for each channel so once again each circuit could be tested with the actual speakers hooked up: (the green button is "on" and red button is "off"




After adding only two lines of Python code (see code snippet below) and commenting out the other buttons in the HTML file, I now have an interface that enables both left and right speaker channels simultaneously.




The PiPlate-RELAYplate tiny library encapsulates all the GPIO business for functions like on, off, toggle and other board operations. It's really easy to control in Python, unlike some of the complexity one has to do with controlling GPIO pins directly on the Raspberry Pi. Below is the code snippet for the modification that closes two relays at once when only one button is pushed on the above web page:

    if state != None:    
        rly=int(state[0])   #parse relay number              
        action=state[2]     #parse action: 'n' for on and 'f' for off
        if action=='n':
            RELAY.relayON(ppADDR,rly)
### turn on 2nd relay simultaneously
            RELAY.relayON(ppADDR,rly+1)
        if action=='f':
            RELAY.relayOFF(ppADDR,rly)
### turn off 2nd relay simultaneously
            RELAY.relayOFF(ppADDR,rly+1)


Conclusion


My unit is now built, tested and deployed in my Media Center cabinet. It has worked flawlessly without any hitches since it was installed. It's nice that such an old piece of dedicated hardware like the Niles speaker switch can be so easily upgraded to a WiFi/LAN based IOT device a given a new lease on life.