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.