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
- Log in to Bluemix console, select DASHBOARD and click CREATE APP.
- Click on WEB and select SDK for Node.js. Click CONTINUE.
- Give a name for the app and click FINISH.
- On the top, you can see the status “Your app is staging”.
- Once the staging is done, click on overview on the left panel to view the dashboard.
- Now from the application dashboard click ADD A SERVICE OR API.
- In the services page, click on the MongoDB by Compose service in the Data and Analytics section.
- On the right side, you can see options to enter values for Username, Password, Host and Port.
- If you don’t have an account with Compose, you may need to create one. Click Register at Compose.
- Once registered, login to Compose.io and create a MongoDB deployment.
- 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.
- Create a database by clicking Add Database at the top right corner. Give a name for the database and click Run.
- Add a user for the database in order to gain access to the database using connection string.
- Click on Admin Settings to obtain the hostname and port details.
- On your Bluemix add service page, enter the details for hostname, port, username and password.
- 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:
Example:
var uri = mongodb:
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