Category Archives: bluemix

Using MQTT to publish data and send commands for IoT

MQTT is a publish-subscribe messaging pattern with a light-weight footprint, ideal for IoT devices. This tutorial will create a basic workflow via an MQTT Broker from device client to application server and back. To avoid any hardware dependencies in this tutorial I will simulate the device client and implement it as a Node.js server. The application server will also be a Node.js server.

  1. Requirements
  2. Setup an MQTT Broker
  3. Register a Device on the MQTT Broker
  4. Create an API Key on the MQTT Broker
  5. Create the IoT Device Client
  6. Create the IoT Application Server
  7. Publish Data from Client to Application via Broker
  8. Send Commands from Application to Client via Broker

Requirements

Setup an MQTT Broker

The publish-subscribe messaging pattern of MQTT requires a message broker. One easy way to create one is to create an MQTT broker from a boilerplate application ‘Internet of Things Platform Starter‘ in Bluemix.
Continue reading

QAVideos (2) – Add Custom Model and ORM to Node.js

(updated: 20170318)
This is part 2 in a series to build QAVideos. QAVideos is a Question and Answer application that uses videos instead of text. Think of QAVideos as StackOverflow meets Youtube. QAVideos will use LoopBack, API Connect, Open API Initiative (OAI formerly Swagger.io), OpenWhisk, Object Storage.

In part 1 ‘QAVideos (Part 1), Adding User Management to Node.js with API Connect‘, I showed how to add User Management to a Node.js app using API Connect.
In this part 2, I will add a custom data model, i.e. a Video model and use ORM to persist data to a PostGreSQL database.
Part 3 (to be updated) is found here, which adds model extensions and uses Open API Initiative (formerly Swagger) support.

Source code for QAVideos is released per part via https://github.com/remkohdev/qavideos/releases.

Requirements

  • Install Node.js and npm,
  • Install API Connect.
  • Check if the ‘apic’ tool is installed, by running ‘apic -v’ from the commandline. If not installed, follow the installation instructions, here.
  • Get the source code for part 1 of this tutorial and follow the installation instructions for QAVideos Release v1.0.0. First clone the repository, then make sure you fetch all the remote tags to your local repository, and then checkout the v1.0.0 tag.
    git clone https://github.com/remkohdev/qavideos.git
    git fetch --all --tags --prune
    git checkout tags/v1.0.0

Table of Contents

  1. Create Data Model
  2. Define Relation
  3. Adding ACL
  4. Add Video Functionality
  5. Add Data Source

1. Create Data Model

First, test if QAVideos (part 1) is running correctly. Open a commandline, change your directory to the root directory of your QAVideos application, type ‘apic start’, and browse to ‘http://0.0.0.0:4001‘ in your browser.

Now, I want to add a custom model ‘Video’ so that users can manage their list of videos. To do this, I create a model for the Video with the ‘apic’ tool, and define the relationship between Video and User (a User can own many videos), and specify the access level of Users to Video objects using an Access Control List (ACL).
Continue reading

QAVideos (1) – Adding User Management to Node.js

(updated: 2017-03-14)
QAVideos is a Question and Answer application that uses videos instead of text. Think of QAVideos as StackOverflow meets Youtube. QAVideos will use LoopBack, API Connect, Open API Initiative (OAI formerly Swagger.io), OpenWhisk, Object Storage.

Requirements:

  1. QAVideos (1) – Adding User Management to Node.js. Part 1 creates the ‘StrongLoop’ application with built-in User and Role management with authentication, with an API layer based on Open API Initiative (OAI), formerly Swagger.io, and I add an Angular.js based web UI.
  2. QAVideos (2) – Add Custom Model and ORM to Node.js. Part 2 adds custom object models and ORM to a PostGreSQL database.
  3. QAVideos (3) – Extend Model and Using Swagger.io in Node.js. Part 3 adds object model extensions and takes a closer look at Open API Initiative (OAI) support.

Steps:

  1. Application Design
  2. Create the Application
  3. Add Static Pages
  4. Add Angular
  5. Add Signup
  6. Create an Authentication Service Provider
  7. Add Login
  8. Add Logout

1. Application Design

The initial design of the QAVideos application is very simple. A user can register, login and logout. An authenticated user can create, update and delete videos to the platform and see a list of their videos. Unauthenticated users can see all videos.

About StrongLoop

If you are not familiar with IBM API Connect (formerly StrongLoop), it is an API platform for Node.js and includes Loopback to quicly compose and manage APIs, and use graphical tools like the API Connect Designer to create, edit, manage and deploy your APIs. I will use the Loopback library that is part of API Connect to compose the model and the APIs.

You must install the API Connect cli for this tutorial. Test to see if you have API Connect cli installed:
$ apic -v
API Connect: v5.0.6.2 (apiconnect: v2.5.21)

Continue reading

Using Open Stack Object Storage on Bluemix

If you need to store images, videos, documents or other files and objects, you should save your objects not to a data base or disk, but to an Object Storage.

Documentation:
https://www.ng.bluemix.net/docs/services/ObjectStorage/index.html

Steps:
1. Get Access Token and Public URL
2. Get Account details
3. Create Container
4. Create Object

Get Access Token and Public URL

curl -X POST -H "Content-Type: application/json" -d '{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "<userId>",
                    "password": "<password>"
                }
            }
        },
        "scope": {
            "project": {
                "id": "<projectId>"
            }
        }
    }
}' 'https://identity.open.softlayer.com/v3/auth/tokens'

Continue reading

How to create a MEAN+ stack application (MongoDB, Express, Angular, Node + Bootstrap, Jade)

Steps

      Create a Node.js with Express.js App
      Create a Git repository and clone to localhost
      Add Angular.js
      Add Bootstrap
      Add MongoDb
      Add Jade
      Add an API for GET /people
      Connect to MongoDb
      Create a sortable table with Angular

Create a Node.js with Express.js App

I will be using Bluemix to host, build and deploy my Node.js application and the MongoDb service. I am developing on my localhost for unit testing before I upload my application to Bluemix, but you can also use the online editor and online Git instead.

1. If you do not have a Bluemix account yet, sign up for a free account at http://ibm.biz/bluemixnyc,
2. Once you have an account, go to your Bluemix console at https://console.ng.bluemix.net/,
3. Go to your Dashboard,
4. In the ‘Cloud Foundry Apps’ box, click ‘CREATE APP’,

5. Choose ‘WEB’

Continue reading

Calling a Watson service in Java

Overview

Note: this API needs to be updated to v2. Identify is now a resource on the Language Translation API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/language-translation/api/v2/

Making an HTTP REST API POST request in Java is in principle as simple this:
Request request = Request.Post(serviceURI)
.addHeader("Authorization", basicAuthorization)
.bodyString(body, ContentType.APPLICATION_FORM_URLENCODED);
Executor executor = Executor.newInstance();
String response = executor.execute(request).returnContent().asString();

This code, uses the Apache Fluent API, which exposes only the essentials of the Apache HTTPClient.

The Authorization header is required for authentication by the Watson API, and is a base64 encoded string.
String auth = username + ":" + password;
String basicAuthorization = "Basic "+ Base64.encodeBase64String(auth.getBytes());

The POST request is “x-www-form-urlencoded” and in Java consists of a List<NameValuePair>:
List<NameValuePair> params = new ArrayList<NameValuePair>();

Continue reading

Call IBM Watson APIs to add Machine Learning to your App

You can easily add very cool IBM Watson APIs to your application. You need to sign up for Bluemix, create an application and bind APIs or services to the app. This will create credentials (username and password) for each API or service.

Create a Bluemix acccount

First if you have not signed up for a Bluemix account yet, go to http://ibm.biz/bluemixnyc and Sign Up.

Create an App

Once you have confirmed your account, go to the Catalog and choose any Runtime. We will only use the runtime to bind services to, which will give us API credentials. For example, name the app ‘my-app1’.

Add a Service

Now, to add Services either go to Catalog > Services, and add a service to your app, or go to your app’s admin page. Bluemix is based on Cloud Foundry, so apps in Bluemix are listed under ‘CF Apps.’ Go to Dashboard > CF Apps > ‘my-app1’, and click the ‘Add a Service or API.’ I will add Personality Insights to my app in this example. Edit the required fields and click the Create button. Bluemix might ask you to restage your app. Now we’re ready to call the Personality Insights API.

Call the Watson API

To make a REST call to a Watson API, you must add an Authorization header with a Base64 encoded “Basic :” string. To generate the Basic Authorization header, use the colon as a separator for username and password and Base64 encode the Authorization string.

String username = "username from your service credentials";
String password = "password from your service credentials";
String s = username + ":" + password;
byte[] b = s.getBytes();
String enc = Base64.encodeBase64String(b);
String authorization = "Basic "+enc;

To call the API with the above credentials use the following curl statement from command line.

curl -X POST -H "Authorization: Basic BzAvEATz4UYqKfU5JA66GSIwKNgdATGiADR8YiWaOSH2Purd6tedhKUJaPa3KLshLq==" -H "Content-Type: text/plain" -H "Accept: application/json" -H "Accept-Language: en" -H "headers: true" -d 'put your text here' https://gateway.watsonplatform.net/personality-insights/api/v2/profile

For the API reference details go here
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/profile

This curl command will return a response object that you can visualize with the Visualize Personality Insights API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/visualize