Storing event data in a secure and stable cloud is a necessity for many IoT deployments. Imagine having a network of devices that are concurrently collecting ambient temperature, humidity and light levels at an industrial plant. All of these devices will generate events that are required to be captured and stored. Typically, this network of devices would need one or a couple gateways for them to talk to the cloud hosting provider.
In this tutorial, we will demonstrate how to collect sensorial event data using our old-time favorite the Texas Instrument CC2650 SensorTag device; using a HTML5-based mobile app as gateway and subsequently publishing its data to the IBM Watson IoT platform. This set-up provides you as a developer with all the necessary building blogs for a dedicated set of tools to work with Internet of Things. The IoT platform is a part of IBM Bluemix cloud services.
Setting up IBM Bluemix account and Watson IoT platform
To get started, follow these steps:
- Register and/or login for a free IBM Bluemix account.
- Once logged in, click on “Use Services or APIs”. You’ll now see an extensive list of services, scroll down and click on “Internet of Things” checkbox from the left-hand pane and click on “Internet of Things Platform”.
(direct link here too, in case you got lost: https://console.ng.bluemix.net/catalog/services/internet-of-things-platform) - On the next screen, you’ll find a brief about Internet of Things Platform in general and a few options on how to get started. For this demo; just leave everything as it is and click on “Create”.
Leave all options and click “Create”
- After a short set-up time, on the next screen, you will notice that a new IoT Platform service has been created for you. Now just click on the “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 defined a device type called “CC2650”, you can call it anything you please, I guess. 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. Also the authentication token is good to have jotted down somewhere (you can always to back any time and look too).
E.g. Org ID “x******”, Device Type “CC2650” and device id “1001”
The organisation ID is generated for us, while the other two you make up yourself. - 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.
Congratulations, you’ve now done all of 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 really time we start developing the mobile app that will collect data from TI SensorTag publish it to our new account at IBM Watson IoT platform.
Developing the app with Evothings Studio
We will be using Evothings studio to rapidly prototype our app. Evothings studio is built with Cordova and allows quick development through the auto-reload mechanism. It’s a robust platform that allows you to develop apps for your IoT devices using HTML, CSS and JavaScript.
To get started with Evothings Studio, 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.
https://github.com/hammadtq/Evothings-Demo-Apps/tree/master/bluemix-iot-cloud-CC2650 - Open the downloaded folder, find the “index.html” file and drag-drop it onto the “My Apps” tab in Evothings Workbench, a new project entry link will be created.
- Open the “app.js” file from the downloaded folder and input your Watson IoT platform service credentials you saved previously.
- Run the example project from Evothings Workbench by pressing “Run”.
- Power up your TI SensorTag (hold down the side button until it starts blinking) and press the “Connect” button in the app on your phone. The app will now scan and connect (in this demo to the first SensorTag it finds), effectively filtering off other device types. The app is currently configured to upload the ambient temperature, humidity and light readings as an event data to the IoT platform every 6 seconds. You can change all these parameters relatively easy.
- Open the IoT platform in your browser, click on the graph icon alongside the device listing and check if it’s registering incoming events.
Code Explanation
The example app code is using MQTT as the transfer protocol instead of HTTP. MQTT is a specialized protocol designed specifically to facilitate IoT device data transfers. We are using MQTT API of IBM Watson IoT platform. You can read more about MQTT terms such as “publish”, “topic” and “subscribe” here.
To work with MQTT we are using Paho JavaScript client library. All of MQTT implementation is done in “app.js”. We connect to the IoT platform using “app.setupConnection” function, once established the Paho library holds the connection, waiting for you to either publish or retrieve data from the cloud. When we have event data to publish, we send it to “app.publish” function as a JSON string from index.html which in turn publishes the data to the IBM Watson IoT platform.