Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    BME680 ESP32 longer sleep time

    BME680 ESP32 longer sleep time

    sagar448
    Member

    Hello,

    So been trying to increase the sleep time of the esp32 to 20minutes. I wrote an algorithm to trick the sensor into recieving measurement triggers at 3 second intervals it worked on ESP-IDF but for some reason it is not working on Arduino.

    I use a marker to calibrate it, the accuracy reaches 3 but as soon as I remove the marker, the air quality starts going down and then it just stays at 25 and the accuracy goes down to 2. There is no way that my room has 25 air quality index because its very hot. I also have an off the shelf sensor at home that shows the airquality to be 109. So clearly there is an issue here.

    Only when im  breathing on the sensor the air quality index would increase and accuracy would go to 3 but then as soon as I move away it would drop. Seems like a sensitivity issue? But this algorithm works perfectly fine on ESP-IDF. 

    Below is the code please advise.

     


    #include <Arduino.h>
    #include <EEPROM.h>
    #include "cJSON.h"


    //-----------------BME libraries and functions---------------
    #include <bsec.h>
    const uint8_t bsec_config_iaq[] = {
    #include "config/generic_33v_3s_4d/bsec_iaq.txt"
    };


    Bsec sensor;

    RTC_DATA_ATTR uint8_t sensor_state[BSEC_MAX_STATE_BLOB_SIZE] = {0};
    RTC_DATA_ATTR static uintmax_t timestampDS = 0;
    int timeToSleepAdd = 3*1000;

    bsec_virtual_sensor_t sensor_list[5] = {
    BSEC_OUTPUT_RAW_PRESSURE,
    BSEC_OUTPUT_STATIC_IAQ,
    BSEC_OUTPUT_CO2_EQUIVALENT,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
    };

    int64_t GetTimestamp() {
    int64_t timeret;
    if (timestampDS == 0) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    timeret = (tv.tv_sec * 1000LL) + tv.tv_usec / 1000LL;
    timestampDS = timeret;
    return timeret;
    } else {
    timestampDS = timestampDS + timeToSleepAdd;
    return timestampDS;

    }
    }

    bool CheckSensor() {
    if (sensor.status < BSEC_OK) {
    Serial.println("BSEC error, status");
    return false;;
    } else if (sensor.status > BSEC_OK) {
    Serial.println("BSEC warning, status ");
    }

    if (sensor.bme680Status < BME680_OK) {
    Serial.println("Sensor error, bme680_status ");
    return false;
    } else if (sensor.bme680Status > BME680_OK) {
    Serial.println("Sensor warning, status ");
    }
    return true;
    }

    //-----------------BME libraries and functions---------------

     

    //-----------------Sleep and Timeout settings----------------
    #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
    #define TIME_TO_SLEEP 1200
    //-----------------Sleep and Timeout settings----------------

     

    void setup() {
    EEPROM.begin(BSEC_MAX_STATE_BLOB_SIZE + 1);
    Serial.begin(115200);
    Wire.begin(13, 16);


    //-------------BME Setup----------------------
    sensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
    sensor.setConfig(bsec_config_iaq);

    loadState();

    sensor.updateSubscription(sensor_list, 5, BSEC_SAMPLE_RATE_LP);

    sensor.status = BSEC_OK;

    cJSON *root = cJSON_CreateObject();
    if (sensor.run(GetTimestamp())) {
    cJSON_AddItemToObject(root, "name", cJSON_CreateString(devName));
    cJSON_AddNumberToObject(root, "temperature", sensor.temperature);
    cJSON_AddNumberToObject(root, "humidity", sensor.humidity + 3);
    cJSON_AddNumberToObject(root, "pressure", sensor.pressure / 1000);
    cJSON_AddNumberToObject(root, "co2", sensor.co2Equivalent);
    cJSON_AddNumberToObject(root, "airquality", sensor.staticIaq);
    cJSON_AddNumberToObject(root, "accuracy", sensor.staticIaqAccuracy);
    dataToSend = cJSON_PrintUnformatted(root);
    updateState();
    }
    //-------------BME Setup----------------------


    cJSON_Delete(root);
    free(dataToSend);
    }

    void loop() {

    }

     

    void updateState(void)
    {
    sensor.getState(sensor_state);
    for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE ; i++) {
    EEPROM.write(i + 1, sensor_state[i]);
    }
    EEPROM.write(0, BSEC_MAX_STATE_BLOB_SIZE);
    EEPROM.commit();

    }

    void loadState(void)
    {
    if (EEPROM.read(0) == BSEC_MAX_STATE_BLOB_SIZE) {
    for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) {
    sensor_state[i] = EEPROM.read(i + 1);
    }
    sensor.setState(sensor_state);
    } else {
    for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE + 1; i++)
    EEPROM.write(i, 0);

    EEPROM.commit();
    }
    }


    void gotoSleep() {
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
    esp_deep_sleep_start();
    }

    6 REPLIES 6

    So the 3s interval works fine with both arduino and the esp idf platform. So is there no way to sleep for 20minutes? The timestamp way I wrote above to "spoof" the timestamp, would that not work? 

    As I said before, It works for esp-idf but not for arduino, why is that?

    Vincent
    Community Moderator
    Community Moderator

    We are currently support only 3s and 300s time interval. 

    Other interval is not guranteed by our testing result. 

    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