How to get data from a C Programm to OpenHAB?

Hallo,

i’m using a C programm to generate data. This data i want to put in OpenHAB but I don’t know which binding i can use for this purpose…? Maybe I can put the data first in a sql database und OpenHAB reads the sql? Or is there another possibility?

Best Wishes

The MQTT binding seems to be a very popular choice as a general purpose mechanism for briding the gap between openHAB and other systems. I guess you could integrate your C code with an MQTT client library of sorts, e.g. http://www.eclipse.org/paho/files/mqttdoc/Cclient/.

The above would probably be a good setup if your C program is running standalone and producing data on it’s own.

If, on the other hand, your C program only generates data when executed (i.e. sort of polling or reading data from somewhere) it could be that using the Exec binding to execute your program is a simpler way. In this case, openHAB will execute your program regularly to poll for data.

I opted for Go in order to do a similar integration of my SDM630 smart meter. Instead of MQTT I simply create a HTTP server that offers JSON endpoints. In OpenHAB, I’m using the HTTP plugin and a JSON transform to query the server:

Number Power_L1 "Strombezug L1 [%.1f W]" <power> (Power, Power_Chart) { http="<[http://192.168.1.44:8080/last:60000:JS(SDM630GetL1Power.js)]" }

I did similar things in C, mostly by using libmicrohttpd to embed a HTTP server directly into the binary.

HTH,
-Mathias

I use /rest api to read and set states of the items.
In PHP:
function sendCommand($item, $data) {
$url = “http://localhost:8080/rest/items/” . $item;
$options = array(
‘http’ => array(
‘header’ => “Content-type: text/plain\r\n”,
‘method’ => ‘POST’,
‘content’ => $data //http_build_query($data),),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}’

Maybe you will be able to do something similar with C and a proper library…

Thanks the replies.

With the c programm i get data from my CAN Bus (can4linux). What I’m doing with the c programm is to receive messages and decode the data. Maybe there is another way which includes CAN in a way?

I can use the CAN Bus as a device in linux…

Isn’t there another binding for my problem?

I made a simple C++ hello world program to read and write some items in my installation using the REST api. I haven’t worked with REST before and I thought this would be a nice challenge. Actually it is quite simple to use REST.

Code is here: https://github.com/aviborg/hello-world