Android Things – Building a Distributed Piano with Nearby Connections API

Android Things – Building a Distributed Piano with Nearby Connections API

In the previous post, we had a short introduction to some hardware basics for the Software Engineer. We learnt about some different components and how to create a blinking LED with Android Things. In this blog post, we will learn to build a distributed piano with Android Things, a Piezo Speaker and Google’s Nearby API.

Hardware setup for an Android Things Distributed Piano:

You will need:

In order to create the speaker component, set up your breadboard with the piezo by following the diagram below:

Distributed Piano Piezo Speaker Setup
Distributed piano piezo speaker setup

Attach the speakers to a PWM pin (red line in this diagram) and to a GND pin to complete the circuit.

If you remember from the previous blog post, we used the GPIO pin to send an on/off signal to the LED. Because the speaker requires us to send different notes to it, a simple on/off wouldn’t work in this case, which is why we use a PWM pin.

PWM (Pulse Width Modulation) is a technique used to encode a message into a pulsing signal. Its main use is to control the power supplied to electrical devices. PWM is used for things such as controlling servo motors or controlling a piezo.

Software Component

Once the hardware is set up, we can move onto creating the piano apps. The descriptions below will highlight the important parts, the full source code can be found here.

We will create two apps, one that will run on the Android Things compatible board and the other that will act as the companion piano app.

Android Things App

  1. Once we have created a project, make sure to include the dependencies for Android Things, Google Play Services and the speaker driver in your build.gradle. In your AndroidManifest.xml, include the permission to ACCESS_NETWORK_STATE  as we will need this whilst using the Nearby API. Define the SERVICE_ID and make sure your PianoActivity  is set to be the IOT_LAUNCHER.

  2.  In the PianoActivity, we will broadcast that we are allowing “nearby connections” on this device. This code is based off the sample provided here for nearby connections.

  3. On receiving a message from the nearby API, translate the message into a frequency that the speaker will play. This example is using the PWM speaker driver from here.


    That is all that we require for the Android Things component.

Companion Piano Player App

  1. Create a module inside your current project for the companion app. This app will need to include the Nearby Connections API dependency too.
  2. In the layout xml file, we will include a keyboard view and a scrolling bar to scroll the keyboard.  I have extracted the KeyboardView  from the repository that can be found here.
  3. In the activity, create a GoogleApiClient and start listening for any connections that are advertising themselves. For simplicity, we will just connect to the first endpoint that is discovered.


  4. Obtain a reference to the KeyboardView and attach a listener which will trigger when the notes are pressed on and off.
  5. When a note is pressed send a message via Nearby API with the corresponding note frequency to the connected Raspberry Pi. Send a “stop” signal, when the KeyboardView listener detects that the note has stopped playing.

That is how you can make a distributed piano using Android Things. For the full source code check the github repository here.

Conclusion

By using Android Things and the Google Nearby API we could quite easily create a distributed piano. This post demonstrated how easily you can build your own Android Things Piano.

If you manage to play something awesome with this example, please share it with me on twitter @riggaroo. Check this Sia cover by Gautier Mechling:


If you want to get updates from this blog, be sure to subscribe below:

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Comments are closed.