Raspberry Pi, Arduino, Sensors and the data they produce

openHAB Success

Since the last blog entry, I have been consumed with building a working environment, and building additional sensors.  I am currently modifying the environment with those additional sensors.

One sensor was particularly difficult for me. It was not really the sensor, nor openHAB, but the way I went about studying the how-to-do-it pages. I’ll explain, just to show how hard you can make a task, even though it is relatively simple.

I must have tried ten different examples of code for motion sensors. As you may recall from earlier blogs, I build a MySensors.org Temp/humidity sensor, and a couple of motion detectors, with the idea to place them outside near the gates to my yard.  That way I could tell if the gate had been opened and when.

Next I tested it with a cable attached and the Arduino IDE and serial monitor open, to ensure the right outputs were being transmitted out of the unit.

Next I went to the openHAB environment on my Raspberry Pi, and check the logs for events, and sure enough, I was getting the data through the gateway into the environment.  Woot! Woot!

Then, as mentioned above, I went to several sites with openHAB configurations, and copied bits of code, and added them to my environment [demo.items, demo.sitemap, demo.rules, etc.]

No matter how many times I tried, I was just not getting the desired results! Over the course of three nights, I came into my workshop and tried different bits and combinations, all to no avail! I had to step away and play with some other devices and other toys for a night, then I got back to it.

I submitted questions to three different forums, and also to the other guys who write in this blog, hoping that they would either point out my problem, or via discussing the problems, I would come up with the answer.

Two things happened shortly after that. First, one of my friends sent back a bit of code I had sent to the email group, and said “is not the answer in this code” which I had probably looked at at least ten times previously.  BOOM, he was on target, I had tried so hard, I overlooked what was right in front of me!!!!

Armed with that code, I figured out immediately the answers that had plagued me.

So on to the Motion Sensor, as I was still reading but not comprehending  again.  Four things make up the setup for a motion detector (I am using the simple one from http://www.mpja.com [ –> item number 31227SC].

You can review the build instructions here:  http://www.mysensors.org/build/motion

You do need to understand the MySensors API to know how to integrate into openHAB:


The reason you need to know this is that is how you identify your unit / units is all on that page.  The gateway receives both the startup and subsequent data transmissions.

Sample transmission:

2016-01-16 01:22:13 – Arduino state updated to 0;0;3;0;9;read: 10-10-0 s=1,c=1,t=16,pt=0,l=1,sg=0:0

Now, you have several steps to configuring the environment. You will recall we build out the items, the sitemap, and the rules. We will update these, and then add in some icons and customize a file that depicts differing states called a map file.

I continue to use a hashmap approach we found during the last effort, so add this entry to the mapping section of the demo.rules file:

“10;1;” ->”basementMot01″,
“basementMot01” -> “10;1;”,

Remember I said you had to read and know the API? the 10;1; depicts the sensor Node ID and the child_node of the sensor. Now go to the rules section of the demo.rules and modify the ones you have for temp and humidity and watch that you get this formatted correctly:

if (subType == V_TRIPPED){
println (“PIR : ” + sensorToItemsMap.get( nodeId + “;” + childId + “;”) + ” status: ” + Integer::parseInt(msg) )
if(Integer::parseInt(msg) == 1) {
postUpdate(sensorToItemsMap.get( nodeId + “;” + childId + “;”), “OPEN”)
println (“V_TRIPPED2: ” + subType + “;” + ” :OPEN”) }
if(Integer::parseInt(msg) == 0) {
postUpdate(sensorToItemsMap.get( nodeId + “;” + childId + “;”), “CLOSED”)
println (“V_TRIPPED2: ” + subType + “;” + ” :Closed”)

There are some debugging “println” entries for V_TRIPPED I left in for demonstration, as you can see these when you start up the environment in debugging mode.  If not today, sooner or later you will do that!

Now add this item to the demo.items file:

Contact basementMot01 “basementMot01 [MAP(en.map):%s]”

Moving on to the demo.sitemap file:

Text item=basementMot01 label=”Basement Sensor 1″

That is really it! Your system can do much more, but this will interpret the open and closed status of your motion sensor, and display an open or closed door icon on your screen:


Check out the icon for Basement Sensor 1, it is reflecting that fact that I am moving around while typing this blog entry.  There is a directoy within the openhab environment, full of icons, and you can add your own to this library:


Since I defined my item with the contact definition, it simply uses these to let you know their state. See that the file name, matches the state we set in the rules of OPEN and CLOSED, so contact-closed.png and contact-open.png match our design.

Next time we’ll discuss cleaning up the screen, making the user interface easier to read, and making better use of our screen real estate.


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

%d bloggers like this: