2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

Create a fun, simple IoT accelerometer game

Note: IBM Data Science Experience (DSX) is now IBM Watson Studio. Although the name has changed and some images may show the previous name, the steps and processes in this tutorial will still work.

You can create a fun little game to play with your friends and colleagues. With anyone that has a smartphone you can play a game I call “Harlem Shake.” (Watch us shake it up at a recent DeepLearning meetup.)

Playing is easy: give someone a URL that they access on their smartphone, and then have them shake their smartphone as hard as they can. The person who uses the most energy wins!

What happens behind the scenes is pretty basic: accelerometer data from the smartphone is streamed to the cloud, captured in a database, and then a script calculates the winner. It sounds simple and fun. But, you have to build it first. And you’ll get to learn a lot about MQTT, NodeRED, ApacheCouchDB, and ApacheSpark along the way.

This IoT game, like all IoT apps, requires a complete system to work. In this tutorial, we’ll use IBM Cloud and the IBM Watson IoT Platform services:

  • Node-RED, which we use to create the IoT application and which we use to stream the data from the sensor to the database
  • IBM Watson IoT Platform, which we use as the MQTT message broker to hold it all together
  • Cloudant, a NoSQL database system that we use to store the data
  • An ApacheSpark SQL script, which we run in IBM Watson Studio, that tells you who wins

Obviously, all the smartphones will act as the sensors, streaming the data into our IoT app.

You can build the game in six steps:

  • Deploy the game application using one-click deployment.
  • Replace the Internet of Things Platform service.
  • Ensure the MQTT message broker can receive data.
  • Set up a NoSQL database to store the data.
  • Stream the data to Cloudant using Node-RED.
  • Analyze the data in a data science notebook.

What you’ll need to build your app

  • An IBM Cloud account.
  • A smartphone. Or, one or more friends with smartphones to play the game with you.
  • An IBM Watson Studio account. You can use the IBM ID you’ve created while registering for the IBM Cloud Account. To get started using IBM Watson Studio, including signing up for an account, watch the videos in this video collection.

0 Deploy the IoT Starter Application

  1. Log in to your IBM Cloud account.
  2. Create the IoT app including Node-RED. Follow these steps in this tutorial.

Deploy the game application using one-click deployment

After you have an IBM Cloud account in place, you can deploy the application in just one click. You’ll have to set up the deployment manually. I’ve created a sample IoT game app for you and put in on Github for you to download.

  1. Go to my discover-iot-sample repo in GitHub, and in the README instructions, click the Deploy to IBM Cloud link.
  2. Under “Select Region”, select “Dallas” screen capture deliver pipeline tile
  3. Click on the “Delivery Pipeline (Required)” icon.screen capture deliver pipeline tile
  4. Click on “Create” next to the “IBM Cloud API Key” field, then on the pop-up, click create again screen capture deliver pipeline tile
  5. On top left, click Create.
  6. Click the Deliver Pipeline tile. screen capture deliver pipeline tile
    1. In case you see a message like below, click on “Add the service” screen capture deliver pipeline tile
    2. Then, click on “Create” and return to the delivery pipeline screen capture deliver pipeline tile
  7. In the Build Stage tile, click the play button
  8. Wait until both stages turned green. Then click on “View console” in the deploy stage
  9. Click on “Visit App URL”
  10. Copy the URL (and either create a short URL or a QR code for the URL), and email it to yourself. You’ll use this link later to play the game. screen capture of the build stage tile (next to the deploy stage tile

While you can now send accelerometer data to the cloud, it won’t get stored. Next, we need to set up the data storage pipeline.


Subscribe to the data stream

Now that we can access Node-RED, let’s double-check if data that is coming from your smartphone arrives in the cloud (the MQTT message broker is a service that is part of the IBM Watson IoT Platform).

We need to use Node-RED to subscribe to the data stream. We will do any debugging of the service using the Node-RED debug panel.

  1. You should already have your Node-RED instance open from the last section. If not, open the Node-RED instance using the Visit App URL that you wrote down when you started the Node-RED engine earlier. Log in to your game app’s Node-RED instance using the user name and password you assigned yourself. screen capture of the log in for Node-RED
  2. To delete all existing nodes in the default flow, select them all, and then press the Backspace or Delete key. (Note: The keyboard shortcut CTRL-A doesn’t work to select all the nodes.) A blank canvas is then displayed.
  3. From the left palette, select and drag the IBM IoT input node (don’t use the output node) and the debug output node to the right pane. Connect them by selecting a connection circle on a node and dragging it to the other node’s connection circle. screen capture showing the connection between nodes
  4. Double-click the IBM IoT node.
  5. For Authentication, select IBM Cloud Service, and click Done. This tells the node to get the credentials to connect to the MQTT broker using Cloud Foundry credentials injection, which is a very handy feature because in IBM Cloud the application that you deployed is connected to the MQTT broker (that is, the Watson IoT Platform service).
  6. Click the Deploy button.
  7. On the right side of the panel, under the Deploy button, select the debug tab.
  8. On your smartphone, open the game app using the App name that you created in Section 1. In my case, it is discover-iot-sample-romeokienzler-1412.mybluemix.net/iot-phone
  9. Provide an 8-character alphanumeric name; for example, qwer1234.
  10. Provide an 8-character password that only contains alphabetic characters between a-z. screen capture showing the connected smartphone device In Node-RED on the debug tab, you should see debug output. screen capture of the debug tab

(Optional) Review the sensor data in the IBM Watson IoT Platform dashboard

Another way to see the accelerometer data is to look at a list of all game players.

  1. Open the IBM Watson IoT Platform. From your IBM Cloud Dashboard view, click the discover-iot-try-service, and then click Launch. screen capture of the Cloud Foundry Apps view
  2. From the left menu, select Devices. screen capture of the devices view
  3. Click a device, and then double-click a sensorData event to see more detail. screen capture showing example sensor data

Congratulations, we are almost done. You’ve verified that data can be streamed from a smartphone by using the MQTT message broker in Watson IoT Platform to our Node-RED app. The next step is to stream this data to a database and analyze it.


Set up a NoSQL database to store the data

When we play Harlem Shake we are sending IoT sensor data through an MQTT message broker in the cloud. We need to send it to a NoSQL database. Let’s add the database function.

  1. You should already have your Node-RED instance open from the last section. If not, open the Node-RED instance using the Visit App URL that you wrote down when you started the Node-RED engine earlier. Log in to your game app’s Node-RED instance using the user name and password that you assigned yourself.
  2. From the left panel, drag the function node in between the other two nodes, which will automatically connect it to those nodes. screen capture showing function between IBM IoT and msg.payload nodes
  3. We first have to flatten the hierarchical JSON messages to a relational scheme. Double-click the function node, and paste in the following JavaScript code.
    msg.payload =
    X : msg.payload.d.ax,
    Y : msg.payload.d.ax,
    Z : msg.payload.d.ax,
    SENSORID : msg.payload.d.id
    return msg;

    The final result should look like this. screen capture showing insertion of function code
  4. Click Done.
  5. Click Deploy. On the debug tab, the output should look similar to this. screen capture showing example debug results

Now that we’ve added the NoSQL function, we are prepared to store the data in Cloudant.


Stream the data to Cloudant using Node-RED

We’ll be using ApacheCouchDB powered by IBM Cloudant to stream the data to the NoSQL database function we just created. Cloudant is based on open-source ApacheCouchDB, and we will use it store our intermediate sensor data.

  1. From the left panel, in the storage area, choose the cloudant out storage connector (the cloud icon is on the right), and connect it to the output of the function node. screen capture showing Cloudant node added to the function
  2. Double-click the cloudant out node, and select your database Service. Note: There should only be one database service unless you’ve connected more than one Cloudant database to this application in the IBM Cloud console. (Again, the IBM Cloud here is making use of Cloud Foundry credential injection basically doing the job of configuring the database endpoint for you.)
  3. Name the database harlemshake.
  4. Select Only store msg.payload object. screen capture showing the edit of the cloudant out node
  5. Click Done.
  6. Click Deploy.

Now it’s time to check if data arrives in the table. Ensure the game app on your smartphone is still sending data by looking at the debug pane in Node-RED.


Analyze the data in a data science notebook

Everything should look good. The smartphone shakes, the data streams up to the cloud, and the database is holding the data. But who is keeping score? Something needs to analyze the data and announce the winner. We’ll be using IBM Watson Studio.

  1. Log in to IBM Watson Studio.
  2. On the New notebook dialog, click From URL, in the Name field add a name. As Runtime select Default Python 3.6 XS. Then, in the Notebook URL field, paste the following URL: https://raw.githubusercontent.com/romeokienzler/developerWorks/master/boomboomshakeshakesparkv2.ipynb
  3. Click Create Notebook.
  4. To create a new data source connection, click the Find and Add Data icon > Connections > project page. screen capture showing where the links are located
  5. Click Connections > Create Connection.
  6. In the Name field, specify a name, and then under Service Instance, select the database. Click Create. Note: Only one Cloudant database from your previously installed Node-RED boilerplate should be listed.
  7. Go back to your notebook and reload. You can now see the connection. Select the first empty cell in your notebook, and click Insert to code. The credentials have been created, and you can start running the notebook. screen capture showing the credentials have been created
  8. Click Cell > Run All.

After you and your friends have played the game, you can check to see who won. In your notebook, look for the spark.sql statement. It looks like “Seb” is the winner with the most energy input.

screen capture showing the results of the game

Understanding the data science behind the results

What the spark.sql statement does is to approximate the energy that your players generated over the run of the game by calculating the overall energy across all three dimensions of the accelerometer data.

On a single axis, the vibration data time series looks like the following graph:

graph of a single axis of vibration data

If you want to know the energy represented by this time series, you can approximate this value by taking the integral of the function.

graph showing when you approximate by taking the integral of the function

Remember that we got rid of the negative values and turned them into positive ones. To approximate the integral, we just sum up all the individual positive values.

graph showing results of getting rid of the negative values

Now that you’ve learned how to approximate the integral of a time series, let’s calculate the overall energy of all three dimensions (remember, we have three individual signals for each dimension of vibration) using the following formula:

formula of the sum of each measurement and taking the square root

We sum the square of each measurement and take the square root.

Note: To obtain real energy we would need to incorporate mass of the device too.

Using SQL, this calculation is represented by the following statement:

    id, // select ID of the device because we need to know who the winner is
    sqrt(sum(axax+ayay+az*az)) //calculate energy
  as energy  // give this new feature a name
    from unconfshake // specify temporary query table of ApachSparkSQL
    group by id // calculate on a per device basis
    order by energy desc // make top performers appear on top


You’ve done your part and built the game. Now it’s time to play.

  1. Find a friend or colleague with a smartphone. Give your friend the game app URL.
  2. They need to enter their own unique 8-digit alphanumeric ID and a unique 8-digit character alphabetic password (a-z).
  3. Ask your friend to shake the smartphone in all directions. This sends data to the cloud. You can verify this in Node-RED and in Watson Studio using the SQL query provided in the notebook you’ve imported. Just re-run the cell containing the SQL statement once in a while, and you’ll notice how the list of winners changes over time.
  4. You can check for the winner by clicking Cell > Run All in the data science notebook.

To play again, you have to complete the following steps:

  1. Rename the destination database (harlemshake) in Node-RED, and then update the name in the notebook.
  2. Open the IBM Watson IoT Platform again, select all devices, and then select Delete devices. screen capture showing where to select all devices and delete them

Note: You could delete the results in the database to play again, but it’s easier to rename the database and reset the connection.

This game can be left up and running so you can play it during your lunch breaks to stay awake after a heavy meal!