meshtastic / firmware

Meshtastic device firmware
https://meshtastic.org
GNU General Public License v3.0
3.37k stars 825 forks source link

[Bug]: BME680 - saving state of the IAQ calculation fails #2586

Closed rpsainio closed 5 months ago

rpsainio commented 1 year ago

Category

Other

Hardware

T-Beam

Firmware Version

2.1.17.7ca2e81

Description

This is a TBeam with the latest FW installed (not updated) with web installer. Once the initialization is over and the status is 3, FW is supposed to write the state of the BSEC calculation on persistent storage so that after next reset/reboot the starting of IAQ calculation goes faster. Unfortunately it seems that this does not succeed. To verify this I made a reset afterwards and logs show that state file is not present.

Besides there seems to a typeconversion error in BME680 state update every 21600000 minutes

Relevant log output

DEBUG | 16:45:46 159618 [EnvironmentTelemetryModule] BME680 state update IAQ accuracy 3 >= 3
INFO  | 16:45:46 159618 [EnvironmentTelemetryModule] BME680 state write to /prefs/bsec.dat.
[159618722][E][vfs_api.cpp:105] open(): /littlefs/prefs/bsec.dat.tmp does not exist, no permits for creation
[159618723][E][vfs_api.cpp:137] rename(): /prefs/bsec.dat.tmp does not exists
ERROR | 16:45:46 159618 [EnvironmentTelemetryModule] Error: can't rename new state file
.................
DEBUG | 16:55:46 160218 [EnvironmentTelemetryModule] BME680 state update every 21600000 minutes
INFO  | 16:55:46 160218 [EnvironmentTelemetryModule] BME680 state write to /prefs/bsec.dat.
[160218756][E][vfs_api.cpp:105] open(): /littlefs/prefs/bsec.dat.tmp does not exist, no permits for creation
[160218757][E][vfs_api.cpp:137] rename(): /prefs/bsec.dat.tmp does not exists
ERROR | 16:55:46 160218 [EnvironmentTelemetryModule] Error: can't rename new state file

.................reset

INFO  | ??:??:?? 12 [EnvironmentTelemetryModule] Environment Telemetry: Initializing
[ 12687][E][vfs_api.cpp:105] open(): /littlefs/prefs/bsec.dat does not exist, no permits for creation
INFO  | ??:??:?? 12 [EnvironmentTelemetryModule] No BME680 state found (File: /prefs/bsec.dat).
INFO  | ??:??:?? 12 [EnvironmentTelemetryModule] Init sensor: BME680 with the BSEC Library version 2.4.0.0
INFO  | ??:??:?? 12 [EnvironmentTelemetryModule] Opened BME680 sensor on default i2c bus
D
rpsainio commented 1 year ago

@caveman99 So, I had finally time to look into this and I got it working by commenting out like following in https://github.com/meshtastic/firmware/blob/master/src/modules/Telemetry/Sensor/BME680Sensor.cpp#L104

  if (update) {
        bme680.getState(bsecState);
  //      std::string filenameTmp = bsecConfigFileName;
  //      filenameTmp += ".tmp";
        auto file = FSCom.open(bsecConfigFileName, FILE_O_WRITE);
        if (file) {
            LOG_INFO("%s state write to %s.\n", sensorName, bsecConfigFileName);
            file.write((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE);
            file.flush();
            file.close();
            // brief window of risk here ;-)
  //          if (FSCom.exists(bsecConfigFileName) && !FSCom.remove(bsecConfigFileName)) {
  //              LOG_WARN("Can't remove old state file\n");
  //          }
  //          if (!renameFile(filenameTmp.c_str(), bsecConfigFileName)) {
  //              LOG_ERROR("Error: can't rename new state file\n");
  //          }

        } else {
            LOG_INFO("Can't write %s state (File: %s).\n", sensorName, bsecConfigFileName);
        }
    }

Seems that you initially thought writing first to temp, delete original and rename temp to original. Unfortunately filenameTmp is not used for opening the file for writing rather the original file is written to. I guess you copied and pasted this from NodeDB handling and then thought that something going wrong here when writing is not so disastrous and cut the corners.

Another Topic is that you wait until "First state update when IAQ accuracy is >= 3" - you might consider lowering it to 2 as the startup time is then faster. Still, matter of taste.