domingo, 5 de febrero de 2012

Internet of things: How to send data to Pachube


Abstract

Last article was dedicated to describe Pachube, today I am going to show you how to send data from a PC to datastreams created on Pachube's Web Site. The information depicted here will give you a general idea of the procedures needed to send information to a datastream from a sensor inside a laptop (i.e. temperature sensor) , and in the next article we will use this background to obtain values from a temperature sensor attached to an arduino platform, to have a chart updated in real time at pachube.

This way we can have a database of any kind of values obtained from any source of data on the internet, totally free and 24 hours uptime.

VIDEO: How to send data to a feed on pachube

So, let get started.

How to send data to patchube

A feed is a database that we can create on www.pachube.com, after create an account. This service is free and can be used anywhere.

A quickstart of pachube can be accessed here, where there are instructions to automatically get statistical information of out twitter.. give it a try!

VIDEO: How to send data to a feed on pachube

Once we have created a feed, we are ready to send information to our database.

There are several ways to do it. I have chosen the python method due to it's versatility and because python is multi-platform.

For our experiment I have decided to read the temperature of my laptop based on Ubuntu. This can be accomplished using the sensors command. Using a simple filter we can isolate the temperature value from the output of sensors:


acpitz-virtual-0

Adapter: Virtual device
temp1:        +54.0°C  (crit = +100.0°C)
temp2:        +54.0°C  (crit = +100.0°C)

I have used the follwing command to isolate the temperature  value of one of the cores of my laptop Dell.

sensors | grep temp1 | cut -d: -f2 | awk '{print $1}' | sed s/+//g | sed s/"°C"//g

... and the result after I execute the above command is:

54.0

Which is the temperature that I want to send and have a chart of it.

After we are confident that we can obtain a consistent value every time we ask for it, it is time to create the python script to get that value and send it to our database on pachube. We can send a value every 30 seconds or every minute, or using the interval that is suitable for our application. This interval is going to be programmed using the automated command execution facility on linux called cron.

The code is very simple and is written below:

1 #!/usr/bin/python
2 import subprocess
3 import eeml

4 pachubeURL="/v2/feeds/47024.xml"
5 pachubeKEY="bfr0eqPG26CdsRgwTOrgIPZV86DRGJr1q9Ept4ZjaJ8"

6 proceso = subprocess.Popen("/home/ydirgan/SCRIPTS/pachube/tempRead.ksh",stdout=subprocess.PIPE,stderr=subprocess.PIPE)

7 temperatura, errores = proceso.communicate()

8 pachube = eeml.Pachube(pachubeURL,pachubeKEY)
9 pachube.update([eeml.Data(0, temperatura, unit=eeml.Celsius())])

10 pachube.put()

Lines 4 and 5 defines the feed and our unique key needed to authenticate and access the web service.

In line 6 we execute  tempRead.ksh that is a simple script thet execute the filter shown above. 

#!/usr/bin/ksh
sensors | grep temp1 | cut -d: -f2 | awk '{print $1}' | sed s/+//g | sed s/"°C"//g

Line 7 reads the output of the script tempRead.ksh which is a value that will be packed in line 9 using eeml. In our case the UID of our feed is 0, the value is in temperatura variable and the units are in Celsius.

Finally we send the value using the put method in line 10.

This simple script can be manipulated and modified to send more than one value if we have more than one feed. Every feed is enumerated when we create it (see video). An example of this might be give the following line as parameter of the update() method:

[eeml.Data(0, temperatura, unit=eeml.Celsius()), eeml.Data(1, cpuUser, unit=eeml.none())]

Using this simple procedure we can update our feeds to have our sensors values up to date on the internet publicly or in a private fashion.
In the next article I am going to show you how to get values from sensors attached to arduino and send them to our feeds.




No hay comentarios:

Publicar un comentario