This article talks about creating a Bluemix application, registering a device, setting triggers using NodeRED flow editor and visualizing data using Rickshaw JS.
Create a Bluemix application with IoTF Starter boilerplate
1. Log in to the Bluemix console. Visit console.ng.bluemix.net and select LOG IN.
2. After logging in, you will be in the DASHBOARD view.
3. Click CREATE APP.
4. Select WEB.
5. Select the Browse Boilerplates option, then click BROWSE BOILERPLATES.
6. Select the Internet of Things Starter from the available boilerplates.
7. Name your app…, then click CREATE.
8. Wait for your application to finish staging. A message indicating that "Your app is running" will be displayed when done
9. Once your application has been created by IBM Cloud, from your application's dashboard, locate and click ADD A SERVICE OR API.
10. You can see a list of services available. Type Internet of Things in the search bar present at the top of the page.
11. Select the Internet of Things Service from the displayed list of services.
12. A window pops up with an auto-generated name. You can keep it or rename it as per your requirement.
13. Click CREATE.
14. After the service has been created, a popup window requesting to restage your application will appear. Click RESTAGE.
15. Wait for your application to finish staging. A message indicating that "Your app is running" will be displayed when done.
Get the device ID of your Intel® Edison device
Create a folder named ibm-iotf.
Change to the directory ibm-iotf.
Install MQTT onto your board using putty. Run:
$npm install mqtt
Install getmac node package, to get the mac address of the Edison device
$npm install getmac
Create file index.js.
$vi index.js
Open index.js file and create node reference variables for the modules mqtt and getmac.
var mqtt = require('mqtt'); var getmac= require('getmac');
- Print the MAC address using the getMac function provided by the getmac module.
getmac.getMac(function(err, macAddress) { if (err) throw err; var deviceId = macAddress.toString().replace(/:/g, '').toLowerCase(); console.log("Device ID: " + deviceId); });Note: We are using the MAC address as device ID.
- Save the file and run index.js.
$node index.js
- Copy the device ID/ MAC address printed on the console.
Register your Intel® Edison device
You need to register your device in order to communicate with the Bluemix cloud.
1. Go to console.ng.bluemix.net and open the created application from left panel.
From your application's dashboard, locate and click the Internet of Things service.
2. You will be redirected to the Internet of Things Foundation homepage which would look like
3. Click Launch Dashboard. The dashboard opens in a new page.
4. Locate and click + Add a device.
5. A window pops up with Add Device as title. Click Create device type to define a type for your Edison device.
6. Type edison for the name and click Next located at the bottom right of the page.
7. Click Next in the following pages until you see Create Device Type. Click Create.
8. You will be returned to the Add Device page. Click Next.
9. Copy the device-id / mac-address you got in the previous step and paste in the Device ID field.
10. Click Next until you see Add Device. Click Add.
11. Save your device credentials into a file as config.json, which you will be using to send data to the cloud.
Send Data to IBM Cloud
1. Make sure you have the file config.json in the folder ibm-iotf along with the index.js file. It should be in the below format with the values you got in the previous step.
org - organization name
port - port number (1883 fixed)
type - device type
id - device id
auth-method - Authentication type
password - Authentication token
username - Username (use-token-auth)
2. Open the index.js file.
3. Create a variable host to store the hostname.
var host = config.org + ".messaging.internetofthings.ibmcloud.com";
4. Create a variable clientId to store the client ID which is a combination of organization, device type and device ID.
var clientId = "d:" + config.org + ":" + config.type + ":" + config.id;
5. Create a variable topic_pub to store the topic to publish the data.
var topic_pub = "iot-2/evt/status/fmt/json"; status - event type json - data format
6. Create a variable client by calling the mqtt API mqtt.connect with the required parameters.
var client = mqtt.connect( { host: host, port: config.port, username: config.username, password : config.password, clientId : clientId });
7. Create a function sendMessage to publish data to IBM Cloud.
function sendMessage(){ // Generates a random value, you can use the actual sensor value. var value = Math.floor((Math.random() * 30) + 60); var message = {"d" : {"value" : value, } }; client.publish(topic_pub, JSON.stringify(message)); }
8. Call the above function sendMessage periodically using JavaScript built-in function setInterval when the connection is established with the IBM Cloud.
client.on('connect', function () { console.log('Connected to IBM'); setInterval(sendMessage, 1000); });
9. Run the application.
$node index.js
10. Go to console.ng.bluemix.net and launch the Internet of Things dashboard. Please refer Register your Edison Device section on how to launch the dashboard.
11. On the dashboard, go to DEVICES tab and click on your device.
12. A popup window gets opened and you can view the messages being received as events.
Create API Key
IBM provides an interface to respond to the real-time events or data using NODE-RED flow editor. To implement a Node-RED application that processes real-time events from devices in your IoT organization, you need to generate an API Key to access the data generated by the device.
1. From your IoT organization dashboard go to ACCESS tab and click API Keys.
2. Click Generate API Key.
3. Copy and save the access key which will be used in the NODE-RED flow editor to access the date.
Note: You can only view it once but you can create multiple keys.
Create Triggers
1. From your application's dashboard, locate and click the route.
2. Go to the Node-RED bluemix flow editor by clicking the red button.
3. Double-click on IBM IoT App In and change Authentication to API Key.
4. Click the edit icon next to the API Key.
5. In the popup window, enter Name as API. Enter API Key and Token obtained from the Generate API Key section. Click Add.
6. Enter values for Device Type, Device Id and Format leaving the field Event. Click Ok.
8. Double-click on the temp threshold and change the value from 40 to 70.
9. Drag and drop the ibmiot output node from left pane.
10. Double-click on the output node. In the edit popup window, select the same API key used for the input node, select Output Type as Device Command and fill the rest of the fields. Click OK.
11. Now connect this ibm output node to the danger template.
12. Double-click on danger template, change it to JSON and update the template.
13. Click Deploy on the top-right corner.
14. If there are no errors, you can either select device data or cpu status nodes to view the messages on debug console.
Subscribe to triggers
Open index.js file and create a variable topic_sub to store the topic to subscribe to.
var topic_sub = "iot-2/cmd/trigger/fmt/json";
Subscribe to the topic on connection with IBM. Add the subscribe function inside on connect.
client.on('connect', function () { client.subscribe(topic_sub); console.log('Connected to IBM'); setInterval(sendMessage, 1000); });
On receive, log the message to console.
client.on('message', function(topic, message) { console.log(JSON.parse(message)); });
Additional resources
IBM Cloud documentation - https://www.ng.bluemix.net/docs/
IBM IoTF documentation - https://docs.internetofthings.ibmcloud.com/
IBM Bluemix IoT starter application to visualize data - https://www.ng.bluemix.net/docs/services/IoT/index-gentopic1.html#iot200
IoT Foundation Recipes - https://developer.ibm.com/iotfoundation/
MQTT - http://mqtt.org/
Node-RED - http://nodered.org/