How to connect a “thing” via MQTT using Ultralight 2.0 and FIWARE

In the previous post we saw how to connect a “thing” via HTTP using Ultralight 2.0 with a FIWARE architecture. In this post we will do exactly the same but using the MQTT transport protocol, but, why?

The reason is because MQTT is a lighter transport protocol than HTTP in terms of data sending. The bandwidth is something critical in the Internet of Things, because usually IoT devices are often decentralized and need their own data connection, like a SIM card or similar. These M2M (machine to machine) cards have a proportional costs to the used bytes, so it is worth be optimized.

A bit of introduction to MQTT

MQTT (Message Queue Telemetry Transport) is a protocol used for communication between machines (M2M), something common in the IoT. It is oriented to the communication of sensors, using low  bandwidth y it does not require great hardware requirements, such as RAM or CPU.

The MQTT architecture follows a star topology, with a central node or “broker“, in charge of managing the network and trasnmitting the messages. In addition, the communication is based in  “topics“. The client creates the topic and publishes his message in that topic, and everyone who wants to receive the message has to be subscribed to the topic. This enables the communication one-to-one or one-to-many.

What will we need?

We will need exactly the same that in the previous post:

  • 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 required steps will be the same that in the previous post, but changing a couple of configurations. The first one to modify is the one of the docker compose, where the MQTT Broker will be added:

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

Our IoT Agent configuration will maintain exactly the same that in the previous example:

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: '',
    deviceRegistrationDuration: 'P1M',
    defaultType: 'Thing'

config.defaultKey = 'TEF';

module.exports = config;

Programming the Intel Edison

In order to implement MQTT over our Intel Edison we will need to use the MQTT library of npm and connect to the server. We will include in the code the following lines:

var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://')

And in the board, via SSH, we will need to execute the following command:

sudo npm install -g mqtt

Finally, we will substitute the implementation of Ultralight 2.0 over HTTP for the implementation of Ultralight 2.0 over MQTT

function uploadData(){    
       console.log('#### MQTT CONNECTION SUCCESSFUL ####');
       console.log('#### MQTT MESSAGE SENT: '+sending+' ####');

And with this we will be actually sending the information to the server, as it can be observed in the logs.

We have seen how to implement the MQTT in a basic way.

Stay tuned to the Secmotic’s blog! We will release soon bigger and more useful projects with MQTT

By | 2018-05-30T12:55:25+00:00 May 30th, 2018|Secmotic|0 Comments

About the Author:

Leave A Comment