Bi-Directional Communication in OH2 Binding

Hi there, I’ve recently started looking into OpenHAB 2 binding development because I want to setup my own home automation solution.

To support a custom device I want to develop my own binding, which communicates with the device (based on an ESP8266 module) over REST. Now I ask myself what is the recommended way to implement bidirectional communication.

openHAB 2 --> Device
Just use apache http client library in the DeviceThingHandler to send REST requests to the device? (i.e. to toggle a device LED)
openHAB 2 <-- Device
How should I receive REST calls in OpenHAB 2? (i.e. to receive state changes from the device)

Hello Jonas,

I think that the best solution should be to use a TCP/IP socket, otherwise in the direction Device -> openHAB2 you should use polling.

Regards.

Mirko

Ah, yes the eternal question. The simplest solution is to organize communication in a way that it is always one-directional. This of course implies polling.

For example, in your case of HTTP requests: do only from OpenHAB -> device. On OpenHAB use HTTP lib like you wanted and on the device run the esp8266web server. Send commands via HTTP requests, and then poll for status to see the outcome.

An alternative approach is for your ESP8266 module to communicate with openHAB via an MQTT broker, and either use the MQTT binding directly (eliminating the need for a specific binding), or implement your binding using the existing MQTT services (as is done in the MQTTitude/OwnTracks binding). Polling is eliminated, and you get a clean separation between your ESP8266 code and openHAB.

Interesting. Is there a common TCP/UDP service?

I imagine you could use the TCP/UDP Binding instead of the other choices, but there is a lot to be said for the advantages of using an MQTT broker + MQTT binding for this sort of integration.

What I meant was instead of everyone who wants a TCP/UDP server for their binding allocating an instance of Netty (or something equivalent) that there would be a central location from where to get a socket and then receive callbacks with data. But I realize, that’s not how this is usually done.