First Test with Everythng Cloud, Raspberry PI and Python

12 May

During my constant research and work on the topic „Internet of Everything“ or „Internet of Things“ and to be more special: searching for a cool middleware, I came across a very interesting website: https://www.evrythng.com/. The whole concept behind looks very promising and I was tempted to give these guys a try. I have a few sensors, some knowledge in programming and I need some online space to store my data. So basic motivation is there to look out for some good concepts which are not doomed to be killed soon after their first release date because of loosing the race to be the best middleware. As a start I registered an account and my first idea was: connecting a sensor to their cloud based storage system and simply try how it might look like. Of course they offer a variety of other services, like:

  • Products, which are a description of class of THNGs (THNG = smallest unit)
  • Applications which can be used to connect e.g. a webbrowser
  • Campaings
  • Analytics
  • and many other very useful services.

Basically you can build a whole economic system on their online spaces. So where to start? First things first and so I decided to build only a simple use case to connect only one sensor which seems to be sufficient for a small test. The sensor which is the basic entity in their terms: a THNG. So for a fast start: build a small sensor and then try to connect it via a Python script. My self-imposed task was: using Python as programming language. (I could have used some different languages, like JAVA. This would have been much easier because there exists a ready-to-use Java API here ..)

At first I thought about a sensor architecture which included to build a sensor gateway to connect my local sensors (picture: Basic Sensor Architecture). Why? A gateway can:

  • protect the communication from sensor to cloud based services
  • has more CPU power to use stronger cryptographic algorithms
  • protect the sensors from being accessed from outside the Internet
  • store data when the connection to the cloud services is lost
  • lower the volume of traffic by sending data only, when the value is changing (saving bandwidth),
  • bridging connectivity options between e.g.: Ethernet and Bluetooth
  • translating IPv4 into IPv6 or vice versa
  • etc …

Sensor gw

Basic Sensor Architecture

But this would have taken some time, so I simplified the architecture a little bit and connected a sensing device directly to the EVRYTHNG cloud, which is reflected by the following picture: Proof of Concept Architecture.

Sensor

Proof of Concept Architecture

And of course the sensor must have enough CPU & storage capacity to run some functionality, like Python, ssh, etc.. on it. Of course there must be at least an ethernet connectivity to transport its data to its final destination. For these basic requirements I made my choice: I used one of my Raspberry PIs for my experiment. A bread board, some wires, a resistor and a waterproof temperature sensor did the trick:

PI sensor

Raspberry Pi as a sensor

Next step: getting data from the sensor via Python was not a very difficult task and there are a lot of very good tutorials on how to program Python to get sensor values: e.g. here. So I skipped that step in my description to concentrate on the new things.

I headed for a test account and after registering I got my personal access. Next step was to setup a THNG as a base entity to send data to. I decided not to do it via API calls (which would have forced me to write another script) instead I did it the traditional way: using the web-GUI from EVRYTHNG:

Thng

THNG Creation

After getting my THNG and of course here you can find the ThingID (greyed out) which is important to access it via API calls from your sensor, you can then define some properties which will then will be filled with data from your devices ( a really nice one is to use Geo locations, because if you have a moving sensor e.g. in a car, this could easily become quite handy). For this THNG I simply used one property: temperature. Of course you can use multiple of these data fields, but again: my task was only to do a simple test if I can get the whole story up and running.

But the most interesting part was: getting the REST API from EVRYTHNG to work with Python. No API exist for Python so far and I had to experiment a little on how to write a PoC Code for such a task. But again: it was not really a challenge, because the API from EVRYTHNG is very well documented and can be found here. A lot of examples of how REST is working, which return codes you can get back, how to create and delete, and most important: how to store and access your data. I decided to build a small class which fits for a sensor with limited amount of space. So the basic task was: sending data via http and before you can do that: converting the Python data array to JSON (yes, https is also an option and it is recommended once you use it for your production environment!!!). I did it via urllib2 and converting Python data to JSON, I used simply the json coder / encoder API. Nearly everything worked straight, except one little glitch which costs me some time: to convert data to an EVRYTHNG API-readable format I had to add brackets to the final json-converted data like: “[“+self.DATA+ “]“. After that challenge my little program worked and I was able to produce some valid input into EVRYTHNG. You can find the Python class below. Feel free to use it (at your own risk)…

”’

@author: Axel Dittmann

”’

import urllib2

import json

class sensorthng:

    ”’

   a very small class just to update sensors .. no further functionality provided 

    ”’

    def __init__(self, API_Token):

        self.API_TOKEN = API_Token

        self.HEADER_UPDATE = {‘Content-Type’‘application/json‘Authorization’: self.API_TOKEN}

        self.URL =https://api.evrythng.com/thngs/”

 

    def open_query (self, query_URL, data, header):

        self.URL_REQUEST = query_URL

        self.DATA = data

        self.HEADER=header

        #Request is GET if data = None, POST if data has some valid JSON data

        self.REQUEST = urllib2.Request(self.URL_REQUEST,self.DATA,self.HEADER)

        self.RESPONSE = urllib2.urlopen(self.REQUEST)

        return self.RESPONSE.read()

    

        #Updating properties of a thng

    def updating_properties(self, thng_id, data):

        self.DATA = data

        self.QUERY_URL = self.URL+thng_id+“/properties”

        #convert to JSON

        self.DATA = json.dumps(self.DATA)

        # now the important part: add [] to the json data, otherwise the function would not work

        self.DATA = “[“+self.DATA+ “]“

        #change of the header -> must be update string

        return self.open_query(self.QUERY_URL, self.DATA, self.HEADER_UPDATE)

My basic Proof-of-Concept code looked like this:

mythng = sensorthng(„USE your API-Access-Code-here”)

    

whileTrue:

    try:

        c_temp = read_ctemperature()

        print c_temp   

        data =  { ‘key’:‘temperature’,‘value’:c_temp}

        print mythng.updating_properties(„USE your THNG-ID here”, data)    

        time.sleep(60)

    except:

        print” Error Occured .. next try”

It is doing nothing else than sending data (of course getting the Celsius temperature from the sensor) and if an error occurs e.g. my internet connection will be down, then it will simply loop until the connection is restored. So no interruption of service in case of failure …

So basically just write a little code to implement the class and and use your API_Token from your EVRYTHNG login and your THNG_ID from your THNG screen. Then it should just work fine and the script updates your data .. Like my Raspberry PI: I decided to just sending a continuous stream for every 60 seconds:

PI response

Temperature and EVRYTHNG API return data

In this picture you can see, that the EVRYTHNG functionality add a timestamp to your data if you do not provide one. And this output shows also the return info from the EVRYTHNG API once the data was sent to their REST API if everything works and the data was successful delivered. And now let’s take a look how it looks like in the EVRYTHNG web GUI:

Evrything

THNG Overview

After clicking on the properties of my PI_temp_sensor you can than see a nice little graphical view of your submitted data:

Tmp graph

Temperature Data

The graphical view expressed the real life situation: at 8:10 it started to rain and this caused the temperature to drop 🙂.

So, first test done, in my opinion very easy and of course for me was my little test a great success, because everything works just „out-of-the-box“ and straight forward!! The EVRYTHNG idea has great potential and I am very sure that they will make their way to one of the leading THNG / IoT providers in the near future. The topics I haven’t covered so far are really interesting subjects like: using coordinates on a map to have a better overview of your THNGs, or grouping THNGs, etc .. I haven’t discovered EVRYTHNG’s full potential yet, but due to the fact that every upcoming day you have to learn new things, there must be something left for tomorrow 🙂 …

 

Source: http://ipv6poclab.org/2014/05/11/first-test-with-evrythng-cloud-raspberry-pi-and-python/

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: