Openhab and IoT platforms integration

512 views
Skip to first unread message

Juanker Atina

unread,
Feb 6, 2012, 8:24:27 AM2/6/12
to openhab
I want to resume here all questions we have actually made about
persistence in external IoT platforms. And it could be a good place to
discuss the best way to integrate with them (and to post working
solutions to achieve that), and also to share nice hacks and tricks to
exploit all their cool features.

So, i have seen at least three related threads where we have talked
about these topics:

openHAB + ThingSpeak: http://groups.google.com/group/openhab/browse_thread/thread/599fee9c96f8dda8
Issue 21: Persistence support: http://code.google.com/p/openhab/issues/detail?id=21#c18
Persistence/Time series support: http://groups.google.com/group/openhab/browse_thread/thread/b90dd6618a98b989

IMHO, I think that these platforms are complementary to any future
persistence solution (provided by openhab). In fact, they can coexist
and you can benefit from both solutions, simultaneously.

But, what are the main benefits or capabilities of these platforms?
Well, it differs a lot from one platform to another, but they have two
points in common.

An IoT platform is, essentally, a data brokerage platform that enables
you to store realtime sensor & environment data. In other words, it is
a on-line database service provider for developers to connect sensor
data to the web, and upon that build their own applications, usually
managing millions of data points per day from thousands of
individuals, organisations and companies across the world.

So, the first point is the ability to store your data, letting the
platform handles the scalability & high-availability required for
complex data management.

Once your data is stored into the platform, the second point is to
graph and monitor your remote environment. This way, all these
platforms let you see the data in several ways (graphs, feeds,
csv...).

But these platforms have evolved a lot from the last year, adding
features that make them not only a data brokerage platform but also a
cloud platform to build complex services on top. I'm talking about
services like:

Acting with your own data
Create triggers, alarms, notifications over the data stored
(like rules, if-then-else).
Import & export data.
Do calcs with your data (convert to unit, apply formulas...)
Share data & create communities
Connect sensors to the web (like let your plant tweet that it
needs water).
Connect sensors between them (m2m, ambient intelligence, or
whatever).
Connect remote sensors from different places (like using data
from all your country to create your own service, or to send a command
to your own devices).
Build other services on top of that:
Web & Mobile Apps
Augmented Reality (http://blog.pachube.com/2009/06/pachube-
augmented-reality-demo-with.html)
Energy, noise or pollution monitors.
Geo-Location


So, letting the openhab platform speak with these IoT platforms will
provide the chance to deal with the last trends and innovations on
these topics (growing very fast) and also the opportunity to work with
emerging technologies, like EEML (http://www.eeml.org/).

FYI, I have found a great directory of IoT platforms,i think it could
be a good starting point to explore this world:
http://postscapes.com/internet-of-things-platforms


Enough theory for today :)... let's talk about the platforms and how
to integrate openhab data.


PACHUBE

Perhaps, the most known and used IoT platform today. You can read all
about this platform here: http://community.pachube.com/about

To start working with Pachube, you must read their quickstart guide:
http://community.pachube.com/quickstart

Once you have complete the required steps (register, get API key,
setup your feeds, etc) you can use their HTTP + Json API. All their
developer documentation is here http://pachube.com/docs/

So, basic integration would be to send data from Openhab to a
previously created datastream on Pachube (http://pachube.com/docs/v2/
datastream/update.html)

As you have to deal with Json, the executeUrl function
(org.openhab.io.net.utils.HttpUtil) can't be used in rules, so we have
created a specific function to deal with pachube data logging.

This way, you have a SenseEvent class to execute the request, which
uses two other classes: a bean to store the data and a transformer to
do the json serializer stuff.

And you can send the current values of each item (seen by pachube as a
datastream inside a feed) to your pachube account. Of course, it could
be improved, but i think it's a first step to play with Pachube.

1. OpenHab Config File: To store the Pachube URL where we are going to
send the data and API KEY, too.

############### OpenHab CONFIG file ###############
drools:pachubeServer=http://api.pachube.com/v2/feeds/46510/
datastreams/
drools:pachubeKey=xxxxxxxxxxxx

2. Java Function to send the data: (you must import this function in
your rules to use it)

############### PachubeEvent ################

public static int sendPachube(PachubeEventBean peb){

try {
URL url = new URL(RuleService.pachubeServer +
peb.getId());
HttpURLConnection httpCon = (HttpURLConnection)
url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT"
);
httpCon.setRequestProperty("Content-type", "application/
json");
httpCon.setRequestProperty("X-
PachubeApiKey",RuleService.pachubeKey);
OutputStreamWriter out = new
OutputStreamWriter(httpCon.getOutputStream());

JSONSerializer serializer = new
JSONSerializer().transform(new PachubeEventTransformer(),
PachubeEventBean.class);
String res = serializer.serialize(peb);

out.write(res);
out.flush();
int responseCode = httpCon.getResponseCode();
logger.info("Sended event to Pachube with response
message:"+httpCon.getResponseMessage());
out.close();

return responseCode;
} catch (Exception e) {
logger.warn("Connection error");
return -1;
}
}


3. Transformer to do the json serializer stuff

################### PachubeEventTransformer #################
public class PachubeEventTransformer extends AbstractTransformer
implements
Transformer {

public void transform(Object o) {

if(o instanceof PachubeEventBean){
PachubeEventBean pe = (PachubeEventBean)o;
getContext().writeOpenObject()
;
getContext().writeName("id");
getContext().write("\""+pe.getId()+"\"");
getContext().writeComma();
getContext().writeName("current_value");
getContext().write("\""+pe.getCurrent_value()+"\"");
if(null!=pe.getMax_value() && !pe.getMax_value().isEmpty())
{
getContext().writeComma();
getContext().writeName("max_value");
getContext().write("\""+pe.getMax_value()+"\"");
}
if(null!=pe.getMin_value() && !pe.getMin_value().isEmpty())
{
getContext().writeComma();
getContext().writeName("min_value");
getContext().write("\""+pe.getMin_value()+"\"");
}
getContext().writeCloseObject();
}

}

}


4. PachubeEventBean: to store data

################### PachubeEventBean #################
private static String current_value;
private static String max_value;
private static String min_value;
private static String id;


5. DRL file. The last parameter is the datastream name (¡)

################# DRL #################
sendPachube(new PachubeEventBean($val.toString(), null, null,
"temp"));




OPENSEN.SE (http://open.sen.se/)

This is, i think, the last player on the IoT Platforms scene. But it
arrives with a lot of cool features.

You can read this page to get an idea of how this platform works:
http://open.sen.se/devices/add/custom/#navbar

One more time, you have to complete the required steps in order to
start playing with the platform (register, etc...). And you can read
about their API in this pages: http://dev.sen.se/device.html#publishing-events

Today, they offer the HTTP API to send and get requests, but they show
other future protocols, like xmpp (nice¡) and CoAP (something related
to IPv6). And as they are using JSON, we have done something similar
to pachube integration.

So, to start using this code, you must have at least one feed in your
open sense account.



1. OpenHab Config File: To store the Pachube URL where we are going to
send the data and API KEY, too.

############### OpenHab CONFIG file ###############
drools:senseServer=http://api.sen.se/events/?sense_key=
drools:senseKey=xxxxxxxxxxx

2. Java Function to send the data: (you must import this function in
your rules to use it)

############### SenseEvent ################
public static int sendSense(SenseEventBean seb){

try {
URL url = new URL(RuleService.senseServer+
RuleService.senseKey);
HttpURLConnection httpCon = (HttpURLConnection)
url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-type", "application/
json");
OutputStreamWriter out = new
OutputStreamWriter(httpCon.getOutputStream());

JSONSerializer serializer = new
JSONSerializer().transform(new SenseEventTransformer(),
SenseEventBean.class);
String res = serializer.serialize(seb);

out.write(res);
out.flush();
int responseCode = httpCon.getResponseCode();
logger.info("Sended event to Sen.se with response
message:"+httpCon.getResponseMessage());
out.close();

return responseCode;
} catch (Exception e) {
logger.warn("Connection error");
return -1;
}
}


3. Transformer to do the json serializer stuff

################### SenseEventTransformer #################
public class SenseEventTransformer extends AbstractTransformer
implements
Transformer {

public void transform(Object o) {

if (o instanceof SenseEventBean) {
SenseEventBean pe = (SenseEventBean) o;
getContext().writeOpenObject()
;
getContext().writeName("feed_id");
getContext().write("" + pe.getFeed_id());
getContext().writeComma();
getContext().writeName("value");
getContext().write("\"" + pe.getValue() + "\"");
if (null != pe.getTimetag() && !pe.getTimetag().isEmpty())
{
getContext().writeComma();
getContext().writeName("timetag");
getContext().write("\"" + pe.getTimetag() + "\"");
}
getContext().writeCloseObject();
}

}

}

4. SenseEventBean: to store data

################### SenseEventBean #################
private static String feed_id;
private static String value;
private static String timetag;


5. DRL file: first parameter is the feed id

################# DRL #################
sendSense(new SenseEventBean("7482", $val.toString(), null));


OPEN ENERGY MONITOR (http://openenergymonitor.org/emon/emoncms)

This is a piece of the Open Energy puzzle. They are working on a open
hardware platform to collect energy data (among other info), and they
have released a tool to store and do some monitoring over your data.

So, yes, you have to deploy your own open energy monitor server
(apache, mysql and php). It's not a cloud service, but still is a good
option (see dashboard, for example, http://openenergymonitor.org/emon/node/299)

As Open Energy monitor API doesn't deal with JSON, to integrate
openhab with emon is as easy as send a http post request.

This way, you can benefit from executeUrl function (already available
on openhab) to write a rule like this:


rule "Luminosity to EMON cms"
when
$event : StateEvent(itemName=="Luminosity")
then
if ($event.hasChanged()) {
executeUrl("POST", "http://localhost/openenergymonitor/api/
post?apikey=xxxxxx&json=%7Bluminosidad:" +
$event.getNewState().toString().substring(0,2) + "%7D", 600);
}
end

Next steps to improve openhab - emon integration

1. To store Open Energy monitor URL and KEY in openhab config file.
2. To hide all details in a function and use it in rules.





THINGSPEAK (https://www.thingspeak.com/)

This is a good alternative to Pachube or Open Sense, and it's open
source. So you can download and deploy it on your own server (ruby, i
think).

If you choose to use it as a cloud service, you must complete the
required steps in order to start working with Thing Speak (see
http://community.thingspeak.com/documentation/ and
http://community.thingspeak.com/tutorials/).

ThingSpeak doesn't need to speak Json, so like Open Energy Monitor,
you can integrate openhab and ThingSpeak with executeURL function.


rule "Luminosity to ThingSpeak"
when
$event : StateEvent(itemName=="Luminosity")
then
if ($event.hasChanged()) {
executeUrl("POST", "http://api.thingspeak.com/update?
key=xxxxxxxxx&field2=" +
$event.getNewState().toString().substring(0,2), 600);
}
end

Next steps to improve openhab - thingspeakintegration

1. To store ThingSpeak URL and KEY in openhab config file.
2. To hide all details in a function and use it in rules.




NIMBITS: http://www.nimbits.com/#

In their own words, Nimbits Server is a free and open source
Operational Process Data Historian you can use to create and share
data points on a public or private cloud and record your changing
numeric, text based, GPS, json or xml values into them. Nimbits is
software platform you can use to develop your own cloud, android and
micro-controller applications. With our open source software
development kit, we greatly simplify your requirements for logging,
alerts and more.

It has a lof of nice features, like WolframAlpha integration to do
calcs, or google spreadsheet integration, charting with google charts,
geo-location and so on.

You can use it as a cloud service or, again, download it and deploy on
your own server (scalable private cloud on your network, Amazon EC2,
Google App Engine).

It's developed inside Google App Engine platform, so you need a gmail
account to start and all their features are tightly related to all
google platform stuff. If you love google platform, you will love
Nimbits, as they play nicely with all google stuff (for example, you
can talk to your devices using gtalk).

To integrate openhab and Nimbits, you have two alternatives. Http
services or XMPP talking.

So, we have integrate both with send function in rules, talking
through XMPP between openhab items and Nimbits platform.

rule "Luminosity to Nimbits"
when
$event : StateEvent(itemName=="Luminosity")
then
if ($event.hasChanged()) {
send("m...@gmail.com","Luminosity=" +
$event.getNewState().toString());
}
end


Next steps to improve openhab - nimbits integration

1. To store xmpp nimbits account in openhab config file.
2. To hide all details in a function and use it in rules (??)

Juanker Atina

unread,
Feb 6, 2012, 8:27:09 AM2/6/12
to openhab
A couple of things:

- We could release all this code if you agree (Kai and community).
- We think that it could be nice to translate this rules to openhab
engine, having them in drools syntax and openhab rules syntax too.
- We think that it could be nice to have a Wiki page to explain these
topics.


so, what do you think?

On 6 feb, 14:24, Juanker Atina <juank...@gmail.com> wrote:
> I want to resume here all questions we have actually made about
> persistence in external IoT platforms. And it could be a good place to
> discuss the best way to integrate with them (and to post working
> solutions to achieve that), and also to share nice hacks and tricks to
> exploit all their cool features.
>
> So, i have seen at least three related threads where we have talked
> about these topics:
>
> openHAB + ThingSpeak:  http://groups.google.com/group/openhab/browse_thread/thread/599fee9c9...
> Persistence/Time series support:http://groups.google.com/group/openhab/browse_thread/thread/b90dd6618...
> developer documentation is herehttp://pachube.com/docs/
> option (see dashboard, for example,http://openenergymonitor.org/emon/node/299)
>
> As Open Energy monitor API doesn't deal with JSON, to integrate
> openhab with emon is as easy as send a http post request.
>
> This way, you can benefit from executeUrl function (already available
> on openhab) to write a rule like this:
>
> rule "Luminosity to EMON cms"
>     when
>         $event : StateEvent(itemName=="Luminosity")
>     then
>         if ($event.hasChanged()) {
>             executeUrl("POST", "http://localhost/openenergymonitor/api/
> post?apikey=xxxxxx&json=%7Bluminosidad:" +
> $event.getNewState().toString().substring(0,2) + "%7D", 600);
>         }
> end
>
> Next steps to improve openhab - emon integration
>
> 1. To store Open Energy monitor URL and KEY in openhab config file.
> 2. To hide all details in a function and use it in rules.
>
> THINGSPEAK (https://www.thingspeak.com/)
>
> This is a good alternative to Pachube or Open Sense, and it's open
> source. So you can download and deploy it on your own server (ruby, i
> think).
>
> If you choose to use it as a cloud service, you must complete the
> required steps in order to start working with Thing Speak (seehttp://community.thingspeak.com/documentation/andhttp://community.thingspeak.com/tutorials/).

Kai Kreuzer

unread,
Feb 8, 2012, 5:38:25 PM2/8/12
to ope...@googlegroups.com
Thanks for your good and profound examples regarding IoT platforms!

What I get from your conclusions is that all these services have the same kind of needs:
1. Some configuration
2. Some way to be called from within rules
3. (imho missing from your examples:) send strategies apart from rules (every minutes etc.)

For me, this results in:
a) One (optional) bundle per IoT platform
b) Configuration in openhab.cfg not done under the drools namespace, but under their own namespace (pachube, thingspeak, etc.), so that the configuration data is correctly delegated to the appropriate bundle.
c) A single rule action like send(Item item, String service), which delegates the call to the appropriate bundle. The user hence does not have to learn different commands for different platforms (and the integration in rules is easier as only one static import is needed and not many dynamic ones depending on the installed IoT bundles).
d) A new DSL file type should be available to allow (optional) additional configuration of send/export/persist strategies for items or groups, so that the user does not have to use rules, but can easily configure automatic exports. This DSL can be shared with the normal/local persistence configuration as they have imho the same requirements regarding trigger strategies.

I'll try to do/start some implementation of c) and d) next week and also provide an example implementation of an IoT bundle. Adding new ones should then be dead simple.

Regards,
Kai

> --
> You received this message because you are subscribed to the Google Groups "openhab" group.
> To post to this group, send email to ope...@googlegroups.com.
> To unsubscribe from this group, send email to openhab+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/openhab?hl=en.
>

Juanker Atina

unread,
Feb 9, 2012, 4:23:51 AM2/9/12
to openhab
You're welcome, Kai.

I've been playing last days with the newest platform in this scene:
open.sen.se. And i must say that it's awesome. They have to do some
work, as this is a beta service, but their base product is imho simply
the best.

You can see my current dashboard here (http://goo.gl/4B3lD).

I'm talking about open.sen.se because i think that we could see how
other players are solving problems like persistence, charting and, why
not, apps and dashboards.

All these platforms are working with similar topics (feeds, channels,
apps, charts over one or more sources), so we could take some ideas
for openhab persistence.

Finally, i'm happy to read that you are working on that, and i agree
with all your points. If you need help, don't hesitate to contact me.
> ...
>
> leer más »

Thomas Eichstädt-Engelen

unread,
Feb 9, 2012, 9:24:58 AM2/9/12
to ope...@googlegroups.com
Hi Juanker,

I've been playing last days with the newest platform in this scene:
open.sen.se. And i must say that it's awesome. They have to do some
work, as this is a beta service, but their base product is imho simply
the best.

is there already some code you would like to share (except for the snippets you already sent here)?

Cheers,

Thomas E.-E.

Juanker Atina

unread,
Feb 9, 2012, 9:33:02 AM2/9/12
to openhab
Yes, of course, it needs some refactoring to do it nicely (for
example, to use an specific bundle), but it's actually working.

I will share the classes and a example of rule in a moment.

On 9 feb, 15:24, Thomas Eichstädt-Engelen <teich...@googlemail.com>
wrote:

Thomas Eichstädt-Engelen

unread,
Feb 9, 2012, 9:34:16 AM2/9/12
to ope...@googlegroups.com

I will share the classes and a example of rule in a moment.

perfect! Are you going to push them to your openHAB-clone?

Juanker Atina

unread,
Feb 9, 2012, 9:42:05 AM2/9/12
to openhab
I will push them to this clone: http://code.google.com/r/juankera-iot-opensense/

Give me a moment, i will post here when finish

On 9 feb, 15:34, Thomas Eichstädt-Engelen <teich...@googlemail.com>
wrote:

Juanker Atina

unread,
Feb 9, 2012, 12:30:57 PM2/9/12
to openhab
Done. You can get the code right here: http://code.google.com/r/juankera-platform-iot/

I've pushed the two examples above, pachube and open.sen.se.

Regards

Juanker Atina

unread,
Feb 9, 2012, 12:35:59 PM2/9/12
to openhab
And one example of rule. Using matches to catch events from different
items, and (the ugly part) sending each one to this right ID channel.

rule "send to open sense"
when
$event : StateEvent(itemName matches "Door.*|Presence.*|Temperature|
Light.*|Luminosity.*|Weather_Temperature")
then
// this is for Number items
if ($event.getItemName().equals("LuminosityRoom")) {
sendSense(new SenseEventBean("7403",
$event.getNewState().toString().replace(",","."), null));
}
// send the value only if changed
if($event.hasChanged()) {
if ($event.getItemName().equals("Temperature")) {
sendSense(new SenseEventBean("7483",
$event.getNewState().toString(), null));
}
}

Regards
Reply all
Reply to author
Forward
0 new messages