Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BME680 error after 12 hours

    jordan982
    Established Member

    BME680 error after 12 hours

    Good morning,
    i have implemented a temperature sensor with bme 680 and esp32. 
    The sketch I implemented seems to work correctly, but after about 12 hours it goes into error.
    This problem has been repeated for days and I can't understand why.
    Unfortunately I could not find the error code
    I am new to arduino and the code is probably not clean.
    To make sure it doesn't depend on other factors, I'm trying the same code, but without using the bme680 sensor. That is, I connect to wifi, publish a message on MQTT and go to sleep, all this repeated every 3 minutes
    Thank you

    This is my actual sketch:

    #include <Arduino.h>

    #include <ArduinoJson.h>
    #include "bsec.h"
    #include <PubSubClient.h>
    #include <WiFiClientSecure.h>

    #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
    #define TIME_TO_SLEEP 180 /* Time ESP32 will go to sleep (in seconds) */

    RTC_DATA_ATTR int bootCount = 0;


    const char *SSID = "XXXXXX";
    const char *PWD = "XXXXXXX";
    const char* mqtt_server = "XXXXX";
    const char* mqtt_user = "XXXXX";
    const char* mqtt_pwd = "XXXXX";

    WiFiClientSecure espclientSecure;
    WiFiClient espClient;
    PubSubClient client(espClient);
    unsigned long lastMsg = 0;
    char msg[MSG_BUFFER_SIZE];
    int value = 0;
    String output;

    StaticJsonDocument<500> jsonDocument;
    char buffer[500];

    // Create an object of the class Bsec
    Bsec iaqSensor;



    // env variable
    String temperature;
    String humidity;
    String pressure;
    String CO2;
    String gasResistance;
    String iaq;
    int i;
    void connectToWiFi() {
    Serial.print("Connecting to ");
    Serial.println(SSID);

    WiFi.begin(SSID, PWD);
    while (WiFi.status() != WL_CONNECTED) {
    if ( i == 15 ){ WiFi.reconnect();}
    if (i > 30){
    break;
    }
    delay(500); i++;
    }
    Serial.print("Connected. IP: ");
    Serial.println(WiFi.localIP());

    }


    void setup() {

    Serial.begin(9600);
    // Sensor setup
    Wire.begin();

    iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire);
    bsec_virtual_sensor_t sensorList[10] = {
    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_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
    };

    iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
    checkIaqSensorStatus();

    connectToWiFi();

    client.setServer(mqtt_server, 1883);

    }

    void loop() {

    if (!client.connected()) {
    connectToWiFi();
    delay(5000);
    reconnect();
    }


    setCpuFrequencyMhz(160);
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, "Nuovi dati letti, count: #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    iaqSensor.run();
    temperature = (String)iaqSensor.temperature;
    humidity = (String)iaqSensor.humidity;
    iaq = (String)iaqSensor.iaq;
    pressure = (String)(iaqSensor.pressure / 100);
    gasResistance = (String)iaqSensor.gasResistance;

    Serial.println("Umidità:"+(String)iaqSensor.humidity);
    Serial.println("Temperatura:"+(String)iaqSensor.temperature);
    Serial.println("IAQ:"+(String)iaqSensor.iaq);
    Serial.println("Pressure:"+(String)(iaqSensor.pressure/100));
    Serial.println("Gas Resistance:"+(String)iaqSensor.gasResistance);


    //PubSubClient::setBufferSize(500);
    setJsonValues(iaqSensor.temperature, iaqSensor.humidity, iaqSensor.gasResistance, iaqSensor.pressure, iaqSensor.iaq);
    size_t n = serializeJson(jsonDocument, buffer);
    client.publish("esp32/output", buffer, n);
    String messageTemperature = "Temperatura:"+(String)iaqSensor.temperature;
    bot.sendMessage(CHAT_ID, messageTemperature, "");
    delay(3000);
    client.disconnect();
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); // ESP32 wakes up every 5 seconds

    Serial.println("Going to light-sleep now");
    Serial.flush();
    setCpuFrequencyMhz(80);
    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
    btStop();

    esp_light_sleep_start();

    }

     


    // Helper function definitions
    void checkIaqSensorStatus(void)
    {
    if (iaqSensor.status != BSEC_OK) {
    if (iaqSensor.status < BSEC_OK) {
    output = "BSEC error code : " + String(iaqSensor.status);
    Serial.println(output);


    for (;;)
    errLeds(); /* Halt in case of failure */

    Serial.println("Restarting in 10 seconds");
    delay(10000);
    ESP.restart();
    } else {
    output = "BSEC warning code : " + String(iaqSensor.status);
    Serial.println(output);

    }
    }

    if (iaqSensor.bme680Status != BME680_OK) {
    if (iaqSensor.bme680Status < BME680_OK) {
    output = "BME680 error code : " + String(iaqSensor.bme680Status);
    Serial.println(output);

    for (;;)
    errLeds(); /* Halt in case of failure */
    Serial.println("Restarting in 10 seconds");
    delay(10000);
    ESP.restart();
    } else {
    output = "BME680 warning code : " + String(iaqSensor.bme680Status);
    Serial.println(output);

    }
    }

    void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Cameretta-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_pwd)) {
    Serial.println("connected");
    } else {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 5 seconds");

    // Wait 5 seconds before retrying
    delay(5000);
    }
    }
    }

    void add_json_object(char *tag, float value, char *unit) {
    JsonObject obj = jsonDocument.createNestedObject();
    obj["type"] = tag;
    obj["value"] = value;
    obj["unit"] = unit;
    }


    void setJsonValues(float temperature, float humidity, float gasResistance, float pressure, float iaq) {
    jsonDocument.clear();
    Serial.println("Get env");
    add_json_object("temperature", temperature, "°C");
    add_json_object("humidity", humidity, "%");
    add_json_object("gas", gasResistance, "ohm");
    add_json_object("pressure", pressure, "mBar");
    add_json_object("iaq", iaq, "IAQ");
    }

     

    void errLeds(void)
    {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    delay(200);
    }

    3 REPLIES 3
    zgg
    Established Member

    Re: BME680 error after 12 hours

    Hello

    It's not super clear what kind of error you are facing. Could you share more details on the error such as logs etc?

    jordan982
    Established Member

    Re: BME680 error after 12 hours

    The problem reported in the post appears to be solved.
    Now the problem is that the value of iaqAccuracy is always 0 even after 24 hours of operation?
    Can you help me?

    ckl
    Occasional Visitor

    Re: BME680 error after 12 hours

    It seems that the you set too much delay inside the main process. BSEC can only support ~6.25% tolerance for the sample rate which means if the system delay is over 0.18 second under LP mode, the BSEC act this measurement as the first measurement again.

    Accuracy is 0 during first run-in time which is 5min under LP mode. In this case, sensors are always in run-in time.

    Please use standard example code and no extra delay for the main process.

    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