Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BSEC2 with BME680 on esp32

    BSEC2 with BME680 on esp32

    Zahari4i
    New Poster
    Hi community. I have a problem with the setup of this sensor. I’m using I2C communication with a basic setup. The only difference is that whenever the callback function is called I gather all data and send it to a local server using the MQTT protocol. Whenever the device is started, everything is working fine for the first 5-10 messages, then quite randomly I get a communication error (error -2), then again I get some correct readings and the error repeats. This cycle may repeat for 5-20 minutes and then I start getting only errors (-2). I have to mention that with the adafruit library everything is working just fine. I have a doubt that this may be somehow related to the delay induced by sending the MQTT message on each callback execution but I wasn’t able to find any useful information. Can you please help?
    Thanks.
    4 REPLIES 4

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Zahari4i,

    Could we know what version of BSEC library and example code do you use?

    Hi BSTRobin, thanks a lot for your help.  I'm using the BSEC2 library version 1.3.2200 and BME68X Sensor Library version 1.1.40407.

    Here is the code I'm using

    static uint16_t AVG_TIMEOUT = 1000;
    static uint16_t SEND_TIMEOUT = 2000;
    static uint8_t MAX_MEAN_COUNT = 50;
    static const String MQTT_TYPE = "bme680";

    static uint64_t lastMessageTimestamp = 0;
    static uint8_t avgCount = 0;
    static float meanTemperature = 20;
    static float meanHumidity = 50;

    bool BME680_ready = false;

    void checkBsecStatus(Bsec2 bsec);

    void newDataCallback(const bme68xData data, const bsecOutputs outputs, Bsec2 bsec);

    static Bsec2 envSensor;


    static String errorMessage = "";
    void BME680_setup(JsonObject config) {
    uint16_t sendTimeout = config["sendTimeout"].as<unsigned int>();
    uint8_t maxMeanCount = config["maxMeanCount"].as<unsigned int>();

    if (!sendTimeout || !maxMeanCount) {
    return;
    }

    SEND_TIMEOUT = sendTimeout ? sendTimeout : SEND_TIMEOUT;
    MAX_MEAN_COUNT = maxMeanCount ? maxMeanCount : MAX_MEAN_COUNT;

    bsecSensor sensorList[] = {
    BSEC_OUTPUT_RAW_TEMPERATURE,
    BSEC_OUTPUT_RAW_PRESSURE,
    BSEC_OUTPUT_RAW_HUMIDITY,
    BSEC_OUTPUT_RAW_GAS,
    BSEC_OUTPUT_IAQ,
    BSEC_OUTPUT_STATIC_IAQ,
    BSEC_OUTPUT_CO2_EQUIVALENT,
    BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
    BSEC_OUTPUT_STABILIZATION_STATUS,
    BSEC_OUTPUT_RUN_IN_STATUS
    };

    if (!envSensor.begin(BME68X_I2C_ADDR_HIGH, Wire)) {
    errorMessage = "Cannot begin BSEC!";
    checkBsecStatus(envSensor);
    return;
    }

    if (!envSensor.updateSubscription(sensorList, ARRAY_LEN(sensorList), BSEC_SAMPLE_RATE_LP)) {
    errorMessage = "Cannot update subscription!";
    checkBsecStatus(envSensor);
    return;
    }

    envSensor.attachCallback(newDataCallback);

    BME680_ready = true;
    }

    void BME680_loop(uint64_t timeStamp) {
    if (errorMessage.length() > 0) {
    MQTT_sendErrorLog(errorMessage);
    errorMessage = "";
    }
    if (!BME680_ready) {
    return;
    }

    if (!envSensor.run()) {
    checkBsecStatus(envSensor);
    }
    }

    void newDataCallback(const bme68xData data, const bsecOutputs outputs, Bsec2 bsec) {
    if (!outputs.nOutputs) {
    return;
    }

    DynamicJsonDocument sendData = MQTT_getDefaultValues(512, MQTT_TYPE);

    for (uint8_t i = 0; i < outputs.nOutputs; i++) {
    const bsecData output = outputs.output[i];
    switch (output.sensor_id) {
    case BSEC_OUTPUT_IAQ:
    sendData["data"]["iaq"] = output.signal;
    sendData["data"]["iaq_acc"] = output.accuracy;
    break;
    case BSEC_OUTPUT_STATIC_IAQ:
    sendData["data"]["s_iaq"] = output.signal;
    break;
    case BSEC_OUTPUT_RAW_TEMPERATURE:
    sendData["data"]["t"] = output.signal;
    meanTemperature = avgCount == 0 ? output.signal : (meanTemperature * avgCount + output.signal) / (avgCount + 1);
    sendData["data"]["mT"] = meanTemperature;
    break;
    case BSEC_OUTPUT_RAW_PRESSURE:
    sendData["data"]["p"] = output.signal;
    break;
    case BSEC_OUTPUT_RAW_HUMIDITY:
    sendData["data"]["h"] = output.signal;
    meanHumidity = avgCount == 0 ? output.signal : (meanHumidity * avgCount + output.signal) / (avgCount + 1);
    sendData["data"]["mH"] = meanHumidity;
    break;
    case BSEC_OUTPUT_RAW_GAS:
    sendData["data"]["g"] = output.signal;
    break;
    case BSEC_OUTPUT_CO2_EQUIVALENT:
    sendData["data"]["co2_eq"] = output.signal;
    break;
    case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT:
    sendData["data"]["br_voc"] = output.signal;
    break;
    case BSEC_OUTPUT_STABILIZATION_STATUS:
    break;
    case BSEC_OUTPUT_RUN_IN_STATUS:
    break;
    default:
    break;
    }
    }

    if (avgCount < MAX_MEAN_COUNT) {
    avgCount++;
    }

    MQTT_publish(JSON_getJsonString(sendData));
    }

    void checkBsecStatus(Bsec2 bsec) {
    if (bsec.status < BSEC_OK) {
    USE_SERIAL.println("BSEC error code : " + String(bsec.status));
    errorMessage = "BSEC error code : " + String(bsec.status);
    } else if (bsec.status > BSEC_OK) {
    USE_SERIAL.println("BSEC warning code : " + String(bsec.status));
    }

    if (bsec.sensor.status < BME68X_OK) {
    USE_SERIAL.println("BME68X error code : " + String(bsec.sensor.status));
    errorMessage = "BME68X error code : " + String(bsec.sensor.status);
    } else if (bsec.sensor.status > BME68X_OK) {
    USE_SERIAL.println("BME68X warning code : " + String(bsec.sensor.status));
    }
    }

    In few words: this code is a part of a bigger project. The device is configured (void BME680_setup(JsonObject config)) if needed and the loop void BME680_loop(uint64_t timeStamp) is called in the main loop, but will only execute if the device was correctly set up (bool BME680_ready). In the callback function I gather all the needed data and send it as JSON string through MQTT. If there is an error during setup or in loop, the error will also be sent through MQTT.

    As I wrote in my original message, whenever the device starts it gets correctly configured and messages are sent. After awhile I start getting errors from the BME library (?) randomly, and after some more time I get only error messages.

    I have to say that from the time I started this thread I reverted to the adafruit library and the sensor worked flawlessly to this day. So I believe the sensor itself works correctly.

    Thank you.

    Hey @BSTRobin any ideas? Thanks.

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Zahari4i,

    Callbacks must be executed precisely. For example, if you use configuration file "generic_33v_3s_4d", you need to ensure that the callback is accurately executed by 3S, execute BSEC code in the callback first, and then perform other tasks.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist