add debug/info print

This commit is contained in:
Thomas Göttgens 2023-04-14 10:38:57 +02:00
parent ac40f77694
commit 1621fbb5ab
2 changed files with 28 additions and 8 deletions

View File

@ -8,7 +8,7 @@ BME680Sensor::BME680Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BM
int32_t BME680Sensor::runOnce() int32_t BME680Sensor::runOnce()
{ {
LOG_INFO("Init sensor: %s\n", sensorName); LOG_INFO("Init sensor: %s with the BSEC Library\n", sensorName);
if (!hasSensor()) { if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS; return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
} }
@ -34,23 +34,25 @@ bool BME680Sensor::getMetrics(meshtastic_Telemetry *measurement)
measurement->variant.environment_metrics.relative_humidity = bme680.humidity; measurement->variant.environment_metrics.relative_humidity = bme680.humidity;
measurement->variant.environment_metrics.barometric_pressure = bme680.pressure / 100.0F; measurement->variant.environment_metrics.barometric_pressure = bme680.pressure / 100.0F;
measurement->variant.environment_metrics.gas_resistance = bme680.gasResistance / 1000.0; measurement->variant.environment_metrics.gas_resistance = bme680.gasResistance / 1000.0;
updateState();
// Check if we need to save state to filesystem (every STATE_SAVE_PERIOD ms) // Check if we need to save state to filesystem (every STATE_SAVE_PERIOD ms)
updateState();
return true; return true;
} }
void BME680Sensor::loadState() void BME680Sensor::loadState()
{ {
#ifdef FSCom #ifdef FSCom
if (File file = FSCom.open(bsecConfigFileName, FILE_O_READ)) { auto file = FSCom.open(bsecConfigFileName, FILE_O_READ);
if (file) {
file.read((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE); file.read((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE);
file.close(); file.close();
bme680.setState(bsecState); bme680.setState(bsecState);
LOG_INFO("%s state read from %s.\n", sensorName, bsecConfigFileName);
} else { } else {
FSCom.remove(bsecConfigFileName); LOG_INFO("No %s state found (File: %s).\n", sensorName, bsecConfigFileName);
} }
#else
LOG_ERROR("ERROR: Filesystem not implemented\n");
#endif #endif
} }
@ -61,12 +63,16 @@ void BME680Sensor::updateState()
if (stateUpdateCounter == 0) { if (stateUpdateCounter == 0) {
/* First state update when IAQ accuracy is >= 3 */ /* First state update when IAQ accuracy is >= 3 */
if (bme680.iaqAccuracy >= 3) { if (bme680.iaqAccuracy >= 3) {
LOG_DEBUG("%s state update IAQ accuracy %u >= 3\n", sensorName, bme680.iaqAccuracy);
update = true; update = true;
stateUpdateCounter++; stateUpdateCounter++;
} else {
LOG_DEBUG("%s not updated, IAQ accuracy is %u >= 3\n", sensorName, bme680.iaqAccuracy);
} }
} else { } else {
/* Update every STATE_SAVE_PERIOD minutes */ /* Update every STATE_SAVE_PERIOD minutes */
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) { if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
LOG_DEBUG("%s state update every %d minutes\n", sensorName, STATE_SAVE_PERIOD);
update = true; update = true;
stateUpdateCounter++; stateUpdateCounter++;
} }
@ -74,11 +80,25 @@ void BME680Sensor::updateState()
if (update) { if (update) {
bme680.getState(bsecState); bme680.getState(bsecState);
if (File file = FSCom.open(bsecConfigFileName, FILE_O_WRITE)) { 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.write((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE);
file.flush(); file.flush();
file.close(); 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);
} }
} }
#else
LOG_ERROR("ERROR: Filesystem not implemented\n");
#endif #endif
} }

View File

@ -2,7 +2,7 @@
#include "TelemetrySensor.h" #include "TelemetrySensor.h"
#include <bsec.h> #include <bsec.h>
#define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // That's 6 hours worth of millis()
const uint8_t bsec_config_iaq[] = { const uint8_t bsec_config_iaq[] = {
#include <config/generic_33v_3s_4d/bsec_iaq.txt> #include <config/generic_33v_3s_4d/bsec_iaq.txt>