Quantcast
Viewing all articles
Browse latest Browse all 3384

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

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.
    Image may be NSFW.
    Clik here to view.
  2. Click on WEB and select SDK for Node.js. Click CONTINUE.
    Image may be NSFW.
    Clik here to view.
  3. Give a name for the app and click FINISH.
    Image may be NSFW.
    Clik here to view.
  4. On the top, you can see the status “Your app is staging”.
    Image may be NSFW.
    Clik here to view.
  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.
    Image may be NSFW.
    Clik here to view.
  7. In the services page, click on the MongoDB by Compose service in the Data and Analytics section.
    Image may be NSFW.
    Clik here to view.
  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.
    Image may be NSFW.
    Clik here to view.
  10. Once registered, login to Compose.io and create a MongoDB deployment.
  11. Using the default values, click Create Deployment.
    Image may be NSFW.
    Clik here to view.

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

    Image may be NSFW.
    Clik here to view.

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

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

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

    Image may be NSFW.
    Clik here to view.

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.

Image may be NSFW.
Clik here to view.

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


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>