Click here to Skip to main content
14,176,451 members
Click here to Skip to main content
Add your own
alternative version

Stats

4.9K views
1 bookmarked
Posted 20 Jun 2016
Licenced CPOL

Enabling IBM Bluemix on Intel® Edison Boards using MongoDB by Compose

, 20 Jun 2016
Rate this:
Please Sign up or sign in to vote.
This article explains how to establish a connection with IBM Bluemix cloud services using Node.js API.

This article is 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 explains how to establish a connection with IBM* Bluemix* cloud services using Node.js* API. This include creating a Bluemix application, adding a mongoDB* connection, as well as storing and retrieving data.

Create a Bluemix application

  1. Log in to Bluemix console, select DASHBOARD and click CREATE APP.
  2. Click on WEB and select SDK for Node.js. Click CONTINUE.
  3. Give a name for the app and click FINISH.
  4. On the top, you can see the status “Your app is staging”.
  5. Once the staging is done, click on overview on the left panel to view the dashboard.
  6. Now from the application dashboard click ADD A SERVICE OR API.
  7. In the services page, click on the MongoDB by Compose service in the Data and Analytics section.
  8. On the right side, you can see options to enter values for Username, Password, Host and Port.
  9. If you don’t have an account with Compose, you may need to create one. Click Register at Compose.
  10. Once registered, login to Compose.io and create a MongoDB deployment.
  11. Using the default values, click Create Deployment.

    It will take few minutes for the deployment to be created; you can see the status as below.

    Once the deployment is finished, you will be redirected to the getting started page where you can create a database.

  12. Create a database by clicking Add Database at the top right corner. Give a name for the database and click Run.
  13. Add a user for the database in order to gain access to the database using connection string.
  14. Click on Admin Settings to obtain the hostname and port details.
  15. On your Bluemix add service page, enter the details for hostname, port, username and password.
  16. Once you click create, click RESTAGE on the popup window that appears.

    After the restaging is finished, you should see a status that reads "Your app is running." In the top-right corner.

Setting up the Development Environment

Install mongodb npm module into your project.

npm install mongodb

Setup mongodb connection

Create a node reference variable for the module and client object for establishing a database connection.

var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient

Create a mongodb connection

The connect function returns a db object, which contains the collection object. The collection object is used to insert and retrieve data from cloud.

The connection url can be obtained from Bluemix console. Select the MongoDB by Compose Service from application dashboard and click Show Credentials.

You can create the connection uri using these credentials. Form the uri as shown below, to be used in the node application:

mongodb://<user>:<password>@<uri>:<port>/iot-compose?ssl=true

Example:

var uri = mongodb://iot-kona:intel123@aws-us-east-1-portal.11.dblayer.com:27832/iot-compose?ssl=true

Copy the uri under credentials and pass it to connect function

db = MongoClient.connect(uri, function(err, db) {});

Store Data

Data can be stored as JSON objects or an array of JSON objects.

data = {‘sensor-id’ : ‘sens341’, ‘value’ : 65.5}
db = MongoClient.connect(config.url, function(err, db) {
	collection = db.collection(config.db);
        collection.insert(data, function(err, result) {});
});

Query Data

Timestamp based query

dataQuery = { "timestamp": { $gt: readQuery.timestamp } };

Sensorid based query

dataQuery = { "sensor_id": { $eq: readQuery.sensor_id } }
Run query
collection = db.collection(self.config.db);
collection.find(dataQuery).toArray( function(err, items) {
if(!err) 
console.log(JSON.stringify(items, null, '  '));
});

References

License

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

Share

About the Author

Intel Corporation
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.
Group type: Organisation

43 members


You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190525.1 | Last Updated 20 Jun 2016
Article Copyright 2016 by Intel Corporation
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid