Connect the Intel Edison Board to the IBM IoT Foundation

Published Date
14 - Dec - 2015
| Last Updated
15 - Dec - 2015
 
Connect the Intel Edison Board to the IBM IoT Foundation

This article talks about creating a Bluemix application, registering a device, setting triggers using Node RED 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.

new.png

4.  Select WEB.
5.  Select the Browse Boilerplates option, then click BROWSE BOILERPLATES.
6.  Select the Internet of Things Starter from the available boilerplates.

IOTF.png

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.

add-service.png

10.  You can see a list of services available. Type Internet of Things in the search bar present at the top of the page.

search-service.png

11.  Select the Internet of Things Service from the displayed list of services.

iotf-service.png

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.

iotf-running.png

Get the device ID of your Intel® Edison device

1. Create a folder named ibm-iotf.
2. Change to the directory ibm-iotf.
3. Install MQTT onto your board using putty. Run:

1$npm install mqtt

4. Install getmac node package, to get the mac address of the Edison device

1$npm install getmac

5. Create file index.js.

1$vi index.js

6. Open index.js file and create node reference variables for the modules mqtt and getmac.

var mqtt = require('mqtt');

var getmac= require('getmac');

7. 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);

});

<span>Note: We are using the MAC address as device ID.</span>

8. Save the file and run index.js.

1$node index.js

9. 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.

iotf-launch.png

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.

iotf-add.png

5.  A window pops up with Add Device as title. Click Create device type to define a type for your Edison device.

iotf-type.png

6.  Type edison for the name and click Next located at the bottom right of the page.

iotf-next.png

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.

iotf-id.png

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.

iotf-info.png

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.
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.

Screen Shot 2015-10-12 at 12.21.31 AM.png

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";
2. 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);

});
3. On receive, log the message to console.

client.on('message', function(topic, message) {

     console.log(JSON.parse(message));

});

For more such intel IoT resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/articles/connect-to-ibm-iot-foundation