Architecture for interfacing with custom electronics

I would like to use various “homebrew” electronics with OpenHAB 2. Things like an analogue temperature sensor hanging off an Arduino, a PIR motion detector, 434 MHz RF transmitter, relays. I would like both to receive events from the device (motion, temperature change, etc), and to push commands from OpenHAB to the device.

In the naive approach I would develop a binding for each new homebrew project, and define a protocol for communicating between the binding and the device. I would however like to do a bit more up-front work, developing a generic protocol/binding/client library (see options below), and make it a whole lot easier to add new devices later. I hope someone with more experience in OpenHAB (2) could comment on what is the best architecture.

For example, I’ve thought of these options (feel free to skip reading if you know the answer):

Calling the OpenHAB 2 REST API directly from the microcontroller, and using that to update the state of the Items, and to accept commands. No binding is needed at all, things are defined in OpenHAB configuration. I could develop a client library for the API, so only a few lines of code would be needed for each new device. Drawbacks: have to hard-code the thing name, OpenHAB hostname, etc in the microcontroller firmware. Also have to define things in the OpenHAB configuration files (duplicated configuration work). It will be a pain to implement a server-push client in Arduino code, to deal with commands.

Connect the microcontroller to the PC which is running OpenHAB, via USB-serial, and invent a simple serial protocol which could work with most sensors / actuators. Write a binding for this protocol (call it something like “Serial Multifunction Device”, represent the serial connection with a bridge, and things for each sensor/actuator on the microcontroller). Drawbacks: needs USB / RS232 connection to PC.

Equip the microcontrollers with NICs and define a custom IP-based protocol which works with various sensors, etc, and develop a binding for that. So the binding and the device communicate over this protocol. It could be an HTTP-based protocol implemented with a servlet. Have generic sensor and actuator thing types. If using HTTP, it could also run a small server on the microcontroller, and accept callbacks for server push. Drawbacks: have to develop both client and server code for this protocol.

Sorry for the long post, thanks for any comments.

Have you looked into MQTT? This is pretty much what that’s designed for. In a way it’s almost an extension of the openHAB bus messages, just broadcast or received via a standardised protocol.

@tinkerfailure nice! I didn’t know of MQTT. Thanks, that will probably be the solution then :slight_smile:

I think your approaches sound fairly valid too though.

I used to have some simple temp sensors that reported the temp via http/GET requests every 5 minutes. But they ended up being slightly flakey, so I’ve moved away from that now.

I’ve not looked into it myself yet, but presumably it’s reasonably simple to run a MQTT client on an arduino. It then publishes any updates to sensors or pulse counters or whatever, and openHAB just listens out for those messages coming in.

Take a look at MySensors.Org. and you’ll find a complete eco system of sensors and an MQTT Gateway, all running off Arduino’s and integrate easily with OpenHAB.

MQTT should a good solution for these devices. Can also recommend the Moteino devices if you are looking for small Arduino based chips with radio communication.

Thanks for the suggestions! I will go with MQTT first, since I have a couple of Arduino Yun and Particle Photon, which are already networked. Good to know about MySensors.Org and Moteino! I’ll want to add more sensors, and these seem like easy ways to do it.

As for MQTT, I finally got it running. The instructions are on the forum (for future reference), e.g. https://groups.google.com/forum/#!topic/openhab/VJOWnZUO7I8 . However, it didn’t load the jars in addons dir automatically, so I had to load them on the OSGi command line. Perhaps it’s not loading because I’m running OpenHAB from eclipse.

install file:///home/fa2k/openhab2-master/git/openhab2/addons/org.openhab.io.transport.mqtt_1.8.0.201601030239.jar
install file:///home/fa2k/openhab2-master/git/openhab2/addons/org.openhab.binding.mqtt-1.8.0-SNAPSHOT.jar
start org.openhab.io.transport.mqtt
start org.openhab.binding.mqtt

I was then able to configure an item and have it send an MQTT message.

(edit:) How to configure to start the binding on every restart, when running in Eclipse: Go to Window, Preferences, Plug-in development, Target Platform. Edit openHAB 2 Target Platform. Choose Add, Directory, and navigate to the addons directory (e.g. ~/openhab2-master/git/openhab2/addons/). Finish the wizard, close preferences. Go to Run menu, Run configurations, and edit openHAB_Runtime. Go to plug-ins tab and check the mqtt binding and transport (search for mqtt). That worked for me :slight_smile: