Gesture Controlled Radio

Our goal was to make an kitchen Internet radio player where we could change the volume and stations without touching the player. For this project we used:

  • Raspberry Pi 3 (or 2 with a Wifi dongle)
  • Pimoroni SkyWriter (~$20US)
  • USB powered portable speaker

The Pimoroni SkyWriter supports both gesture and touch control.

Setup

To install the Pimoroni SkyWriter python libraries:

curl -sS get.pimoroni.com/skywriter | bash

For the internet music interface we used the mpd , Music Player Daemon, it can be installed by:

sudo apt-get install mpd mpc

The SkyWriter can be wrapped in plastic wrap and the gestures can still be picked up. For a more robust project it would probably be best to design a more waterproof enclosure.

Python Code

A few years ago we found that the mpc calls were bullet-proof, but when we did this project we found that the mpd service would often lock up  if a new internet radio station was selected (mpc next). For this reason we used external calls to adjust the volume and we restarted the mpd service when we changed radio stations

We used a gesture of up/down to adjust the volume, and a left/right to change the station.

OLYMPUS DIGITAL CAMERA

Below is our final code:

#!/usr/bin/env python
import skywriter
import signal
import os

thevolume = 70 #starting volume
thestation = 1 #starting station

# Some internet radio stations
stations = (('http://185.33.21.112:11029','1.FM Amsterdam Trance Radio'),
            ('http://www.partyviberadio.com:8000','Raggae Roots'),
            ('http://66.85.88.2:7136','Comedy 104'),
            ('http://eu.radioboss.fm:8121','Yoga'),
            ('http://185.33.21.112:11269','Baroque'))

def setvolume(voldif):
  global thevolume
  if (thevolume + voldif >= 0) and (thevolume + voldif <100):
    thevolume = thevolume + voldif
    os.system('amixer sset "PCM" ' + str(thevolume) + '%')
    print 'volume = ' , thevolume

def newstation(direction):
  global thestation
  print direction , thestation , len(stations)
  if ((direction + thestation >0) and (direction + thestation <= len(stations))):
    thestation = thestation + direction
    os.system('mpc clear')
    os.system('sudo service mpd restart')
    os.system('mpc add ' + stations[thestation][0] )
    os.system('mpc play')
    print thestation, stations[thestation][0], stations[thestation][1]

@skywriter.flick()
def flick(start,finish):
  print('Got a flick!', start, finish)
  if (finish == 'north'):
    setvolume(5)
  if (finish == 'south'):
    setvolume(-5)
  if (finish == 'west'):
    newstation(-1)    
  if (finish == 'east'):
    newstation(1)

# start playing the radio at defaults 
setvolume(0)
newstation(0)

signal.pause() # wait for a new gesture

House Music

Our goal was to have the same music playing throughout the house, and then use a phone to control the music station and volume.

screenshot

For this project we used a technology called MQTT (Message Queue Telemetric Transport). MQTT is based on the following standard concepts:
– MQTT Broker manages and distributes messages
– MQTT Publisher a source for the data
– MQTT Subscriber an application that wants the data

layout2

Pi Setup

On the Raspberry Pi’s we needed to load a Python MQTT library and a music player (mpd – music server, mpc – music client):

sudo pip install paho-mqtt
sudo apt-get install mpd mpc

The key commands to manage playing of Internet radio stations are:
mpc add radio station url– this will add a radio station to a play list
mpc play play list number – play a selected play list
mpc volume number – change the volume from 0-100%
– mpc clear – clear the play lists

Internet radio stations can be found by going to https://www.internet-radio.com. To find a radio station URL right click on any of the radio station links and select “Copy link address”

On each of the Pi’s the Python program subscribes to an MQTT topic called Radio. New Internet Radio Stations are passed as strings, and new volume settings are passed as numbers between 0 and 100. For this application the MQTT payload could be either an integer or a string so a decode function (msg.payload.decode(“utf-8”)) is used to convert from a byte array to a generic string.

When a new radio station is received:
– the current play list is cleared (mpc clear),
– the new radio station is added to the play list (mpc add),
– the volume is reset to it’s original (mpc volume), and
– the radio station is played (mpc play).

If the payload is numeric only the volume is changed. The full Pi Python program is shown below:

# mqtt_2_mpc.py - have MQTT change Internet Radio Stations
#
import paho.mqtt.client as mqtt
import os

thevolume = 75  # save the volume

# Subscribe to topic: Radio

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))
  client.subscribe("Radio")

def on_message(client, userdata, msg):
  global thevolume
  print( msg.payload)
  themsg = msg.payload.decode("utf-8")
  # if the message is a number it's the volume
  if themsg.isnumeric():
    thevolume = themsg
    os.system("mpc volume " + thevolume)
  # if the message is a string it's the station URL
  else:
    os.system("mpc clear")
    os.system("mpc add '" + themsg + "'")
    os.system("mpc volume " + thevolume)
    os.system("mpc play")

client = mqtt.Client()
client.connect("iot.eclipse.org",1883,60)

client.on_connect = on_connect
client.on_message = on_message

print ("Waiting for messages .....")
client.loop_forever()

Web Page Setup

On the Web page the Paho JavaScript client is used to connect to an MQTT broker. It is important to note that the JavaScript interface uses Web Sockets to communicate with the MQTT broker, (typically on port 80) rather than the native MQTT port of 1883. If you install your own MQTT broker, you will need check the document for Web Socket setup and support.

layout3

The final Web page that we used is listed below.

websock1