IOTdev send/connect to openHAB instance

Hello,

I’m trying to send the data from a co2 sensor to openhab (and show it in openhab),
this works for tago (mqtt) as such:

or waylay (rest):

But i would like to use openhab.

So i guess basically something has to be setup on openhab to listen to the data sent from the iot device,
the easiest seems via REST (as mosquitto is needed otherwise?). the iot device has built-in wifi.

From the documentation:

  • there is a rest api from openhab : docs/configuration/restdocs.html
  • i guess i also have to create a ‘thing’ with an ‘http’ binding?

Suggestions are welcome on how to proceed.

The best implementation over all would be to use MQTT. The device publishes the readings and OH subscribes. This is the more efficient and responsive approach. Yes, you would still need Mosquitto for this.

Correct, with MQTT you’d install the MQTT binding and configure it to subscribe to the messages published by the device. Link that to Items and you use your Items in the rest of OH.

This tells us nothing. It’s the protocol/API that matters.

Use this if you want your device to push data into OH by having it call REST API endpoints on OH.

Use this if you want OH to poll the device by calling a REST API endpoint provided by the device itself.

Thank you

I understand MQTT is more lightweight and such the preferred option, but as i gather the implementation on the device is also in plaintext (port 1883), while the REST implementation is over https in this case; so i would prefer that one to start with.

As i understand it PUSH based from the device via REST to OH, not pulled based (as the device is on a LAN for example), so i would have to use the REST api from openhab.

Based on REST push subscriptions - #3 by mladams922
i guess i have to use /events/states/{connectionId} ?

If you happen to know a howto or video with a similar example, always welcome;
i’ll keep on searching

You have two choices.

  1. Your device pushes the data to OH.

  2. OH pulls the data from your device.

For 1, your device will need to send an HTTP POST call on OH with the proper authentication.

For 2, you will have to configure openHAB with the HTTP binding to send an HTTP GET request to your device periodically and parse the result.

You will implement 1 or 2, not both.

If you want to use REST because you think it will be easier I’m afraid you are out of luck. Both HTTP and MQTT will be equally challenging in this case.

Based on the documentation of controlco2.space,
the device should push to the data to OH.

As this is running on the esp32:

The messages seem to have the format
{
“co2”: 655,
“timestamp”: 1623…6654
}
(unix timestamp i guess)

In which case the device will have to be able to issue an HTTP PUT or POST command with either basic authentication or an authentication token with the “message” as the body using plain/text as the encoding.

The config screen shot doesn’t appear to let you choose all of that.

you are right, in the waylay setup they use as part of uri

/api/v1/e05f1b76-fe35-4790-8566-056789a53291/CO2-ontvanger?secret=a03AvpE163s2U8MXOp7gg06sIqzh4dEQ21HGxoVVbu0%3D

from the documentation of OH i don’t think it is possible to pass it along like that?

Do not mistake OH’s REST API from this device’s REST API. That looks like the part of the URL OH would call (or a browser) in order to poll for and fetch the data. It’s a pull instead of a push.

If this device supports pushing to some other systems REST API, it would allow you to specify the entire URL. This device cannot dictate the API of some other system. It has to conform to that other systems API.

ok, it is a push from the device to OH, but if it doesn’t conform with the REST API of OH, then i can’t use that, tnx. Is there another way (without extra middleware), to receive in OH the pushed REST data? (some kind of listening binding for the post requests coming from the device?)

This is frustrating because you have a fundamental misunderstanding about how REST works and what push/pull means. Given this MQTT is going to be way easier to implement. And it sets you up to support all sorts of stuff because MQTT is the lingua franca of home automation and IoT world.

But if not running Mosquitto is that important that you want to do it the hard way:

  1. Go read up some basic tutorials on how REST APIs work in general. The concepts, data paths, etc. The most important concept you must understand is that it is the client that initiates all the actions.

  2. Decide if OH is going to pull the data from the device or the device is going to push the data to OH. It appears that the device cannot support a push at all so there’s your answer. OH needs to pull the data.

  3. Set up OH to poll the device and pull the data periodically. And as I said above

you will have to configure openHAB with the HTTP binding to send an HTTP GET request to your device periodically and parse the result.