Contents


Create a fun, simple IoT accelerometer game

Learn how to capture and send sensor data to the cloud using Watson IoT Platform and then analyze that data using Data Science Experience

Comments

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 (IBM Bluemix) 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 Data Science Experience, 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

  • A Bluemix account. (You can request a free trial here). For the time being, you cannot use a freemium account for this app.
  • A smartphone. Or, one or more friends with smartphones to play the game with you.
  • A Data Science Experience account.
    1. Open http://datascience.ibm.com/
    2. Click Sign up.
    3. Click Sign in with your IBM ID.
    4. Click Sign up for DSX.
    5. On the Create Organization and Space page, click Continue.
    6. Wait for the environment initialization, and when available, click Get Started.
1

Deploy the game application using one-click deployment

After you have a Bluemix 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.

Note: With a standard freemium account, you can't use the auto-deployment option because you can't access the build pipeline services.

  1. Log in to your Bluemix account.
  2. Go to my discover-iot-sample repo in GitHub, and in the README instructions, click the Deploy to Bluemix button.screen capture of the github readme deploy to bluemix button
    screen capture of the github readme deploy to bluemix button
  3. In Bluemix, click Deploy.
    screen capture of the Bluemix Deploy                         button
    screen capture of the Bluemix Deploy button

    While the application is being created, a status window is displayed. After the app is created, the View app button is enabled.screen capture of the status window

    screen capture of the status window

    Note: If the View app button is disabled, you can click Delivery Pipeline to see the progress of the deployment, which could take several minutes.

    screen capture of the delivery pipeline progress
    screen capture of the delivery pipeline progress
  4. If you opened the Delivery Pipeline, click the discover-iot-sample toolchain. Then, click the View app button.

    The link you need to play the game on a smartphone is displayed. 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 link
    screen capture of the link

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

2

Replace the Internet of Things Platform service

After you deploy the game app, you need a way to get the accelerometer data to the cloud. To get the data to the cloud, you'll use Node-RED, which is an open-source graphical flow editor. You use Node-RED to subscribe to the MQTT data that is coming from the smartphone and being stored to the database in the cloud. However, before we can get to Node-RED, we need to replace the default platform service with the service that I've included as part of the app that we've just deployed.

2a

Delete the default IoT Platform service

The Internet of Things Platform Starter boilerplate contains a Node-RED engine that you will use to process IoT messages. First, you need to remove the default Watson IoT service from the starter.

  1. You should already be logged in to your Bluemix account. If not, log in.
  2. Go to the Internet of Things Platform Starter in the Bluemix catalog. (Alternatively, you can navigate to this Starter by selecting Catalog > Boilerplates > Internet of Things Platform Starter.)
  3. In the App name field, enter a unique name for your app, such as myharlemshake, and then click Create.
  4. Wait until the status changes from Starting to Running. screen capture of the starting status
    screen capture of the starting status
    screen capture of the running status
    screen capture of the running status
  5. From the menu on the left, click Connections. screen capture of the Connections menu
    screen capture of the Connections menu
  6. For the default Internet of Things Platform service, click the menu in the upper right corner, and then click Unbind service .
  7. In the pop-up message that displays, select Delete this service instance, and then click OK.
  8. When prompted to restage your app, click Cancel.
2b

Connect to the deployed game app’s IoT service

Now that you've deleted the default iotf-service-free service, you need to create a connection to the deployed game application's IoT service and set the correct security level.

  1. Click the Connect existing button.
  2. Select discover-iot-try-service, and then click Connect.
  3. When you are prompted to restage your app, click Restage.
  4. Wait until the status changes to Running.
  5. Click discover-iot-try-service, and then click Launch. This starts IBM Watson IoT Platform in the IBM Cloud.
  6. Hover over the column of icons on the left. From the left menu, select Security.
  7. Edit Connection Security.
  8. In the Default Rule section, under Security Level, select TLS Optional, and then click Refresh compliance.
  9. At the top of the window, in the upper right, click Save .
3

Ensure the MQTT message broker can receive data

Now that we've successfully connected to the IoT service that I provided in the game app, we need to use Node-RED to store the sensor (accelerometer) data in the cloud.

3a

Secure the Node-RED flow editor

Before you can open Node-RED, you must configure a user name and password for accessing Node-RED flows in your app.

  1. You need to return to the Cloud Foundry Apps page for your game app. In your browser, locate the Service Details tab. From the hamburger menu, select Cloud Foundry Apps.screen capture of the hamburger menu highlighted
    screen capture of the hamburger menu highlighted
  2. From the table, select the name of your game app.
  3. From the left menu, select Connections. screen capture of the left menu
    screen capture of the left menu
  4. To the right of the status, click Visit App URL. The URL should be your app name followed by mybluemix.net (for example, https://myharlemshake.mybluemix.net).

    Note: Write down this URL because you can use it to open your Node-RED instance.

  5. To secure your Node-RED instance, click Next.
  6. Choose a user name and password. Make a note of it. Click Next.
  7. Click Finish.
  8. Click Go to your Node-RED flow editor.screen capture of the go to your Node-RED flow editor
    screen capture of the go to your Node-RED flow editor
  9. Enter your user name and password to log in to Node-RED.

Now you have everything in place to capture and store the data. You have deployed your game app, and you've created a connection between your game app and the message broker. The communication channel is working, but you need a place to store the data you will be sending.

3b

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 in Section 3a. 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
    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
    screen capture showing the connection between nodes
  4. Double-click the IBM IoT node.
  5. For Authentication, select Bluemix 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 Bluemix 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

3c

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 Cloud Foundry Apps view, click the discover-iot-try-service, and then click Launch.screen capture of the Cloud Foundry Apps view
    screen capture of the Cloud Foundry Apps view
  2. From the left menu, select Devices.screen capture of the devices view
    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
    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.

4

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 in step 3a. 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
    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
    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
    screen capture showing example debug results

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

5

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
    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 Bluemix 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
    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.

6

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 the Data Science Experience.

  1. Log in to datascience.ibm.com.
  2. In the top right, click Create new > Notebook. screen capture showing the data science experience dashboard
    screen capture showing the data science experience dashboard
  3. Click From URL, in the Name field add a name, and in the Notebook URL field, paste the following URL:
    https://raw.githubusercontent.com/romeokienzler/developerWorks/master/boomboomshakeshakesparkv2.ipynb
  4. Click Create Notebook.
  5. 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
    screen capture showing where the links are located
  6. Click Connections > Create Connection.
  7. 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.

  8. 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
    screen capture showing the credentials have been created
  9. 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

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

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

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

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

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:

select 
	id, // select ID of the device because we need to know who the winner is
	sqrt(sum(ax*ax+ay*ay+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

Conclusion

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 Data Science Experience 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
    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!


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Internet of Things, Big data and analytics
ArticleID=1048987
ArticleTitle=Create a fun, simple IoT accelerometer game
publish-date=08282017