Previously, we were using TI SensorTag for sensor readings, however, as its possible that you may not have that particular sensor module, I have modified this tutorial to use your smartphone’s accelerometer readings. So, in this tutorial, we will read the accelerometer readings, publish them to the IoT platform, retrieve the published readings and then visualize them as a streaming line chart.
Step 1: Setting up IBM Bluemix account and Watson IoT platform
To get started, follow these steps:
- Register and/or login for an IBM Bluemix account.
- Once logged in, click on “Use Services or APIs”.
- You will see a huge list of services, click on “Internet of Things” checkbox from the left-hand pane and click on “Internet of Things Platform”.
- On the next screen, you will see a brief about Internet of Things Platform and a few options to get started, leave everything as it is and click on “Create”.
- On the next screen, you will notice that a new IoT Platform service has been created for you. Now just click on “Launch dashboard” button.
- You will be presented with an overview of your account. From here, click on “Add device” button under “Device Types” panel.
- You will be presented with a pop-up to add new device type. Note that these are your device types, as you want to declare them so you can name and describe them in any way you want. For the purpose of this tutorial, I am just using a device type called “Accelerometer”. Fill in all other optional inputs if you want them or just leave them and finish the process to create a new device type.
- As you finish, a new pop-up will start that will guide you to add your device by using the “device ID”. Choose any alphanumeric string to be your device ID.
- At the end of the process, you will see your organization ID, device type and device ID. Make note of these values as they will be used in our app.
- Now close the popup and click on “Access” option from the left-hand menu, then choose “api keys” from the sub-menu on the page that open. From here, click on “+ Generate API Key” button.
- On the next popup, you will see your “API Key” and “Authentication Token”, note these values.
By now, you have created an IBM account, registered a Watson IoT platform service, added a device and generated API key and authentication token to use them with our app. Now is the time we start developing the mobile app that will collect data from phone’s accelerometer and publish it to our new account at IBM Watson IoT platform.
Step 2: Developing the app using Evothings Studio
To get started, follow these steps:
- Download the latest version of Evothings Workbench.
- Download Evothings Viewer app on your mobile phone. (iOS, Android).
- Connect the Evothings Workbench with Evothings Viewer app.
- Click on “Examples” tab in the Evothings Workbench and run “Hello World” example to see the working of Evothings Studio.
Now you know Evothings Studio, let’s download the example app code for this tutorial and run it with Evothings Studio.
- Clone or download the example app code from this GitHub repository.
- Open the downloaded folder, find the “index.html” file and drag it to the “My Apps” tab in Evothings Workbench, a new project entry will be created.
- Open “app.js” file from the downloaded folder and input your Watson IoT platform service credentials.
- Run the example project from Evothings Workbench.
- Tilt the phone to see changing values of the accelerometer on Smoothie streaming chart. Note the slight delay between when you tilt the phone and when the lines start updating themselves, that is because we are first publishing the data to IoT Platform and then retrieving and plotting it on the app.
- Go back to the IBM Watson IoT Platform service dashboard and click on graph icon alongside the device listing to see the incoming events.
Step 3: Understanding the code
Open “index.html” from the downloaded app code in your favorite code editor. Note that upon initialization, the initialiseChart function plots and initialize Smoothie chart. After that, the accelerometer gets initialized, we start watching the accelerometer readings using watchAcceleration function from Device Motion Cordova plugin.
Once, we have the accelerometer readings in accelerometerHandler, we make a JSON string of them and send them for publishing to MQTT library.
Now, open “app.js” and note the app.onMessageArrived function, this is where we receive the data from IoT Platform as it get published then we parse the JSON and update the readings on our Smoothie chart.
If you want to discuss this tutorial or have any questions, feel free to use Evothings Forum where a large community is available to help you!
Get Evothings Studio from the download page (Workbench part) and mobile Viewer clients from Apple iTunes and Google Play (look for “evothings viewer”), and join in!