Saturday 9 June 2018

MuSe - Simple as a button

To become part of everyday life the Multi-room Music Server (MuSe) needs to be easy and friendly.
We have been working towards this at each stage of the project and by now we can do everything we want  to control music using one tool or another.  Ipad / android apps allow albums or streams to be played, a web interface is available for those who prefer it and a customised web app allows radio stations to be loaded easily.  Server status and startup can be done through a web interface.
All these tools require an app or browser running on a phone, tablet or PC.  A lot of the time this is exactly what is required and is not inconvenient.  However sometimes we want something more akin to a remote control.  It will have limited simple functions to do things like skip the current track or pause playback.

Button logical design

We envisage a button or buttons , operated by battery, which can be used in any room for specific pre-defined functions.
A screen / LCD should not be required as functionality will be simple and phone / tablet can be used for more complex actions.
Wifi connectivity should be used to allow the flexibility of using the button anywhere in the house.

To communicate with the RPI mpd server we could program C sockets, use http/cgi or utilise the websocket capability previously implemented for server control.  The type of information sent across would be mpc commands or perhaps bluetooth start / stop.  It would be nice to get a response back to the button to confirm success/failure.

Physical design

Two wifi enabled processors spring to mind, a Raspberry Pi Zero or an Arduino ESP8266.  As we only require simple functions the ESP8266 is the preferred way forward.
An Arduino websocket library is available which works with the ESP8266 so it should be possible for 2-way communication from an ESP8266 button to the websocket port.  Scripts on the RPI server can then process a button command and carry out the function on RPi.
We may find a number of buttons useful, we can easily have as many as there are free GPIO pins (about 8) on an ESP8266.  We use an ESP8266 "vertical" breakout board which mounts the ESP8266 on a small board with pins for all the available GPIO pins.
Pull up resisters are used so that when a button is pressed the GPIO pin goes from HI to LOW.
For programming, implementation and testing it is helpful to have Reset (RST) and Flash (GPIO0) buttons and FTDI connections.
Battery power can be provided by 2 or 3 AA cells plus a switch.

Our ESP8266 design is simple, we connect up to an FTDI232  on a breadboard and provide RST, FLSH buttons.  This basic setup allows us to download and run programs on the ESP.  ESP GPIO2 can be used to flash an LED, indispensible for testing.  We add a button on GPIO16; a pull-up 10K resistor keeps the pin high and the button grounds it when pressed.
The ESP8266 raw pin is used connected to +4.5V battery so that the ESP button can be used without FTDI connected.

Programming

I chose websocket library from links2004 which seems to work very well. We only need a websocket client as the server has been previously setup using websocketd.  A specific ESP8266 example is provided so we just need to enter wifi and URL/port details.  After flashing to the ESP8266 the program successfully communicates with websocketd on RPi.
It is now straightforward to program a button so that it causes websocket.sendTXT to send a command "mpc next" to websocketd which then handles it in a script called mpc.sh and sends mpc next to mpd and changes track.
Care is needed to ensure the command isn't sent repeatedly.  I used an LED indicator to show when the command has been sent so the button can be released appropriately.

Keypad

As an upgrade to buttons a keypad provides extra functionality / flexibility.  Buttons are adequate for basic connection, play/pause, skip but it is also useful to have various presets, e.g. to choose a specific channel or playlist.
A 4x4 touch keypad only costs about £2 and an arduino keypad library is available so that it can be easily programmed.  The keypad just has 4 row pins and 4 column pins so you just connect 8 GPIO pins, configure a basic keypad sketch and you can then use it.  Initially I tested with a Uno using a simple hackster tutorial and then moved on to the ESP8266.
The options are also made available on the web app where they can be tested first.  Keys 1,2,3 should be the same as the 3 buttons so that both solutions can be implemented - we may want a button control or keypad in each room.