2023-01-18 14:51:48 -06:00
|
|
|
#include "BME680Sensor.h"
|
2023-01-18 08:56:47 -06:00
|
|
|
#include "../mesh/generated/meshtastic/telemetry.pb.h"
|
2023-04-12 10:04:29 +02:00
|
|
|
#include "FSCommon.h"
|
2022-02-26 20:52:22 -08:00
|
|
|
#include "TelemetrySensor.h"
|
2023-01-18 14:51:48 -06:00
|
|
|
#include "configuration.h"
|
2022-01-31 20:24:32 -06:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
BME680Sensor::BME680Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BME680, "BME680") {}
|
2022-01-31 20:24:32 -06:00
|
|
|
|
2023-01-18 14:51:48 -06:00
|
|
|
int32_t BME680Sensor::runOnce()
|
|
|
|
|
{
|
2023-04-14 10:38:57 +02:00
|
|
|
LOG_INFO("Init sensor: %s with the BSEC Library\n", sensorName);
|
2022-06-10 12:04:04 -05:00
|
|
|
if (!hasSensor()) {
|
2022-06-05 09:50:06 -05:00
|
|
|
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
|
|
|
|
|
}
|
2023-04-12 10:04:29 +02:00
|
|
|
bme680.begin(nodeTelemetrySensorsMap[sensorType], Wire);
|
2023-05-05 18:09:06 +02:00
|
|
|
#ifdef USE_BSEC2
|
|
|
|
|
if (bme680.status == BSEC_OK) {
|
|
|
|
|
#else
|
2023-04-12 10:04:29 +02:00
|
|
|
if (bme680.bsecStatus == BSEC_OK) {
|
2023-05-05 18:09:06 +02:00
|
|
|
#endif
|
|
|
|
|
bme680.setConfig(Default_H2S_NonH2S_config);
|
2023-04-12 10:04:29 +02:00
|
|
|
loadState();
|
|
|
|
|
bme680.updateSubscription(sensorList, 13, BSEC_SAMPLE_RATE_LP);
|
|
|
|
|
status = 1;
|
|
|
|
|
} else {
|
|
|
|
|
status = 0;
|
|
|
|
|
}
|
2022-11-06 10:37:14 +01:00
|
|
|
|
2023-05-05 18:09:06 +02:00
|
|
|
|
2022-11-06 10:37:14 +01:00
|
|
|
return initI2CSensor();
|
2022-01-31 20:24:32 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-18 14:51:48 -06:00
|
|
|
void BME680Sensor::setup() {}
|
2022-11-06 10:37:14 +01:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
bool BME680Sensor::getMetrics(meshtastic_Telemetry *measurement)
|
2023-01-18 14:51:48 -06:00
|
|
|
{
|
2023-04-12 10:04:29 +02:00
|
|
|
bme680.run();
|
2023-05-05 18:09:06 +02:00
|
|
|
#ifdef USE_BSEC2
|
|
|
|
|
measurement->variant.environment_metrics.temperature = bme680.getData(BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE).signal;
|
|
|
|
|
measurement->variant.environment_metrics.relative_humidity = bme680.getData(BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY).signal;
|
|
|
|
|
measurement->variant.environment_metrics.barometric_pressure = bme680.getData(BSEC_OUTPUT_RAW_PRESSURE).signal / 100.0F;
|
|
|
|
|
measurement->variant.environment_metrics.gas_resistance = bme680.getData(BSEC_OUTPUT_RAW_GAS).signal / 1000.0;
|
|
|
|
|
#else
|
2022-11-06 10:37:14 +01:00
|
|
|
measurement->variant.environment_metrics.temperature = bme680.temperature;
|
|
|
|
|
measurement->variant.environment_metrics.relative_humidity = bme680.humidity;
|
|
|
|
|
measurement->variant.environment_metrics.barometric_pressure = bme680.pressure / 100.0F;
|
2023-04-12 10:04:29 +02:00
|
|
|
measurement->variant.environment_metrics.gas_resistance = bme680.gasResistance / 1000.0;
|
2023-05-05 18:09:06 +02:00
|
|
|
#endif
|
2023-04-12 10:04:29 +02:00
|
|
|
// Check if we need to save state to filesystem (every STATE_SAVE_PERIOD ms)
|
2023-04-14 10:38:57 +02:00
|
|
|
updateState();
|
2022-01-31 20:24:32 -06:00
|
|
|
return true;
|
2023-04-12 10:04:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BME680Sensor::loadState()
|
|
|
|
|
{
|
|
|
|
|
#ifdef FSCom
|
2023-04-14 10:38:57 +02:00
|
|
|
auto file = FSCom.open(bsecConfigFileName, FILE_O_READ);
|
|
|
|
|
if (file) {
|
2023-04-12 10:04:29 +02:00
|
|
|
file.read((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE);
|
|
|
|
|
file.close();
|
|
|
|
|
bme680.setState(bsecState);
|
2023-04-14 10:38:57 +02:00
|
|
|
LOG_INFO("%s state read from %s.\n", sensorName, bsecConfigFileName);
|
2023-04-12 10:04:29 +02:00
|
|
|
} else {
|
2023-04-14 10:38:57 +02:00
|
|
|
LOG_INFO("No %s state found (File: %s).\n", sensorName, bsecConfigFileName);
|
2023-04-12 10:04:29 +02:00
|
|
|
}
|
2023-04-14 10:38:57 +02:00
|
|
|
#else
|
|
|
|
|
LOG_ERROR("ERROR: Filesystem not implemented\n");
|
2023-04-12 10:04:29 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BME680Sensor::updateState()
|
|
|
|
|
{
|
|
|
|
|
#ifdef FSCom
|
|
|
|
|
bool update = false;
|
|
|
|
|
if (stateUpdateCounter == 0) {
|
|
|
|
|
/* First state update when IAQ accuracy is >= 3 */
|
2023-05-05 18:09:06 +02:00
|
|
|
#ifdef USE_BSEC2
|
|
|
|
|
accuracy = bme680.getData(BSEC_OUTPUT_IAQ).accuracy;
|
|
|
|
|
#else
|
|
|
|
|
accuracy = bme680.iaqAccuracy;
|
|
|
|
|
#endif
|
|
|
|
|
if (accuracy >= 3) {
|
|
|
|
|
LOG_DEBUG("%s state update IAQ accuracy %u >= 3\n", sensorName, accuracy);
|
2023-04-12 10:04:29 +02:00
|
|
|
update = true;
|
|
|
|
|
stateUpdateCounter++;
|
2023-04-14 10:38:57 +02:00
|
|
|
} else {
|
2023-05-05 18:09:06 +02:00
|
|
|
LOG_DEBUG("%s not updated, IAQ accuracy is %u >= 3\n", sensorName, accuracy);
|
2023-04-12 10:04:29 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* Update every STATE_SAVE_PERIOD minutes */
|
|
|
|
|
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
|
2023-04-14 10:38:57 +02:00
|
|
|
LOG_DEBUG("%s state update every %d minutes\n", sensorName, STATE_SAVE_PERIOD);
|
2023-04-12 10:04:29 +02:00
|
|
|
update = true;
|
|
|
|
|
stateUpdateCounter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
|
bme680.getState(bsecState);
|
2023-04-14 10:38:57 +02:00
|
|
|
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);
|
2023-04-12 10:04:29 +02:00
|
|
|
file.write((uint8_t *)&bsecState, BSEC_MAX_STATE_BLOB_SIZE);
|
|
|
|
|
file.flush();
|
|
|
|
|
file.close();
|
2023-04-14 10:38:57 +02:00
|
|
|
// 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);
|
2023-04-12 10:04:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 10:38:57 +02:00
|
|
|
#else
|
|
|
|
|
LOG_ERROR("ERROR: Filesystem not implemented\n");
|
2023-04-12 10:04:29 +02:00
|
|
|
#endif
|
2023-01-18 14:51:48 -06:00
|
|
|
}
|