How to connect a “thing” via HTTP using Ultralight 2.0

In this blogpost we are going to see how to connect a “thing” to the Internet of Things, using Ultralight 2.0 over HTTP using FIWARE.

In order to do it, we are going to use a Intel Edison board. We will add a temperature sensor. So our “thing” will be a thermometer, and we will send the information using the Ultralight 2.0, developed by Telefónica. In addition we will configure everything to use FIWARE.

So we will need:

  • An Intel Edison board and a temperature sensor.
  • A Cloud instance, or a server with access via SSH
  • Intel XDK or a similar IDE to program our “thing”
  • FIWARE Generic Enablers: Orion Context BrokerIoT Agent Ultra Light 2.0

The first step will be configure an instance, which we can connect with, and configure it. Having access to a cloud, we can launch an Ubuntu 14.04 instance and access to it via ssh. Then, we will install Docker and Docker-Compose, to configure our docker-compose file, which will be as follows:

orion:
  image: fiware/orion
  links:
    - mongodb
  ports:
    - "1026:1026"
  command: -dbhost mongodb
mosquitto:
    image: ansi/mosquitto
    ports:
        - "1883:1883"
    expose:
        - "1883"
ul2:
    image: josebenitez/idasul:vMALAGA
    links:
        - mongodb
        - orion
        - mosquitto
    ports: 
        - "7896:7896"
        - "4041:4041"
    expose:
        - "7896"
        - "4041"
    log_opt:
        max-size: 5G
mongodb:
  image: mongo:2.6
  ports: ['27017:27017']

Now, in order to have the services up and running, we will use the command

sudo docker compose up -d

And after this, using the following commandwe will check that the services are up and running.

sudo docker ps

The next step will be configuring the IoT Agent, in order to communicate with  Orion Context Broker. To do it so, we will follow the next steps:

  • Extract the configuration file from the IoT Agent (docker) to the home directory of our instance (local host)
  • Modifying the parameters
  • Insert back again the configuration inside the IoT Agent docker container.
  • Restart the IotAgent

In order to copy the configuration file, we will use the command:

sudo docker cp creation_ul2_1:/opt/iotaul/config-blank.js ~/

We are now configuring our IoT Agent in order to work over HTTP, so everything related to MQTT will be seen later.

var config = {};

config.mqtt = {
    host: 'mosquitto',
    port: 1883
};

config.http = {
    port: 7896
};

config.iota = {
    logLevel: 'DEBUG',
    timestamp: true,
    contextBroker: {
        host: 'orion',
        port: '1026'
    },
    server: {
        port: 4041
    },
    deviceRegistry: {
        type: 'mongodb'
    },
    mongodb: {
        host: 'mongodb',
        port: '27017',
        db: 'iotagentul'
    },
    types: {},
    service: 'howtoService',
    subservice: '/howto',
    providerUrl: 'http://192.168.210.176:4041',
    deviceRegistrationDuration: 'P1M',
    defaultType: 'Thing'
};

config.defaultKey = 'TEF';

module.exports = config;

The most important thing of this file is specifying the “service” and “subservice” headers that we are going to use, the entity type (in this case “Thing”) and a URL and port where Orion Context Broker can be reached from the IoT Agent. In this case, the private IP of the instance will be enough,but we should avoid using localhost reserved word, because if we do it , we will find communications problems, since the docker container acts as a virtual machine with its own network interface, and we will not be able to communicate if we use this reserved word or 127.0.0.1

Now we will copy back the modified file and will substitute what was inside the IoT Agent container, and finally, we will restart the service, in order to reload the configuration. To do it, we will use the following commands

sudo docker cp ~/config-blank.js creation_ul2_1:/opt/iotaul/ && sudo docker restart creation_ul2_1

We have now our Cloud architecture, so once we have done it, we can now use our Intel Edison. We will be using the Intel XDK IDE, to program in Node.js and upload it to the board. The steps to be followed are:

  • Connect the board
  • Configure it to work in the same network
  • Connect the sensors
  • Upload the code
  • Leave the board sending data

The Intel Edison has two USBs, one for the data transmission and another one for the power supply. We connect both of them to our computer and, in the case of Mac or Linux, there is no need to install any additional driver. In Window’s case you will need to install some specific drivers.

Once it has been done and with the board connected to the computer, we open a Terminal and use the command:

screen /dev/tty.usbserial-A903OR4K 115200 -L

We will be asked to introduce user and password. In the first usage it will be root user and blank password. Once we are inside the board, we will configure it, and use the command

In this way, we will assign the device a name and a password, and will connect it to the same WiFi where our computer is.

Them we are going to use an LCD screen and a temperature sensor to show the ambient temperature recollected by the sensor, and send it through the Internet to our Orion Context Broker in the cloud. To do it so, we connect the Arduino shield to our Intel and connect the temperature sensor to one of the analog input/outputs and the LCD to one of the I2C input/outputs. The board have the following appearance

Now it is the moment to upload the code in the board. We will open the Intel XDK and we will upload the code, that will be explained later.

"use strict" ;

var mraa = require("mraa") ;
var request = require("request");
var groveSensor = require('jsupm_grove');

var temp = new groveSensor.GroveTemp(0);
console.log(temp.name());


useLcd();

var FIWARE_APIKEY = "TEF" ;


//Device´s name
var FIWARE_DEVICE = "edison" ;
var FIWARE_SERVER = '130.206.126.208';
var FIWARE_PORT = '7896';
var STATUS_CODE_OK = 200;

var sending;

function uploadData(){    
    setInterval(function(){
        var body=""; //FIWARE IoT Stack body message
        body = "temperature" + '|' + sending;

        console.log('--Sending measures to FIWARE IoT Stack '+body);



        var options = { 
          method: 'POST',
          url: 'http://'+FIWARE_SERVER+':'+FIWARE_PORT+'/iot/d',
          qs: { i: FIWARE_DEVICE, k: FIWARE_APIKEY },
          body: body 
        };
        console.log(options.url);
        console.log(options.qs);

        request(options, function (error, response, body) {
            if (error){
                console.log('--ERROR connectivity problems');	
            }else{
                if (response.statusCode == STATUS_CODE_OK){
                    console.log('--Data uploaded to FIWARE IoT Stack');
                }else{
                    console.log('--ERROR Status Code: '+response.statusCode);
                }
            }
        });
    },5000)
}

function waiting2(display){
    var waiting = setInterval(function() {
        var celsius = temp.value();
        display.setCursor(1,1);
        display.write(celsius.toString() + " Celsius");
        sending = celsius.toString();
        }, 1000);
}

function rotateColors(display) {
    var red = 0;
    var green = 0;
    var blue = 0;
    display.setColor(red, green, blue);
    setInterval(function() {
        blue += 64;
        if (blue > 255) {
            blue = 0;
            green += 64;
            if (green > 255) {
                green = 0;
                red += 64;
                if (red > 255) {
                    red = 0;
                }
            }
        }
        display.setColor(red, green, blue);
        display.setCursor(0,0);
        display.setCursor(1,0);


    }, 1000);
}


function useLcd() {
    var i2c = require('./i2c');
    var display = new i2c.LCD(0);
    process.on("exit", function(code) {
        clearInterval(intervalID) ;
    }) ;

    display.setColor(0, 60, 255);
    display.setCursor(1, 1);
    
    
    //display.write('hi there');
    display.setCursor(0,0);  
    display.write('secmotic.com');
    display.waitForQuiescent()
    .then(function() {
        rotateColors(display);
        waiting2(display);
        uploadData();
    })
    .fail(function(err) {
        console.log(err);
        display.clearError();
        rotateColors(display);
        waiting2(display);
        uploadData();
    });
}

In this code the function useLcd() is the first to be called, which will write in the upper part of the LCD a message, and will be followed by the rotateColors(display)waiting2(display) and uploadData() functions. The rotateColors function will only change the LCD screen colors, while the function waiting2 will read the sensor and will write its value in the display, to finally call the uploadData function. The uploadData function will communicate our “thing” with the cloud using the Ultralight 2.0 using HTTP

We can see in the IntelXDK console how the data is being uploaded if we execute the code

In addition, we can now check that the data are being collected correctly in Orion Context Broker if we do a REST request to its API

Finally we can create a dashboard to visualize the data of Ultralight 2.0 in real time in a very easy way thanks to freeboard.io

With an existing account of  freeboard.io, we will add the datasource. We will have to introduce the parameters like Fiware-Service, Fiware-Subservice, Host:Port, etc.

Then we will add a pane to later on add a widget of the Gauge type.

Next, we link the datasource that we just added to the graphic value. In this case it would result as follows:

Once saved, we will be able to visualize our graphic and see how it changes every second.

The whole process can be found in our YouTube channel. You can find the video here:

This was the post about how to connect a “thing” to the Internet of Things using FIWARE and the Ultralight 2.0 protocol over HTTP

Don’t miss the next post, where we will discuss about how to communicate via MQTT instead of HTTP!

By | 2017-03-24T11:44:34+00:00 March 24th, 2017|Secmotic|0 Comments

About the Author:

Leave A Comment