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 (??)