Click here to Skip to main content
15,885,985 members
Articles / Internet of Things
Article

Connect the Intel® Edison Board to IBM IoT Foundation

13 Nov 2015CPOL6 min read 20.7K  
This article talks about creating a Bluemix application, registering a device, setting triggers using NodeRED flow editor and visualizing data using Rickshaw JS.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

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.

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

Image 2

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.

Image 3

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

Image 4

12. A window pops up with an auto-generated name. You can keep it or rename it as per your requirement.

Image 5

13. Click CREATE.

14. After the service has been created, a popup window requesting to restage your application will appear. Click RESTAGE.

Image 6

15. Wait for your application to finish staging. A message indicating that "Your app is running" will be displayed when done.

Image 7

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:

    $npm install mqtt
  4. Install getmac node package, to get the mac address of the Edison device

    $npm install getmac
  5. Create file index.js.

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

    JavaScript
    var mqtt = require('mqtt');
    var getmac= require('getmac');
  7. Print the MAC address using the getMac function provided by the getmac module.
    JavaScript
    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.
  8. Save the file and run index.js.
    $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.

1055156/iotf-launch.png

2. You will be redirected to the Internet of Things Foundation homepage which would look like

1055156/iotf-page.png

3. Click Launch Dashboard. The dashboard opens in a new page.

4. Locate and click + Add a device.

Image 10

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

Image 11

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

Image 12

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.

Image 13

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.

Image 14

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.

Image 15

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.

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

JavaScript
var clientId = "d:" + config.org + ":" + config.type + ":" + config.id;

5. Create a variable topic_pub to store the topic to publish the data.

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

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

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

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

Image 16

12. A popup window gets opened and you can view the messages being received as events.

Image 17

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.

Image 18

2. Click Generate API Key.

Image 19

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.

Image 20

Create Triggers

1. From your application's dashboard, locate and click the route.

Image 21

2. Go to the Node-RED bluemix flow editor by clicking the red button.

Image 22

3. Double-click on IBM IoT App In and change Authentication to API Key.

Image 23

4. Click the edit icon next to the API Key.

Image 24

5. In the popup window, enter Name as API. Enter API Key and Token obtained from the Generate API Key section. Click Add.

Image 25

6. Enter values for Device Type, Device Id and Format leaving the field Event. Click Ok.

Image 26

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.

Image 27

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.

Image 28

11. Now connect this ibm output node to the danger template.

Image 29

12. Double-click on danger template, change it to JSON and update the template.

Image 30

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

  1. Open index.js file and create a variable topic_sub to store the topic to subscribe to.

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

    JavaScript
    client.on('connect', function () {
       client.subscribe(topic_sub);    
       console.log('Connected to IBM');
       setInterval(sendMessage, 1000);
    });
  3. On receive, log the message to console.

    JavaScript
    client.on('message', function(topic, message) {
         console.log(JSON.parse(message));
    });

Additional resources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
This is a Organisation

42 members

Comments and Discussions

 
-- There are no messages in this forum --