MQTT / Arduino / OpenHAB - newbie advice on sensor data structure

1,522 views
Skip to first unread message

Scott K

unread,
May 19, 2014, 12:43:57 PM5/19/14
to ope...@googlegroups.com
Hello,
I'm slowly learning about MQTT.  I have a gateway Arduino that will (hopefully) communicate with OpenHAB running on a RPi.  Mosquitto would also be running on the same RPi.  This Arduino has some sensor data like this:

Kitchen, temperature
Garage, temperature
Garage, door_open_status

There's a Arduino MQTT library, and the example usage is pretty simple:

  if (client.connect("arduinoClient"))
    client.publish("myTopicName","hello world");

I'm unsure about how to structure the topics and the data.  Can I have topics like this:

Topic_Kitchen_Temp
Topic_Garage_Temp
Topic_Garage_Door

On the OpenHAB side of things, does that make sense that each sensor value is its own topic and gets updated at different times during the day?
Message has been deleted

Scott K

unread,
May 19, 2014, 1:25:35 PM5/19/14
to ope...@googlegroups.com
Follow up question - it seems that all message content is a string of characters on the Arduino library.  When it gets through the broker and into OpenHAB, does OpenHAB have to convert strings to int/bool/float based on the topic name?  So if the topic is "xxxx_temperature", it should convert the payload string to float.  If the topic name is "xxxx_door_open_status", it should be a bool?

I'm still really new to this.  Can you point me in the right direction with how OpenHAB interprets MQTT messages, and where the massaging of the messages happen?

Ben Jones

unread,
May 19, 2014, 4:06:01 PM5/19/14
to ope...@googlegroups.com
Have you had a look at https://github.com/openhab/openhab/wiki/MQTT-Binding?

I would have a separate topic for each openHAB item. A good way to organise things might be;

    /openhab/room/device

E.g.

    /openhab/garage/door
    /openhab/garage/temp
    /openhab/garage/humidity
    /openhab/kitchen/temp
    etc...

The MQTT binding will attempt to convert the message payload to the specified openHAB item type as best it can. So if you receive "12.34" for a Number item, it will automatically convert it. For something like a garage door you will probably want to specify a Contact item in openHAB. In that case you would need to be publishing "OPEN" or "CLOSED" as your payload.

If you can't control what is being published you can use transformation services to transform the raw message received on the MQTT topic to something openHAB can parse. For example I have the following;

Switch     Alarm_Burglar      "Burglar Alarm"      <alarm>      (Alarm)        { mqtt="<[mosquitto:/openhab/piface/out/1:state:JS(gpio_to_switch.js)]" }
Switch     Alarm_Fire            "Fire Alarm"             <fire>           (Alarm)        { mqtt="<[mosquitto:/openhab/piface/out/2:state:JS(gpio_to_switch.js)]" }

And in my /configurations/transform folder I have a file gpio_to_switch.js;

if (input == "1") { 
    "ON"; 
} else { 
    "OFF"; 

There are a number of transform services available, regex, XLST, and JS.

Hope this helps.

Jan-Piet Mens

unread,
May 20, 2014, 2:20:39 AM5/20/14
to ope...@googlegroups.com
> I would have a separate topic for each openHAB item. A good way to organise
> things might be;
>
> /openhab/room/device

^
This is a rather 'religious' topic ;-), but there's absolutely no reason
for the leading slash in topic names. Actually, technically, it's an
empty component, so you're better off dropping it. But as I said: it's a
'religious' thing; some do, others don't.

-JP

Ben Jones

unread,
May 20, 2014, 7:21:47 AM5/20/14
to ope...@googlegroups.com
But /openhab/room/device just looks nice... ;) Think it is from my Linux usage - everything starts with a /.

But as JP quite rightly says, there is no need for the leading /. Entirely up to you how you structure your topics.

Although quite clearly you will offend one of either myself of JP depending on which way you go...;)

Scott K

unread,
May 20, 2014, 9:09:58 AM5/20/14
to ope...@googlegroups.com
Thanks for the detailed explination Ben.  That makes sense.
Reply all
Reply to author
Forward
0 new messages