Using BSEC 2 library for bme688 with Thingy53

I'm developing a custom project for thingy53. 

I'm trying to use the bsec2 library to use the bme688 in Thingy53, but there's no reference example anywhere.

With the library provided by bosch, the libalgobsec.a and bsec_interface.h and bsec_datatypes.h files, I succeed to compile together.

But I don't know how to program them including config files.

I find the example with bme680 which is integrated in Thingy91, the asset tracker.

However, that was meaningless because the bsec library version is different (bme680 -> bsec v1.4x / bme688 -> bsec v2.0x) and because of different version, the structure of library is also different.

I think the Thingy53's edge-impulse example uses bme688. But also, unlike Thiny52, Thingy53's source code is not distributed.

So, is there example that adopts bme688's bsec lib. v2.0x for the custom project?

If not, How could I integrate bsec v2.0x library to my custom project?

Here's my CMakeList.txt. I built my project based on Nordic_LBS example from NCS v2.0.0 (zephyr) with Thingy53.

#
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
cmake_minimum_required(VERSION 3.20.0)

set(PM_STATIC_YML_FILE
  ${CMAKE_CURRENT_SOURCE_DIR}/boards/pm_static_${BOARD}.yml
  )

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(THINGY53_AMG88XX_BLE_APP)

# NORDIC SDK APP START
target_sources(app PRIVATE src/main.c)

# Preinitialization related to Thingy:53 DFU
target_sources_ifdef(CONFIG_BOARD_THINGY53_NRF5340_CPUAPP app PRIVATE
  boards/thingy53.c
)
target_sources_ifdef(CONFIG_BOARD_THINGY53_NRF5340_CPUAPP_NS app PRIVATE
  boards/thingy53.c
)


# NORDIC SDK APP END
zephyr_library_include_directories(.)
zephyr_library_include_directories({ZEPHYR_BASE}/drivers/sensor/amg88xx)

# zephyr_library_sources(amg88xx.c)
# zephyr_library_sources_ifdef(CONFIG_AMG88XX_TRIGGER amg88xx_trigger.c)


#External library config
# The external static library that we are linking with does not know
# how to build for this platform so we export all the flags used in
# this zephyr build to the external build system.
#
# Other external build systems may be self-contained enough that they
# do not need any build information from zephyr. Or they may be
# incompatible with certain zephyr options and need them to be
# filtered out.
zephyr_get_include_directories_for_lang_as_string(       C includes)
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
zephyr_get_compile_definitions_for_lang_as_string(       C definitions)
zephyr_get_compile_options_for_lang_as_string(           C options)

set(external_project_cflags
  "${includes} ${definitions} ${options} ${system_includes}"
  )

include(ExternalProject)

# Add an external project to be able download and build the third
# party library. In this case downloading is not necessary as it has
# been committed to the repository.
set(lib_gt2_src_dir   ${CMAKE_CURRENT_SOURCE_DIR}/lib_gt2)
set(lib_gt2_build_dir ${CMAKE_CURRENT_BINARY_DIR}/lib_gt2)

set(LIB_GT2_LIB_DIR     ${lib_gt2_build_dir}/lib)
set(LIB_GT2_SOURCE_DIR  ${lib_gt2_build_dir}/src)
set(LIB_GT2_INCLUDE_DIR ${lib_gt2_src_dir}/include)

if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
# https://www.gnu.org/software/make/manual/html_node/MAKE-Variable.html
set(submake "$(MAKE)")
else() # Obviously no MAKEFLAGS. Let's hope a "make" can be found somewhere.
set(submake "make")
endif()

ExternalProject_Add(
  lib_gt2_grideye_project                 # Name for custom target
  PREFIX     ${lib_gt2_build_dir} # Root dir for entire project
  SOURCE_DIR ${lib_gt2_src_dir}
  BINARY_DIR ${lib_gt2_src_dir} # This particular build system is invoked from the root
  CONFIGURE_COMMAND ""    # Skip configuring the project, e.g. with autoconf
  BUILD_COMMAND
  ${submake}
  PREFIX=${lib_gt2_build_dir}
  CC=${CMAKE_C_COMPILER}
  AR=${CMAKE_AR}
  CFLAGS=${external_project_cflags}
  INSTALL_COMMAND ""      # This particular build system has no install command
  BUILD_BYPRODUCTS ${LIB_GT2_LIB_DIR}/lib_gt2.a
  BUILD_BYPRODUCTS ${LIB_GT2_SOURCE_DIR}/libalgobsec.a
  DEPENDS zephyr_interface
  )

# Create a wrapper CMake library that our app can link with
add_library(lib_gt2_grideye STATIC IMPORTED GLOBAL)
add_dependencies(
  lib_gt2_grideye
  lib_gt2_grideye_project
  )
set_target_properties(lib_gt2_grideye PROPERTIES IMPORTED_LOCATION              ${LIB_GT2_LIB_DIR}/lib_gt2.a)
# set_target_properties(lib_gt2_grideye PROPERTIES IMPORTED_LOCATION              ${LIB_GT2_SOURCE_DIR}/libalgobsec.a)
set_target_properties(lib_gt2_grideye PROPERTIES INTERFACE_INCLUDE_DIRECTORIES  ${LIB_GT2_INCLUDE_DIR})

# BSEC static library configuration
add_library(lib_bsec STATIC IMPORTED GLOBAL)
# add_dependencies(lib_bsec)
set_target_properties(lib_bsec PROPERTIES IMPORTED_LOCATION                     ${LIB_GT2_SOURCE_DIR}/libalgobsec.a)
set_target_properties(lib_bsec PROPERTIES INTERFACE_INCLUDE_DIRECTORIES         ${LIB_GT2_INCLUDE_DIR})

target_link_libraries(app PUBLIC lib_gt2_grideye)

Parents
  • Hi,
    I would like to bring an update to this thread which I believe can help the community with a ready reference example.
    I would like first to thank  which answer helped me with the .a lib integration in Zephyr, hereby I would like to share a full project source code with driver, library integrations in Zephyr and multiple testing samples and complete application.
    The answer is similar to the one I posted here which is a similar question, this one focus on Bsec2 which is also included in the project
    Gas resistance values from BME688 on Thingy:53

    here the project link https://github.com/HomeSmartMesh/sdk-hsm-thingy53

    some details

    • Mesh Network with OpenThread broadcasting UDP packets as client and running a sensors server.
    • C++ application with json inputs for config and outputs for sensors enables direct mapping to MQTT
    • RGB Led, Light color, Battery

    if only interested in BSec2 integration then it's possible to check the sample 08_bme688_bsec2
    Here an overview about the driver and library integration, more details on the project readme.

  • Awesome,  
    I have tried your provided samples and integrated the BME688 sample in my project, and it is working absolutely fine.
    First of all that's for helping many people like me.

    So now I am getting the different gas_res value every time. I want to convert the gas_res to IAQ.

    I have used your sample named bme688_bsec2, but facing a difficulty that bme688_server.h is not able to find the find json.hpp, I researched and found that I have to add the JSON for modern C++ provided by nlohmann, Is that true?

    If so, please help me to integrate that into my code.

    EDITED: Also error finding          #include "flash_settings_storage.h"

  • Hi,
    I'm glad this integration helped you. So for reading IAQ, you can refer to the sample "08_bme688_bsec2", all needed is integrating and working, the .a lib, the json hpp header.
    as explained in the readme https://github.com/HomeSmartMesh/sdk-hsm-thingy53#08_bme688_bsec2 you need to activate new flags.
    I'm using json to simplify configuration, and it is needed in my lib API, in my repo, yes the nlohmann's header is integrated by default in the include folder https://github.com/HomeSmartMesh/sdk-hsm-thingy53/tree/main/include but you could include it conditionally with a flag if needed.

    The easiest is first to make the sample run from my repo as it is, that will help you to easier compare my repo to your integration.

    For "flash_settings_storage.h" usage, that is used on the sample "20_sensors_server" https://github.com/HomeSmartMesh/sdk-hsm-thingy53#20_sensors_server also with a flag "CONFIG_APP_FLASH", those flags are important because in case you do not activate them, the code does not consume memory and create conflicts in your code.

    This is a very important point, without this subsystem, the bsec2 lib will not have persistence and will require new calibration on every power cycle. It is activated with the flag "CONFIG_APP_FLASH", also make sure to use recent commits (> c3cc8c5) because I fixed a conflict between thread and bsec2 that were using the same flash aderss.

    In general, and if you're interested in collaborating in this project, contributing, even by testing and giving feedback you can feel free to contact me.

Reply
  • Hi,
    I'm glad this integration helped you. So for reading IAQ, you can refer to the sample "08_bme688_bsec2", all needed is integrating and working, the .a lib, the json hpp header.
    as explained in the readme https://github.com/HomeSmartMesh/sdk-hsm-thingy53#08_bme688_bsec2 you need to activate new flags.
    I'm using json to simplify configuration, and it is needed in my lib API, in my repo, yes the nlohmann's header is integrated by default in the include folder https://github.com/HomeSmartMesh/sdk-hsm-thingy53/tree/main/include but you could include it conditionally with a flag if needed.

    The easiest is first to make the sample run from my repo as it is, that will help you to easier compare my repo to your integration.

    For "flash_settings_storage.h" usage, that is used on the sample "20_sensors_server" https://github.com/HomeSmartMesh/sdk-hsm-thingy53#20_sensors_server also with a flag "CONFIG_APP_FLASH", those flags are important because in case you do not activate them, the code does not consume memory and create conflicts in your code.

    This is a very important point, without this subsystem, the bsec2 lib will not have persistence and will require new calibration on every power cycle. It is activated with the flag "CONFIG_APP_FLASH", also make sure to use recent commits (> c3cc8c5) because I fixed a conflict between thread and bsec2 that were using the same flash aderss.

    In general, and if you're interested in collaborating in this project, contributing, even by testing and giving feedback you can feel free to contact me.

Children
  • Hi  

    The easiest is first to make the sample run from my repo as it is, that will help you to easier compare my repo to your integration.

    1) Thanks for the idea, I first use your sample to make the project working, working absolutely fine, except the IAQ value and CO2 value is not changing and constant at 50.0 and 500.0 respectively, even if I am blowing air in front of sensor.

    2) I have resolve all the include problem, I have observe by changing the name of main.cpp in your sample to main.c same errors appears as I am facing at my project. I need your help as my project is based on peripheral sample and having the main.c (not main.cpp). How can I use those server libraries in main.c? 

  • for 1) you have to be patient and give it some time, you can read the Bosch bm688 datasheet and application notes to how to calibrate the sensor on first use and after each startup without previous state.

    for 2) the library I designed is using json in its API so c++ is required, technically speaking 'bsec_interface.h' can be used in C, but you'll have to rewrite the whole server that links the bsec2 to the i2c sensor API. Even the Bosch example is Arduino (c++), but rewriting in C is possible, just needs more time.

    I recommend to get familiar with west and its CMakeLists.txt, otherwise it is not possible to just rename files, you have to adapt the associated CMakeLists.txt file.

Related