Introduction: SONOS Remote Control
Sonos speakers are great sounding and highly practical since you can control them from your phone, tablet or computer. I use the one installed in my bedroom to wake me up in the morning to my favorite tune. The speaker is installed at the other end of the room so if I want to stop the alarm, I must either walk to the speaker or launch the Sonos application from my mobile phone which is not always nearby since I don’t typically sleep with my phone. In order to wake up more gently, I decided to build this Sonos remote control. It allows to stop the alarm, play/pause the currently playing tune, skip to the next one and raise/lower the volume.
It is based on a ESP8266 combined with a cheap ATTINY85 that allows easier and better power management and will give you over a year of operation on a small 500 mAh Li-Po battery.
IMPORTANT: In order to get this remote to work, you’ll need a device able to run node.js. This could be a desktop computer or a Raspberry Pi. On top of it, you will need the excellent Sonos HTTP API by jishi which is based on node.js. I installed it on my desktop and configured it as a service. More on this later.
Step 1: Material
Here is the list of material you will need
- 2 x Momentary push buttons ()
- JST connector
- Li-Po Battery 500 mAh
- 2N3904 transistor
Step 2: Installing the SONOS HTTP API Software
I recommend reading this article about the HTTP API’s installation procedure.
Once this is done and you’ll be able to send commands to your in-house web server in the form: http://localhost:5005/Room/playpause
Make sure your host based firewall allows this traffic by testing the connection from another device on the same network. Once this works, we can proceed and configure the web server as a service.
Configuring the HTTP API as a service
First, we need to download and install the nssm software found here: https://nssm.cc/download
Open an Administrative command prompt and type the following:
c:\> NSSM install SONOS
In the dialog box, fill up with the parameters as shown in the images above. Make sure you specify the paths and directories you used when installing Node.js and the SONOS HTTP API.
Once configured, you can hit the "Install Service" button. This way, you’ll be sure the Sonos API HTTP server will always automatically start when you reboot your machine.
Step 3: NETWORK Configuration (optional Step)
In order to speed up the connection time required by the ESP01 to connect to the network and issue the command to the SONOS HTTP API server, I recommend that you bypass the DNS and DHCP requests and use the direct IP addresses instead. This is done in your home router where you need to specify the network MAC address of the machine hosting the HTTP server as well as the MAC of the ESP01. In my NetGear router, this is done through the LAN Setup menu. In my case, the DESKTOP device runs the SONOS HTTP API webserver and the SonosRem is the Sonos remote we’re about to build.
Check your own router user’s manual on how to do this on your specific router.
Step 4: Building the Circuit
I initially built this circuit without the ATTINY85. It worked fine but I went through batteries way to fast, even though the ESP01 was almost always in deep sleep mode with the LED unsoldered. This might have been caused by some object being left on the remote who kept the button pushed indefinitely. I'll never know exactly but I still wanted to handle this situation.
Using the ATTINY85 as proved to be a wise choice for this. The ATTINY code is using less than 2uA while in sleep mode. I use hardware interrupts on the buttons to wake it up. Once triggered, the ATTINY finds out which button was pressed, single or double-clicked. It then switches the ESP01 on through the 2N3904 transistor, wait for it to come online and sends the command associated with the button pressed.
The first command sent will take between 3 to 10 seconds to perform since the ESP01 has to boot, connect to Wifi and send the command to the web server. The ESP01 stays awake for another 20 seconds after the last command in order to avoid the repeat of the connection time.
Step 5: The Code
Here is the Attiny and ESP01 code. You will have to install Nick Gammon's SendOnlySoftwareSerial library from here to be able to compile the Attiny code.
I had to resort to use this library because of incompatibilities between the different SoftwareSerial libraries I tried and the Interrupt handling libraries of the Attiny. Because of this, the ESP01 signals it is done with commands requested by the Attiny by raising its GPIO pin to HIGH for a few ms.
Step 6: Building the Device
I made a PCB with the components soldered and 3D printed an enclosure for the device. Please note that I used breakaway headers for the ESP01 and machine pin breakaway headers for the ATTINY so the chips can easily be removed and updated if needed in the future.
I also 3D printed a device holder so the remote can serve as an easy to reach “Stop the alarm” button in the morning. I position the remote upside down on it. There is step in the bottom of the holder where the top button sits. Pressing on the back of the device sends a play/pause command to the SONOS speaker.
I hope you like it. Let me know if you have questions or comments, I try to answer every single one of them.
Step 7: Using the Device
Top button (single click) : PlayPause
Top button (double click): PlayNext song
Bottom button (single click) : VolumeUp
Bottom button (double click) : VolumeDown