Getting a C code output to MQTT

I’m currently using Bosch BSEC C library on a BME680.
https://github.com/alexh-name/bsec_bme680_linux
https://www.bosch-sensortec.com/bst/products/all_products/bsec

And I need help to get the data to MQTT to home assistant. That I believe would be the easiest way to get data to HASS.

Before you ask why not the use the BME680 sensor component. It’s a way more sophisticated implementation made by the manufacturer that calibrates itself that hopefully will give more meaningful results for air quality.
People did their best to get to work here but unfortunately, Bosch doesn’t offer its library in python.

The code right now basically output this:

$ ./bsec_bme680
2017-12-27 18:47:21,[IAQ (1)]: 33.96,[T degC]: 19.61,[H %rH]: 46.41,[P hPa]: 983.39,[G Ohms]: 540924.00,[S]: 0

I thought about 3 options:

  1. Code the MQTT on the bsec_bme680.c. Like how I would do for python. However, I have no knowledge of how is done in C.
    https://github.com/alexh-name/bsec_bme680_linux/blob/master/bsec_bme680.c

  2. Save the output as a .csv and mqtt every new line with some python script.

  3. Save the output as a .csv and use NODE RED to parse and MQTT.

Any ideas or examples of how is done?

Can you not run that program using command_line and dump output to mqtt?

The answer to 1. is to use the mosquitto_dev package, which installs libmosquitto

If all you are doing is posting messages, it should be straightforward.

But you might be able to publish data from your existing app using a pipe directly to mosquitto_pub. This is an example from the man page

       Send parsed electricity usage data from a Current Cost meter, reading
       from stdin with one line/reading as one message:

       ·   read_cc128.pl | mosquitto_pub -t sensors/cc128 -l
1 Like

That’s a great idea. I will give it a try.

The mosquitto_pub works :slight_smile: however I’m not sure if I can make it send as JSON to be parsed by Home Assistant. Couldn’t find on the mosquitto_pub documentation.

I think it might be easy to adapt the example to this code

You don’t necessarily have to convert to JSON prior to publishing to MQTT for HA to consume. You can always parse the text as-is. Here is a sample code that parses individual values from the text.

{% set bme680_output = "2017-12-27 18:47:21,[IAQ (1)]: 33.96,[T degC]: 19.61,[H %rH]: 46.41,[P hPa]: 983.39,[G Ohms]: 540924.00,[S]: 0" %}

IAQ: {{ bme680_output.split(',')[1].split(':')[1] }}
Temp: {{ bme680_output.split(',')[2].split(':')[1] }}
Humidity: {{ bme680_output.split(',')[3].split(':')[1] }}
PhPa: {{ bme680_output.split(',')[4].split(':')[1] }}
G: {{ bme680_output.split(',')[5].split(':')[1] }}
S: {{ bme680_output.split(',')[6].split(':')[1] }}

That code prints:

IAQ:  33.96
Temp:  19.61
Humidity:  46.41
PhPa:  983.39
G:  540924.00
S:  0

Sorry for the noob but where would that code example go?

If I change the printf from the code, wouldn’t it be easier?

printf(“%d-%02d-%02d %02d:%02d:%02d,”, tm.tm_year + 1900,tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); /* localtime */
printf(“[IAQ (%d)]: %.2f”, iaq_accuracy, iaq);
printf(“,[T degC]: %.2f,[H %%rH]: %.2f,[P hPa]: %.2f”, temperature,
humidity,pressure / 100);
printf(“,[G Ohms]: %.0f”, gas);
printf(“,[S]: %d”, bsec_status);

I could remove the timestamp, and get something like {“IAQ”: “33.96”, “Temp”: “19.61”…}

It works :smile:
I changed the printf to

printf("{\"IAQ Accuracy\": \"%d\",\"IAQ\":\"%.2f\"", iaq_accuracy, iaq);
printf(",\"Temperature\": \"%.2f\",\"Humidity\": \"%.2f\",\"Pressure\": \"%.2f\"", temperature,
humidity,pressure / 100);

printf(“,"Gas": "%.0f"”, gas);
printf(“,"Status": "%d"}”, bsec_status);

Now the really boring part yaml :frowning:

I still wonder about your code, probably is more elegant than my ugly json.

Thanks for the help!

I didn’t see that C code where it was generating the output… glad you did it. The printf works just as good!

Once you have the valid json, it is extremely easy to parse individual values - just have an mqtt sensor, and access using value_json.temperature…etc

Here is a sample mqtt sensor code that reads the mqtt value https://github.com/skalavala/smarthome/blob/master/packages/batteries.yaml#L171-L179

Thanks!
I will write a little guide on the project section!

1 Like

You just make sure the output of your program is a string formatted as JSON.

Yes, luckily it had a friendly print output part that I added the {" " : " "}.
This is how it looks
home/pizero/bme680 {"IAQ_Accuracy": "1","IAQ":"25.00","Temperature": "25.19","Humidity": "44.71","Pressure": "994.80","Gas": "226105","Status": "0"}

Maybe someday I will try to get on the C code :slight_smile:

Excellent thread and may rescue me as I have been a bit stupid! I assumed the BME680 component would allow access to a remote Rpi (or similar) via IP Address! I have some experience of using MQTT as currently using it to monitor heat pump metrics but is there a simple guide on how to get the BME680 connected to a remote Rpi to the HA system?

I am impressed with this sensor’s capabilities though. Pretty awesome for something so small and relatively inexpensive. I now need a way to get the sensor away from the CPU as the temp is showing about 7c too high.