Building an IoT App on IBM Bluemix
Building an IoT App on IBM Bluemix
A step-by-step guide to using Bluemix's IoTF service to develop an IoT application.
Join the DZone community and get the full member experience.Join For Free
Digi-Key Electronics’ Internet of Things (IoT) Resource Center Inspires the Future: Read More
IBM Bluemix is an open-standard, cloud platform to build, manage and run different types of applications for mobile, web and smart devices. It makes the delivery of an app ready to use by providing services. It also provides runtime environments for java, Node.js, Ruby, Go etc., thereby giving control and flexibility to the developers.
This blog will demonstrate the various steps involved in developing an IoT application using the IBM services and deploying it on the Bluemix cloud platform. IBM Bluemix offers a wide range of services for developing apps related to Internet of Things, Mobile, DevOps, Data and Analytics etc. This blog focuses on implementing the Internet of Things Foundation (IoTF) service provided by Bluemix for developing IoT applications.
Raspbian Wheezy OS, java, eclipse, python
Raspberry Pi Kit with Wi-Fi adaptor
Building an IoT application Using the Bluemix IoTF Service
Let’s build an IoT application and register the Raspberry Pi to the Bluemix service for communication
Building an IoT application using Bluemix mainly involves 4 steps mentioned below:
- Create an app and bind IoTF service
- Register Device to IoTF service
- Device Programming
- Application Programming
Create an app and Bind IoTF Service
- Sign in to Bluemix
- Click on Create App and choose the app template as Web
- Choose the starter SDK for Node.js and click Continue to name the app
- Click on Finish to start the app
- On App Overview tab click on Add a service and select Internet of Things Foundation Service
- Click on Create and the service gets bind to your app
As the IoT application is created and service binding is done, here comes device registration with IoTF.
Register Raspberry Pi to IoTF
- Click on Launch dashboard in IoT Foundation service and this will generate an organization ID which is used for unique identification
- In Devices Tab click on Add device
- Click on create device type, provide the name and click Next
- Now choose the device type and click Next
- Give device id and click Next
- Click Next to auto generate the authentication token
- Save the following details Organization Id, device type, device id, authentication token
- In Access tab click on API keys
- Click on Generate API key and save the generated key and token associated with it
After registering, Raspberry pi needs to be configured accordingly in order to use the IoTF services and is as follows
Prior to the device programming, first setup the Raspberry pi
Setting Up Raspberry Pi
To make the Raspberry Pi up and running follow the below mentioned steps:
- Install Raspbian Wheezy OS on Raspberry Pi.
- Install java (recommended versions java 7 or later) using the following command: sudo apt-get install oracle-java7-jdk
- Install python(recommended versions are python 3.5, 3.4, 2.7) using the following command: sudo apt-get install python-dev
- Install eclipse using the following command: sudo apt-get install eclipse
With these installations Raspberry Pi is ready to use.
As devices are uniquely identified by IoT Foundation (IoTF) with dedicated authentication Token, it has to be registered before connecting to IoTF.
Connecting Device to IoTFoundation Service
- For device configuration, download the sample code from here: https://hub.jazz.net/project/chunbintang/bluemixmqtt/overview
- After extracting the contents search for MyData folder and update the device.conf file with registered device credentials saved earlier.
- DeviceTest.java file in source folder com.ibm.bluemixmqtt reads device.conf file and sets MQTT connection with the properties mentioned.
- The device access point for IoT Foundation service is <orgid>.messaging.internetofthings.ibmcloud.com
where orgid will be auto-generated when the IoT service is created.
- The client-id property which is used for MQTT connection should be in the form of d:<orgid>:<typeid>:<deviceid>
where d identifies client as device orgid is a 6 character auto generated alphanumeric during service sign up typeid is an identifier of the type of device connecting deviceid is a unique name given to the device connecting where the above ids are copied earlier when device is added.
- The auth-method property should be set to “token”.
- The authtoken property should be set to the token copied previously.
- The format to publish events is iot-2/evt/<event-id>/fmt/<format>event-id, used to categorize different event types format is given as json,e.g., "iot-2/evt/" + MqttUtil.DEFAULT_EVENT_ID+ "/fmt/json",The message encoded in json contains a single top-level property as “d”.
- The format to subscribe for the events is iot-2/cmd/<cmd-type>/fmt/<format-id>cmd-type, used to categorize different command types Using “+” wildcard, one can subscribe to various types of commands format can be set as json,
e.g., "iot-2/cmd/" + MqttUtil.DEFAULT_CMD_ID + "/fmt/json".
- After receiving command event, callback function messageArrived is executed.
This device configuration makes Raspberry Pi ready to publish the messages.
Let’s see how the application subscribes to the messages published by the Raspberry Pi.
Applications are identified by IoTF with a unique application ID and API key.
This blog focusses on developing a Node.js application using IBM runtime SDK for Node.js. Bluemix provides a sample Node.js starter application. It facilitates the integration of code with Git by using DevOps services. This specific application displays the count value pushed by device to the cloud.
Set Up GIT Integration Using DevOps Services
- In Bluemix dashboard’s app overview click on “ADD GIT”.
- Check the box that says “Populate the repository with the starter application package and enable build and deploy” to create Git repository with example code and to deploy the app.
- Click EDIT CODE to open the project in the web Integrated Development Environment(IDE).
- To make a change to a file, find the file to modify and click on it to edit in the editor.
Visualize the Data Posted to Bluemix Cloud
- Download the source code from the following link: https://hub.jazz.net/project/demos/nodeproject1/overview
- In app.js specify the API Key and authentication token in util.iot_httpCall() copied earlier.
- Go to views folder and select index.jade to edit.
- Replace first ajax call and jsonurl with the following format: https://<app name>.mybluemix.net/api/v0001/historian/<orgid>/<typeid>/<deviceid>?Where app name is the name given during creation, orgid, typeid, and deviceid are copied earlier.
- The first ajax call in index.jade displays the recent event i.e., last count value sent by the device whereas the second ajax call collect the recent 25 events and display them using graph.
- Run the DeviceTest.java file in Raspberry pi. When it gets successfully connected to the IoT service, a confirmation message will be displayed as follows: connected to tcp://obmn2p.messaging.internetofthings.ibmcloud.com:1883
- Run the web app created in Bluemix over http protocol with “/Count” at the end of app URL because we customized it in app.js file.
- Then a graph showing the count value will be displayed.
Opinions expressed by DZone contributors are their own.