FIWARE lab – end to end exercise

On a daily basis I deal with data integration to the Telefónica’s SmartCity Platform coming from heterogeneous sources. The most interesting for me is device integration.

Challenged by a training course, the freaky I carry in, came out and dared to connect a device with a fiware key piece, such as a “context broker”.

The exercise consisted in transmitting humidity and temperature measures taken using a sensor (model DHT11) to a server, a context broker, in this case a filab server, an instance running a context broker, “orion”.

Why transmitting the measures to a context broker?. Well, maybe an explanation about what is a context broker helps. A very technical definition can be found here, but I would define it as a way to put the information about your devices in a place that allows you handling the information as you wish from there on. It´s a way to make your device information available to publish it on a web page, or to pass it on to another SW component (i.e. a database, or other information consumers).

A context broker, handles “contexts”, obvious huh? 😉

Yes… a context is a sensor measuring a given variable (i.e. temperature or humidity on a given place). That context (its measures) may vary and decissions can be taken based on that “context” information. That context can be “consumed” by “context subscribers”, etc.

On my exercise I don´t go that deep into detail. I have just connected the device to the server. That is the base to further developments.That’s what I describe next in a “howto” fashion, step by step:

FIRST STEP: Register a user on FIWARE

Access the URL below and fill in the registration form:

https://account.lab.fiware.org/sign_up/

regform1

 

Once the registration is confirmed by clicking the link in the email that is received after submitting the form. After that, you can access FILAB to create the instance you wish out of all the available images. In this case I choose “orion context broker v4.4”

https://cloud.lab.fiware.org

 

SECOND STEP: Create the ORION instance (the context broker)

Once logged in FILAB, I created this instance following the wizard:

filab1

I assigned it  a floating IP address.

filab2

Configured SSH Keys to access remotely:

filab3

And configured this rules so packets addressed to orion(port 1026), ssh(port 22) and icmp are permitted. The rules editor is a little bit cumbersome. This took me a while to figure out, not easy to explain but it works 🙂

filab4

Once the instance was available I logged in and updated the SW as the loggin banner advises:

$ yum install contextBroker

$  yum install cygnus

$ yum install pep-proxy

THIRD STEP: Create the context ENTITY in ORION

With the invaluable help from Fermin Galan I created this context entity in orion, executing the script below:

curl ‘localhost:1026/v1/contextEntities’ -X POST -s -S -vvvv –header ‘Content-Type: application/json’ –header ‘Accept: application/json’ -d @-  <<EOF

{

“id”: “Room1”,

“type”: “Room”,

“attributes”: [

 {

   “name”: “temperature”,

   “type”: “float”,

   “value”: “30”

 },

 {

   “name”: “humidity”,

   “type”: “float”,

   “value”: “80”

 }

]

}

EOF

FOURTH STEP: Create the context ENTITY in ORION

Once the entity is created data can be sent by using POST queries from anywhere permitted, but first you can check the entity es created by issuing a GET query:

entity

FIFTH STEP: Program the device to MEASURE temperature & humidity and POST them to the ContextBroker

The sensor used is the DHT11 (costs less than 3USD):

sensor

and the device used to obtain the measures and send them to the context broker server is the nodemcu devkit v12 (costs less than 10USD):

nodemcu

This device has a WiFi chip built in and a PIC with several digital I/O pins.

This is the final montage:

kit

Here the code run in the nodemcu device using ESPlorer :

--CONFIGURE WIFI (elegir “SSID” y “PASSWORD”)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD")
tmr.delay(10000)
-- MEASURE AND POST DATA TO ORION
function postValores(level)
--MEASURE USING PIN 2
pin = 2
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
if status == dht.OK then
    print("DHT Temperature:"..temp..";".."Humidity:"..humi)
 
elseif status == dht.ERROR_CHECKSUM then
    print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
    print( "DHT timed out." )
end
 
-- CONNECT AND SEND (POST) TO ORION
    connout = nil
    connout = net.createConnection(net.TCP, 0)
 
    connout:on("receive", function(connout, payloadout)
        print (payloadout);
        if (string.find(payloadout, "400 Bad Request") ~= nil) then
            print("BAD POST");
        end
        if (string.find(payloadout, "411 Bad Request") ~= nil) then
            print("BAD POST");
        end  
        if (string.find(payloadout, "200 OK") ~= nil) then
            print("GOOD POST");
        end
    end)
 
    connout:on("connection", function(connout, payloadout)
 
        print ("Posting...")
 
 local var = '{"id": "Sala1", "type": "Sala", "attributes": [{"name": "temperature","type": "float","value": "'..temp..'" },{"name": "humidity","type": "float","value":"'..humi..'"}]}'
        local num = string.len(var)  
        local postval = "POST /v1/contextEntities HTTP/1.1\r\n"    
          .."Host: 130.206.124.50\r\n"
          .."Accept: application/json\r\n"
          .."Content-Type: application/json\r\n"
          .."Content-Length: "
          ..num.."\r\n"
          .."\r\n"
          ..var.."\r\n"  
        print (postval)
        connout:send(postval)
    end)
 
    connout:on("disconnection", function(connout, payloadout)
        connout:close()
        collectgarbage()
    end)
    connout:connect(1026,'130.206.124.50')
end
--  SEND EVERY  10 
tmr.alarm(1, 10000, 1, function() postValores(0) end)

 

ESPLORER

Advertisements

One thought on “FIWARE lab – end to end exercise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s