Tutorial
By Romeo Kienzler | Updated March 14, 2018 - Published August 28, 2017
AnalyticsIoT
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:
Obviously, all the smartphones will act as the sensors, streaming the data into our IoT app.
You can build the game in six steps:
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.
discover-iot-sample
Git Repository
While you can now send accelerometer data to the cloud, it won’t get stored. Next, we need to set up data storage.
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.
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.
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.
iotf-service-free
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.
Before you can open Node-RED, you must configure a user name and password for accessing Node-RED flows in your app.
mybluemix.net
https://myharlemshake.mybluemix.net
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.
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.
discover-iot-sample-romeokienzler-1412.mybluemix.net/iot-phone
Another way to see the accelerometer data is to look at a list of all game players.
sensorData
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.
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.
msg.payload = { X : msg.payload.d.ax, Y : msg.payload.d.ax, Z : msg.payload.d.ax, SENSORID : msg.payload.d.id } return msg;
Now that we’ve added the NoSQL function, we are prepared to store the data in Cloudant.
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.
harlemshake
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.
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.
https://raw.githubusercontent.com/romeokienzler/developerWorks/master/boomboomshakeshakesparkv2.ipynb
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.
spark.sql
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:
If you want to know the energy represented by this time series, you can approximate this value 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.
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:
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(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.
To play again, you have to complete the following steps:
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!
IoTNode-RED+
This article provides a technical introduction to the MQTT protocol. You learn what is MQTT, what makes MQTT suitable for…
IoTMessaging+
Back to top