diff --git a/platformio.ini b/platformio.ini
index ef0fef791..f13c368ab 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -141,8 +141,6 @@ lib_deps =
adafruit/Adafruit INA260 Library@1.5.3
# renovate: datasource=custom.pio depName=Adafruit INA219 packageName=adafruit/library/Adafruit INA219
adafruit/Adafruit INA219@1.2.3
- # renovate: datasource=custom.pio depName=Adafruit PM25 AQI Sensor packageName=adafruit/library/Adafruit PM25 AQI Sensor
- adafruit/Adafruit PM25 AQI Sensor@2.0.0
# renovate: datasource=custom.pio depName=Adafruit MPU6050 packageName=adafruit/library/Adafruit MPU6050
adafruit/Adafruit MPU6050@2.2.6
# renovate: datasource=custom.pio depName=Adafruit LIS3DH packageName=adafruit/library/Adafruit LIS3DH
@@ -208,4 +206,4 @@ lib_deps =
# renovate: datasource=custom.pio depName=Sensirion Core packageName=sensirion/library/Sensirion Core
sensirion/Sensirion Core@0.7.1
# renovate: datasource=custom.pio depName=Sensirion I2C SCD4x packageName=sensirion/library/Sensirion I2C SCD4x
- sensirion/Sensirion I2C SCD4x@1.1.0
+ sensirion/Sensirion I2C SCD4x@1.1.0
\ No newline at end of file
diff --git a/src/configuration.h b/src/configuration.h
index 81632c89e..ae810bf3d 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -199,6 +199,7 @@ along with this program. If not, see .
#define BQ27220_ADDR 0x55 // same address as TDECK_KB
#define BQ25896_ADDR 0x6B
#define LTR553ALS_ADDR 0x23
+#define SEN5X_ADDR 0x69
// -----------------------------------------------------------------------------
// ACCELEROMETER
diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h
index e46c6f623..4456373bd 100644
--- a/src/detect/ScanI2C.h
+++ b/src/detect/ScanI2C.h
@@ -80,6 +80,7 @@ class ScanI2C
LTR553ALS,
BHI260AP,
BMM150,
+ SEN5X,
DRV2605
} DeviceType;
diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp
index 9aef9defe..9a6979345 100644
--- a/src/detect/ScanI2CTwoWire.cpp
+++ b/src/detect/ScanI2CTwoWire.cpp
@@ -8,6 +8,7 @@
#endif
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
#include "meshUtils.h" // vformat
+
#endif
bool in_array(uint8_t *array, int size, uint8_t lookfor)
@@ -109,6 +110,42 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation
return value;
}
+/// for SEN5X detection
+// Note, this code needs to be called before setting the I2C bus speed
+// for the screen at high speed. The speed needs to be at 100kHz, otherwise
+// detection will not work
+String readSEN5xProductName(TwoWire* i2cBus, uint8_t address) {
+ uint8_t cmd[] = { 0xD0, 0x14 };
+ uint8_t response[48] = {0};
+
+ i2cBus->beginTransmission(address);
+ i2cBus->write(cmd, 2);
+ if (i2cBus->endTransmission() != 0) return "";
+
+ delay(20);
+ if (i2cBus->requestFrom(address, (uint8_t)48) != 48) return "";
+
+ for (int i = 0; i < 48 && i2cBus->available(); ++i) {
+ response[i] = i2cBus->read();
+ }
+
+ char productName[33] = {0};
+ int j = 0;
+ for (int i = 0; i < 48 && j < 32; i += 3) {
+ if (response[i] >= 32 && response[i] <= 126)
+ productName[j++] = response[i];
+ else
+ break;
+
+ if (response[i + 1] >= 32 && response[i + 1] <= 126)
+ productName[j++] = response[i + 1];
+ else
+ break;
+ }
+
+ return String(productName);
+}
+
#define SCAN_SIMPLE_CASE(ADDR, T, ...) \
case ADDR: \
logFoundDevice(__VA_ARGS__); \
@@ -494,21 +531,39 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
}
break;
- case ICM20948_ADDR: // same as BMX160_ADDR
+ case ICM20948_ADDR: // same as BMX160_ADDR and SEN5X_ADDR
case ICM20948_ADDR_ALT: // same as MPU6050_ADDR
+ // ICM20948 Register check
registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x00), 1);
if (registerValue == 0xEA) {
type = ICM20948;
logFoundDevice("ICM20948", (uint8_t)addr.address);
break;
- } else if (addr.address == BMX160_ADDR) {
- type = BMX160;
- logFoundDevice("BMX160", (uint8_t)addr.address);
- break;
} else {
- type = MPU6050;
- logFoundDevice("MPU6050", (uint8_t)addr.address);
- break;
+ String prod = "";
+ prod = readSEN5xProductName(i2cBus, addr.address);
+ if (prod.startsWith("SEN55")) {
+ type = SEN5X;
+ logFoundDevice("Sensirion SEN55", addr.address);
+ break;
+ } else if (prod.startsWith("SEN54")) {
+ type = SEN5X;
+ logFoundDevice("Sensirion SEN54", addr.address);
+ break;
+ } else if (prod.startsWith("SEN50")) {
+ type = SEN5X;
+ logFoundDevice("Sensirion SEN50", addr.address);
+ break;
+ }
+ if (addr.address == BMX160_ADDR) {
+ type = BMX160;
+ logFoundDevice("BMX160", (uint8_t)addr.address);
+ break;
+ } else {
+ type = MPU6050;
+ logFoundDevice("MPU6050", (uint8_t)addr.address);
+ break;
+ }
}
break;
diff --git a/src/main.cpp b/src/main.cpp
index 111709d07..4eaa872ab 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -531,6 +531,7 @@ void setup()
Wire.setSCL(I2C_SCL);
Wire.begin();
#elif defined(I2C_SDA) && !defined(ARCH_RP2040)
+ LOG_INFO("Starting Bus with (SDA) %d and (SCL) %d: ", I2C_SDA, I2C_SCL);
Wire.begin(I2C_SDA, I2C_SCL);
#elif defined(ARCH_PORTDUINO)
if (settingsStrings[i2cdev] != "") {
@@ -741,7 +742,7 @@ void setup()
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::RAK12035, meshtastic_TelemetrySensorType_RAK12035);
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::PCT2075, meshtastic_TelemetrySensorType_PCT2075);
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::SCD4X, meshtastic_TelemetrySensorType_SCD4X);
-
+ scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::SEN5X, meshtastic_TelemetrySensorType_SEN5X);
i2cScanner.reset();
#endif
diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp
index 0d405fa81..1b370c90c 100644
--- a/src/modules/Modules.cpp
+++ b/src/modules/Modules.cpp
@@ -231,11 +231,7 @@ void setupModules()
// TODO: How to improve this?
#if HAS_SENSOR && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
new EnvironmentTelemetryModule();
-#if __has_include("Adafruit_PM25AQI.h")
- if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first > 0) {
- new AirQualityTelemetryModule();
- }
-#endif
+ new AirQualityTelemetryModule();
#if !MESHTASTIC_EXCLUDE_HEALTH_TELEMETRY
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MAX30102].first > 0 ||
nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MLX90614].first > 0) {
diff --git a/src/modules/Telemetry/AirQualityTelemetry.cpp b/src/modules/Telemetry/AirQualityTelemetry.cpp
index 21a563b9d..8ecd72563 100644
--- a/src/modules/Telemetry/AirQualityTelemetry.cpp
+++ b/src/modules/Telemetry/AirQualityTelemetry.cpp
@@ -1,36 +1,56 @@
#include "configuration.h"
-#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include("Adafruit_PM25AQI.h")
+#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#include "../mesh/generated/meshtastic/telemetry.pb.h"
-#include "AirQualityTelemetry.h"
#include "Default.h"
+#include "AirQualityTelemetry.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "PowerFSM.h"
#include "RTC.h"
#include "Router.h"
-#include "detect/ScanI2CTwoWire.h"
+#include "UnitConversions.h"
+#include "graphics/SharedUIDisplay.h"
+#include "graphics/images.h"
#include "main.h"
+#include "sleep.h"
#include
-#ifndef PMSA003I_WARMUP_MS
-// from the PMSA003I datasheet:
-// "Stable data should be got at least 30 seconds after the sensor wakeup
-// from the sleep mode because of the fan’s performance."
-#define PMSA003I_WARMUP_MS 30000
-#endif
+// Sensor includes
+#include "Sensor/PMSA003ISensor.h"
+PMSA003ISensor pmsa003iSensor;
+
+#include "Sensor/SEN5XSensor.h"
+SEN5XSensor sen5xSensor;
+
+#include "graphics/ScreenFonts.h"
int32_t AirQualityTelemetryModule::runOnce()
{
+ if (sleepOnNextExecution == true) {
+ sleepOnNextExecution = false;
+ uint32_t nightyNightMs = Default::getConfiguredOrDefaultMs(moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs);
+ LOG_DEBUG("Sleeping for %ims, then awaking to send metrics again.", nightyNightMs);
+ doDeepSleep(nightyNightMs, true, false);
+ }
+
+ uint32_t result = UINT32_MAX;
+ uint32_t sen5xPendingForReady;
+
/*
Uncomment the preferences below if you want to use the module
without having to configure it from the PythonAPI or WebUI.
*/
// moduleConfig.telemetry.air_quality_enabled = 1;
+ // TODO there is no config in module_config.proto for air_quality_screen_enabled. Reusing environment one, although it should have its own
+ // moduleConfig.telemetry.environment_screen_enabled = 1;
+ // moduleConfig.telemetry.air_quality_interval = 15;
- if (!(moduleConfig.telemetry.air_quality_enabled)) {
+ if (!(moduleConfig.telemetry.air_quality_enabled || moduleConfig.telemetry.environment_screen_enabled ||
+ AIR_QUALITY_TELEMETRY_MODULE_ENABLE)) {
// If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it
return disable();
}
@@ -42,79 +62,189 @@ int32_t AirQualityTelemetryModule::runOnce()
if (moduleConfig.telemetry.air_quality_enabled) {
LOG_INFO("Air quality Telemetry: init");
-#ifdef PMSA003I_ENABLE_PIN
- // put the sensor to sleep on startup
- pinMode(PMSA003I_ENABLE_PIN, OUTPUT);
- digitalWrite(PMSA003I_ENABLE_PIN, LOW);
-#endif /* PMSA003I_ENABLE_PIN */
+ if (pmsa003iSensor.hasSensor())
+ result = pmsa003iSensor.runOnce();
- if (!aqi.begin_I2C()) {
-#ifndef I2C_NO_RESCAN
- LOG_WARN("Could not establish i2c connection to AQI sensor. Rescan");
- // rescan for late arriving sensors. AQI Module starts about 10 seconds into the boot so this is plenty.
- uint8_t i2caddr_scan[] = {PMSA0031_ADDR};
- uint8_t i2caddr_asize = 1;
- auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire());
-#if defined(I2C_SDA1)
- i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan, i2caddr_asize);
-#endif
- i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan, i2caddr_asize);
- auto found = i2cScanner->find(ScanI2C::DeviceType::PMSA0031);
- if (found.type != ScanI2C::DeviceType::NONE) {
- nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first = found.address.address;
- nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].second =
- i2cScanner->fetchI2CBus(found.address);
- return setStartDelay();
- }
-#endif
- return disable();
- }
- return setStartDelay();
+ if (sen5xSensor.hasSensor())
+ result = sen5xSensor.runOnce();
}
- return disable();
+
+ // it's possible to have this module enabled, only for displaying values on the screen.
+ // therefore, we should only enable the sensor loop if measurement is also enabled
+ return result == UINT32_MAX ? disable() : setStartDelay();
} else {
// if we somehow got to a second run of this module with measurement disabled, then just wait forever
- if (!moduleConfig.telemetry.air_quality_enabled)
- return disable();
-
- switch (state) {
-#ifdef PMSA003I_ENABLE_PIN
- case State::IDLE:
- // sensor is in standby; fire it up and sleep
- LOG_DEBUG("runOnce(): state = idle");
- digitalWrite(PMSA003I_ENABLE_PIN, HIGH);
- state = State::ACTIVE;
-
- return PMSA003I_WARMUP_MS;
-#endif /* PMSA003I_ENABLE_PIN */
- case State::ACTIVE:
- // sensor is already warmed up; grab telemetry and send it
- LOG_DEBUG("runOnce(): state = active");
-
- if (((lastSentToMesh == 0) ||
- !Throttle::isWithinTimespanMs(lastSentToMesh, Default::getConfiguredOrDefaultMsScaled(
- moduleConfig.telemetry.air_quality_interval,
- default_telemetry_broadcast_interval_secs, numOnlineNodes))) &&
- airTime->isTxAllowedChannelUtil(config.device.role != meshtastic_Config_DeviceConfig_Role_SENSOR) &&
- airTime->isTxAllowedAirUtil()) {
- sendTelemetry();
- lastSentToMesh = millis();
- } else if (service->isToPhoneQueueEmpty()) {
- // Just send to phone when it's not our time to send to mesh yet
- // Only send while queue is empty (phone assumed connected)
- sendTelemetry(NODENUM_BROADCAST, true);
- }
-
-#ifdef PMSA003I_ENABLE_PIN
- // put sensor back to sleep
- digitalWrite(PMSA003I_ENABLE_PIN, LOW);
- state = State::IDLE;
-#endif /* PMSA003I_ENABLE_PIN */
-
- return sendToPhoneIntervalMs;
- default:
+ if (!moduleConfig.telemetry.air_quality_enabled && !AIR_QUALITY_TELEMETRY_MODULE_ENABLE) {
return disable();
}
+
+ // Wake up the sensors that need it, before we need to take telemetry data
+ // TODO - Do it for SENSOR ROLE too?
+ if (((lastSentToMesh == 0) ||
+ (sen5xSensor.hasSensor() && !Throttle::isWithinTimespanMs(lastSentToMesh - SEN5X_WARMUP_MS_1, Default::getConfiguredOrDefaultMsScaled(
+ moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs, numOnlineNodes))) ||
+ (pmsa003iSensor.hasSensor() && !Throttle::isWithinTimespanMs(lastSentToMesh - PMSA003I_WARMUP_MS, Default::getConfiguredOrDefaultMsScaled(
+ moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs, numOnlineNodes)))) &&
+ airTime->isTxAllowedChannelUtil(config.device.role != meshtastic_Config_DeviceConfig_Role_SENSOR) &&
+ airTime->isTxAllowedAirUtil()) {
+
+ if (sen5xSensor.hasSensor() && !sen5xSensor.isActive())
+ return sen5xSensor.wakeUp();
+
+#ifdef PMSA003I_ENABLE_PIN
+ if (pmsa003iSensor.hasSensor() && !pmsa003iSensor.isActive())
+ return pmsa003iSensor.wakeUp();
+#endif /* PMSA003I_ENABLE_PIN */
+ }
+
+ // Check if sen5x is ready to return data, or if it needs more time because of the low concentration threshold
+ if (sen5xSensor.hasSensor() && sen5xSensor.isActive()) {
+ sen5xPendingForReady = sen5xSensor.pendingForReady();
+ LOG_DEBUG("SEN5X: Pending for ready %ums", sen5xPendingForReady);
+ if (sen5xPendingForReady > 0) {
+ return sen5xPendingForReady;
+ }
+ }
+ LOG_DEBUG("Checking if sending telemetry");
+
+ if (((lastSentToMesh == 0) ||
+ !Throttle::isWithinTimespanMs(lastSentToMesh, Default::getConfiguredOrDefaultMsScaled(
+ moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs, numOnlineNodes))) &&
+ airTime->isTxAllowedChannelUtil(config.device.role != meshtastic_Config_DeviceConfig_Role_SENSOR) &&
+ airTime->isTxAllowedAirUtil()) {
+ sendTelemetry();
+ lastSentToMesh = millis();
+ } else if (((lastSentToPhone == 0) || !Throttle::isWithinTimespanMs(lastSentToPhone, sendToPhoneIntervalMs)) &&
+ (service->isToPhoneQueueEmpty())) {
+ // Just send to phone when it's not our time to send to mesh yet
+ // Only send while queue is empty (phone assumed connected)
+ sendTelemetry(NODENUM_BROADCAST, true);
+ lastSentToPhone = millis();
+ }
+
+ // Send the sensor to idle ONLY if there is enough time to wake it up before the next reading cycle
+ // TODO - include conditions here for module timing
+#ifdef PMSA003I_ENABLE_PIN
+ if (pmsa003iSensor.hasSensor() && pmsa003iSensor.isActive()) {
+ if (PMSA003I_WARMUP_MS < Default::getConfiguredOrDefaultMsScaled(
+ moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs, numOnlineNodes)) {
+ LOG_DEBUG("PMSA003I: Disabling sensor until next period");
+ pmsa003iSensor.sleep();
+ } else {
+ LOG_DEBUG("PMSA003I: Sensor stays enabled due to warm up period");
+ }
+ }
+#endif /* PMSA003I_ENABLE_PIN */
+
+ if (sen5xSensor.hasSensor() && sen5xSensor.isActive()) {
+ if (SEN5X_WARMUP_MS_2 < Default::getConfiguredOrDefaultMsScaled(
+ moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs, numOnlineNodes)) {
+ LOG_DEBUG("SEN5X: Disabling sensor until next period");
+ sen5xSensor.idle();
+ } else {
+ LOG_DEBUG("SEN5X: Sensor stays enabled due to warm up period");
+ }
+ }
+ }
+ return min(sendToPhoneIntervalMs, result);
+}
+
+bool AirQualityTelemetryModule::wantUIFrame()
+{
+ return moduleConfig.telemetry.environment_screen_enabled;
+}
+
+void AirQualityTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
+{
+ // === Setup display ===
+ display->clear();
+ display->setFont(FONT_SMALL);
+ display->setTextAlignment(TEXT_ALIGN_LEFT);
+ int line = 1;
+
+ // === Set Title
+ const char *titleStr = (graphics::isHighResolution) ? "Environment" : "Env.";
+
+ // === Header ===
+ graphics::drawCommonHeader(display, x, y, titleStr);
+
+ // === Row spacing setup ===
+ const int rowHeight = FONT_HEIGHT_SMALL - 4;
+ int currentY = graphics::getTextPositions(display)[line++];
+
+ // === Show "No Telemetry" if no data available ===
+ if (!lastMeasurementPacket) {
+ display->drawString(x, currentY, "No Telemetry");
+ return;
+ }
+
+ // Decode the telemetry message from the latest received packet
+ const meshtastic_Data &p = lastMeasurementPacket->decoded;
+ meshtastic_Telemetry telemetry;
+ if (!pb_decode_from_bytes(p.payload.bytes, p.payload.size, &meshtastic_Telemetry_msg, &telemetry)) {
+ display->drawString(x, currentY, "No Telemetry");
+ return;
+ }
+
+ const auto &m = telemetry.variant.air_quality_metrics;
+
+ // Check if any telemetry field has valid data
+ bool hasAny = m.has_pm10_standard || m.has_pm25_standard || m.has_pm100_standard;
+
+ if (!hasAny) {
+ display->drawString(x, currentY, "No Telemetry");
+ return;
+ }
+
+ // === First line: Show sender name + time since received (left), and first metric (right) ===
+ const char *sender = getSenderShortName(*lastMeasurementPacket);
+ uint32_t agoSecs = service->GetTimeSinceMeshPacket(lastMeasurementPacket);
+ String agoStr = (agoSecs > 864000) ? "?"
+ : (agoSecs > 3600) ? String(agoSecs / 3600) + "h"
+ : (agoSecs > 60) ? String(agoSecs / 60) + "m"
+ : String(agoSecs) + "s";
+
+ String leftStr = String(sender) + " (" + agoStr + ")";
+ display->drawString(x, currentY, leftStr); // Left side: who and when
+
+ // === Collect sensor readings as label strings (no icons) ===
+ std::vector entries;
+
+ if (m.has_pm10_standard)
+ entries.push_back("PM1.0: " + String(m.pm10_standard) + "ug/m3");
+ if (m.has_pm25_standard)
+ entries.push_back("PM2.5: " + String(m.pm25_standard) + "ug/m3");
+ if (m.has_pm100_standard)
+ entries.push_back("PM10.0: " + String(m.pm100_standard) + "ug/m3");
+
+ // === Show first available metric on top-right of first line ===
+ if (!entries.empty()) {
+ String valueStr = entries.front();
+ int rightX = SCREEN_WIDTH - display->getStringWidth(valueStr);
+ display->drawString(rightX, currentY, valueStr);
+ entries.erase(entries.begin()); // Remove from queue
+ }
+
+ // === Advance to next line for remaining telemetry entries ===
+ currentY += rowHeight;
+
+ // === Draw remaining entries in 2-column format (left and right) ===
+ for (size_t i = 0; i < entries.size(); i += 2) {
+ // Left column
+ display->drawString(x, currentY, entries[i]);
+
+ // Right column if it exists
+ if (i + 1 < entries.size()) {
+ int rightX = SCREEN_WIDTH / 2;
+ display->drawString(rightX, currentY, entries[i + 1]);
+ }
+
+ currentY += rowHeight;
}
}
@@ -128,9 +258,10 @@ bool AirQualityTelemetryModule::handleReceivedProtobuf(const meshtastic_MeshPack
t->variant.air_quality_metrics.pm10_standard, t->variant.air_quality_metrics.pm25_standard,
t->variant.air_quality_metrics.pm100_standard);
- LOG_INFO(" | PM1.0(Environmental)=%i, PM2.5(Environmental)=%i, PM10.0(Environmental)=%i",
- t->variant.air_quality_metrics.pm10_environmental, t->variant.air_quality_metrics.pm25_environmental,
- t->variant.air_quality_metrics.pm100_environmental);
+ // TODO - Decide what to do with these
+ // LOG_INFO(" | PM1.0(Environmental)=%i, PM2.5(Environmental)=%i, PM10.0(Environmental)=%i",
+ // t->variant.air_quality_metrics.pm10_environmental, t->variant.air_quality_metrics.pm25_environmental,
+ // t->variant.air_quality_metrics.pm100_environmental);
#endif
// release previous packet before occupying a new spot
if (lastMeasurementPacket != nullptr)
@@ -144,35 +275,23 @@ bool AirQualityTelemetryModule::handleReceivedProtobuf(const meshtastic_MeshPack
bool AirQualityTelemetryModule::getAirQualityTelemetry(meshtastic_Telemetry *m)
{
- if (!aqi.read(&data)) {
- LOG_WARN("Skip send measurements. Could not read AQIn");
- return false;
- }
-
+ bool valid = false;
+ bool hasSensor = false;
m->time = getTime();
m->which_variant = meshtastic_Telemetry_air_quality_metrics_tag;
- m->variant.air_quality_metrics.has_pm10_standard = true;
- m->variant.air_quality_metrics.pm10_standard = data.pm10_standard;
- m->variant.air_quality_metrics.has_pm25_standard = true;
- m->variant.air_quality_metrics.pm25_standard = data.pm25_standard;
- m->variant.air_quality_metrics.has_pm100_standard = true;
- m->variant.air_quality_metrics.pm100_standard = data.pm100_standard;
+ m->variant.air_quality_metrics = meshtastic_AirQualityMetrics_init_zero;
- m->variant.air_quality_metrics.has_pm10_environmental = true;
- m->variant.air_quality_metrics.pm10_environmental = data.pm10_env;
- m->variant.air_quality_metrics.has_pm25_environmental = true;
- m->variant.air_quality_metrics.pm25_environmental = data.pm25_env;
- m->variant.air_quality_metrics.has_pm100_environmental = true;
- m->variant.air_quality_metrics.pm100_environmental = data.pm100_env;
+ if (pmsa003iSensor.hasSensor()) {
+ valid = valid || pmsa003iSensor.getMetrics(m);
+ hasSensor = true;
+ }
- LOG_INFO("Send: PM1.0(Standard)=%i, PM2.5(Standard)=%i, PM10.0(Standard)=%i", m->variant.air_quality_metrics.pm10_standard,
- m->variant.air_quality_metrics.pm25_standard, m->variant.air_quality_metrics.pm100_standard);
+ if (sen5xSensor.hasSensor()) {
+ valid = valid || sen5xSensor.getMetrics(m);
+ hasSensor = true;
+ }
- LOG_INFO(" | PM1.0(Environmental)=%i, PM2.5(Environmental)=%i, PM10.0(Environmental)=%i",
- m->variant.air_quality_metrics.pm10_environmental, m->variant.air_quality_metrics.pm25_environmental,
- m->variant.air_quality_metrics.pm100_environmental);
-
- return true;
+ return valid && hasSensor;
}
meshtastic_MeshPacket *AirQualityTelemetryModule::allocReply()
@@ -206,7 +325,21 @@ meshtastic_MeshPacket *AirQualityTelemetryModule::allocReply()
bool AirQualityTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
{
meshtastic_Telemetry m = meshtastic_Telemetry_init_zero;
+ m.which_variant = meshtastic_Telemetry_air_quality_metrics_tag;
+ m.time = getTime();
+
+ // TODO - if one sensor fails here, we will stop taking measurements from everything
+ // Can we do this in a smarter way, for instance checking the nodeTelemetrySensor map and making it dynamic?
+
if (getAirQualityTelemetry(&m)) {
+ LOG_INFO("Send: pm10_standard=%u, pm25_standard=%u, pm100_standard=%u",
+ m.variant.air_quality_metrics.pm10_standard, m.variant.air_quality_metrics.pm25_standard,
+ m.variant.air_quality_metrics.pm100_standard);
+ if (m.variant.air_quality_metrics.has_pm10_environmental)
+ LOG_INFO("pm10_environmental=%u, pm25_environmental=%u, pm100_environmental=%u",
+ m.variant.air_quality_metrics.pm10_environmental, m.variant.air_quality_metrics.pm25_environmental,
+ m.variant.air_quality_metrics.pm100_environmental);
+
meshtastic_MeshPacket *p = allocDataProtobuf(m);
p->to = dest;
p->decoded.want_response = false;
@@ -221,16 +354,54 @@ bool AirQualityTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
lastMeasurementPacket = packetPool.allocCopy(*p);
if (phoneOnly) {
- LOG_INFO("Send packet to phone");
+ LOG_INFO("Sending packet to phone");
service->sendToPhone(p);
} else {
- LOG_INFO("Send packet to mesh");
+ LOG_INFO("Sending packet to mesh");
service->sendToMesh(p, RX_SRC_LOCAL, true);
+
+ if (config.device.role == meshtastic_Config_DeviceConfig_Role_SENSOR && config.power.is_power_saving) {
+ meshtastic_ClientNotification *notification = clientNotificationPool.allocZeroed();
+ notification->level = meshtastic_LogRecord_Level_INFO;
+ notification->time = getValidTime(RTCQualityFromNet);
+ sprintf(notification->message, "Sending telemetry and sleeping for %us interval in a moment",
+ Default::getConfiguredOrDefaultMs(moduleConfig.telemetry.air_quality_interval,
+ default_telemetry_broadcast_interval_secs) /
+ 1000U);
+ service->sendClientNotification(notification);
+ sleepOnNextExecution = true;
+ LOG_DEBUG("Start next execution in 5s, then sleep");
+ setIntervalFromNow(FIVE_SECONDS_MS);
+ }
}
return true;
}
-
return false;
}
+AdminMessageHandleResult AirQualityTelemetryModule::handleAdminMessageForModule(const meshtastic_MeshPacket &mp,
+ meshtastic_AdminMessage *request,
+ meshtastic_AdminMessage *response)
+{
+ AdminMessageHandleResult result = AdminMessageHandleResult::NOT_HANDLED;
+#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR_EXTERNAL
+ if (pmsa003iSensor.hasSensor()) {
+ // TODO - Potentially implement an admin message to choose between pm_standard
+ // and pm_environmental. This could be configurable as it doesn't make sense so
+ // have both
+ result = pmsa003iSensor.handleAdminMessage(mp, request, response);
+ if (result != AdminMessageHandleResult::NOT_HANDLED)
+ return result;
+ }
+
+ if (sen5xSensor.hasSensor()) {
+ result = sen5xSensor.handleAdminMessage(mp, request, response);
+ if (result != AdminMessageHandleResult::NOT_HANDLED)
+ return result;
+ }
+
+#endif
+ return result;
+}
+
#endif
\ No newline at end of file
diff --git a/src/modules/Telemetry/AirQualityTelemetry.h b/src/modules/Telemetry/AirQualityTelemetry.h
index 0142ee686..8314c54bc 100644
--- a/src/modules/Telemetry/AirQualityTelemetry.h
+++ b/src/modules/Telemetry/AirQualityTelemetry.h
@@ -1,12 +1,18 @@
#include "configuration.h"
-#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include("Adafruit_PM25AQI.h")
+#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#pragma once
+
+#ifndef AIR_QUALITY_TELEMETRY_MODULE_ENABLE
+#define AIR_QUALITY_TELEMETRY_MODULE_ENABLE 0
+#endif
+
#include "../mesh/generated/meshtastic/telemetry.pb.h"
-#include "Adafruit_PM25AQI.h"
#include "NodeDB.h"
#include "ProtobufModule.h"
+#include
+#include
class AirQualityTelemetryModule : private concurrency::OSThread, public ProtobufModule
{
@@ -20,18 +26,15 @@ class AirQualityTelemetryModule : private concurrency::OSThread, public Protobuf
ProtobufModule("AirQualityTelemetry", meshtastic_PortNum_TELEMETRY_APP, &meshtastic_Telemetry_msg)
{
lastMeasurementPacket = nullptr;
- setIntervalFromNow(10 * 1000);
- aqi = Adafruit_PM25AQI();
nodeStatusObserver.observe(&nodeStatus->onNewStatus);
-
-#ifdef PMSA003I_ENABLE_PIN
- // the PMSA003I sensor uses about 300mW on its own; support powering it off when it's not actively taking
- // a reading
- state = State::IDLE;
-#else
- state = State::ACTIVE;
-#endif
+ setIntervalFromNow(10 * 1000);
}
+ virtual bool wantUIFrame() override;
+#if !HAS_SCREEN
+ void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
+#else
+ virtual void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) override;
+#endif
protected:
/** Called to handle a particular incoming message
@@ -49,19 +52,15 @@ class AirQualityTelemetryModule : private concurrency::OSThread, public Protobuf
*/
bool sendTelemetry(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
+ virtual AdminMessageHandleResult handleAdminMessageForModule(const meshtastic_MeshPacket &mp,
+ meshtastic_AdminMessage *request,
+ meshtastic_AdminMessage *response) override;
private:
- enum State {
- IDLE = 0,
- ACTIVE = 1,
- };
-
- State state;
- Adafruit_PM25AQI aqi;
- PM25_AQI_Data data = {0};
bool firstTime = true;
meshtastic_MeshPacket *lastMeasurementPacket;
uint32_t sendToPhoneIntervalMs = SECONDS_IN_MINUTE * 1000; // Send to phone every minute
uint32_t lastSentToMesh = 0;
+ uint32_t lastSentToPhone = 0;
};
#endif
\ No newline at end of file
diff --git a/src/modules/Telemetry/EnvironmentTelemetry.cpp b/src/modules/Telemetry/EnvironmentTelemetry.cpp
index 8926b171c..94c41a870 100644
--- a/src/modules/Telemetry/EnvironmentTelemetry.cpp
+++ b/src/modules/Telemetry/EnvironmentTelemetry.cpp
@@ -743,8 +743,6 @@ bool EnvironmentTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
LOG_INFO("Send: soil_temperature=%f, soil_moisture=%u", m.variant.environment_metrics.soil_temperature,
m.variant.environment_metrics.soil_moisture);
- sensor_read_error_count = 0;
-
meshtastic_MeshPacket *p = allocDataProtobuf(m);
p->to = dest;
p->decoded.want_response = false;
diff --git a/src/modules/Telemetry/EnvironmentTelemetry.h b/src/modules/Telemetry/EnvironmentTelemetry.h
index d70c063fc..ffbb229f0 100644
--- a/src/modules/Telemetry/EnvironmentTelemetry.h
+++ b/src/modules/Telemetry/EnvironmentTelemetry.h
@@ -62,7 +62,6 @@ class EnvironmentTelemetryModule : private concurrency::OSThread, public Protobu
uint32_t sendToPhoneIntervalMs = SECONDS_IN_MINUTE * 1000; // Send to phone every minute
uint32_t lastSentToMesh = 0;
uint32_t lastSentToPhone = 0;
- uint32_t sensor_read_error_count = 0;
};
#endif
\ No newline at end of file
diff --git a/src/modules/Telemetry/Sensor/PMSA003ISensor.cpp b/src/modules/Telemetry/Sensor/PMSA003ISensor.cpp
new file mode 100644
index 000000000..c83c3e01a
--- /dev/null
+++ b/src/modules/Telemetry/Sensor/PMSA003ISensor.cpp
@@ -0,0 +1,179 @@
+#include "configuration.h"
+
+#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
+
+#include "../mesh/generated/meshtastic/telemetry.pb.h"
+#include "PMSA003ISensor.h"
+#include "TelemetrySensor.h"
+
+#include
+
+PMSA003ISensor::PMSA003ISensor()
+ : TelemetrySensor(meshtastic_TelemetrySensorType_PMSA003I, "PMSA003I")
+{
+}
+
+void PMSA003ISensor::setup()
+{
+#ifdef PMSA003I_ENABLE_PIN
+ pinMode(PMSA003I_ENABLE_PIN, OUTPUT);
+#endif
+}
+
+bool PMSA003ISensor::restoreClock(uint32_t currentClock){
+#ifdef PMSA003I_I2C_CLOCK_SPEED
+ if (currentClock != PMSA003I_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Restoring I2C clock to %uHz", currentClock);
+ return bus->setClock(currentClock);
+ }
+ return true;
+#endif
+}
+
+int32_t PMSA003ISensor::runOnce()
+{
+ LOG_INFO("Init sensor: %s", sensorName);
+
+ if (!hasSensor()) {
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+
+ bus = nodeTelemetrySensorsMap[sensorType].second;
+ address = (uint8_t)nodeTelemetrySensorsMap[sensorType].first;
+
+#ifdef PMSA003I_I2C_CLOCK_SPEED
+ uint32_t currentClock;
+ currentClock = bus->getClock();
+ if (currentClock != PMSA003I_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Changing I2C clock to %u", PMSA003I_I2C_CLOCK_SPEED);
+ bus->setClock(PMSA003I_I2C_CLOCK_SPEED);
+ }
+#endif
+
+ bus->beginTransmission(address);
+ if (bus->endTransmission() != 0) {
+ LOG_WARN("PMSA003I not found on I2C at 0x12");
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+
+#ifdef PMSA003I_I2C_CLOCK_SPEED
+ restoreClock(currentClock);
+#endif
+
+ status = 1;
+ LOG_INFO("PMSA003I Enabled");
+
+ return initI2CSensor();
+}
+
+bool PMSA003ISensor::getMetrics(meshtastic_Telemetry *measurement)
+{
+ if(!isActive()){
+ LOG_WARN("PMSA003I is not active");
+ return false;
+ }
+
+#ifdef PMSA003I_I2C_CLOCK_SPEED
+ uint32_t currentClock;
+ currentClock = bus->getClock();
+ if (currentClock != PMSA003I_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Changing I2C clock to %u", PMSA003I_I2C_CLOCK_SPEED);
+ bus->setClock(PMSA003I_I2C_CLOCK_SPEED);
+ }
+#endif
+
+ bus->requestFrom(address, PMSA003I_FRAME_LENGTH);
+ if (bus->available() < PMSA003I_FRAME_LENGTH) {
+ LOG_WARN("PMSA003I read failed: incomplete data (%d bytes)", bus->available());
+ return false;
+ }
+
+#ifdef PMSA003I_I2C_CLOCK_SPEED
+ restoreClock(currentClock);
+#endif
+
+ for (uint8_t i = 0; i < PMSA003I_FRAME_LENGTH; i++) {
+ buffer[i] = bus->read();
+ }
+
+ if (buffer[0] != 0x42 || buffer[1] != 0x4D) {
+ LOG_WARN("PMSA003I frame header invalid: 0x%02X 0x%02X", buffer[0], buffer[1]);
+ return false;
+ }
+
+ auto read16 = [](uint8_t *data, uint8_t idx) -> uint16_t {
+ return (data[idx] << 8) | data[idx + 1];
+ };
+
+ computedChecksum = 0;
+
+ for (uint8_t i = 0; i < PMSA003I_FRAME_LENGTH - 2; i++) {
+ computedChecksum += buffer[i];
+ }
+ receivedChecksum = read16(buffer, PMSA003I_FRAME_LENGTH - 2);
+
+ if (computedChecksum != receivedChecksum) {
+ LOG_WARN("PMSA003I checksum failed: computed 0x%04X, received 0x%04X", computedChecksum, receivedChecksum);
+ return false;
+ }
+
+ measurement->variant.air_quality_metrics.has_pm10_standard = true;
+ measurement->variant.air_quality_metrics.pm10_standard = read16(buffer, 4);
+
+ measurement->variant.air_quality_metrics.has_pm25_standard = true;
+ measurement->variant.air_quality_metrics.pm25_standard = read16(buffer, 6);
+
+ measurement->variant.air_quality_metrics.has_pm100_standard = true;
+ measurement->variant.air_quality_metrics.pm100_standard = read16(buffer, 8);
+
+ measurement->variant.air_quality_metrics.has_pm10_environmental = true;
+ measurement->variant.air_quality_metrics.pm10_environmental = read16(buffer, 10);
+
+ measurement->variant.air_quality_metrics.has_pm25_environmental = true;
+ measurement->variant.air_quality_metrics.pm25_environmental = read16(buffer, 12);
+
+ measurement->variant.air_quality_metrics.has_pm100_environmental = true;
+ measurement->variant.air_quality_metrics.pm100_environmental = read16(buffer, 14);
+
+ measurement->variant.air_quality_metrics.has_particles_03um = true;
+ measurement->variant.air_quality_metrics.particles_03um = read16(buffer, 16);
+
+ measurement->variant.air_quality_metrics.has_particles_05um = true;
+ measurement->variant.air_quality_metrics.particles_05um = read16(buffer, 18);
+
+ measurement->variant.air_quality_metrics.has_particles_10um = true;
+ measurement->variant.air_quality_metrics.particles_10um = read16(buffer, 20);
+
+ measurement->variant.air_quality_metrics.has_particles_25um = true;
+ measurement->variant.air_quality_metrics.particles_25um = read16(buffer, 22);
+
+ measurement->variant.air_quality_metrics.has_particles_50um = true;
+ measurement->variant.air_quality_metrics.particles_50um = read16(buffer, 24);
+
+ measurement->variant.air_quality_metrics.has_particles_100um = true;
+ measurement->variant.air_quality_metrics.particles_100um = read16(buffer, 26);
+
+ return true;
+}
+
+bool PMSA003ISensor::isActive()
+{
+ return state == State::ACTIVE;
+}
+
+#ifdef PMSA003I_ENABLE_PIN
+void PMSA003ISensor::sleep()
+{
+ digitalWrite(PMSA003I_ENABLE_PIN, LOW);
+ state = State::IDLE;
+}
+
+uint32_t PMSA003ISensor::wakeUp()
+{
+ digitalWrite(PMSA003I_ENABLE_PIN, HIGH);
+ state = State::ACTIVE;
+ return PMSA003I_WARMUP_MS;
+}
+#endif
+
+#endif
diff --git a/src/modules/Telemetry/Sensor/PMSA003ISensor.h b/src/modules/Telemetry/Sensor/PMSA003ISensor.h
new file mode 100644
index 000000000..35a4df735
--- /dev/null
+++ b/src/modules/Telemetry/Sensor/PMSA003ISensor.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include "TelemetrySensor.h"
+
+#define PMSA003I_I2C_CLOCK_SPEED 100000
+#define PMSA003I_FRAME_LENGTH 32
+#define PMSA003I_WARMUP_MS 30000
+
+class PMSA003ISensor : public TelemetrySensor
+{
+public:
+ PMSA003ISensor();
+ virtual void setup() override;
+ virtual int32_t runOnce() override;
+ virtual bool restoreClock(uint32_t currentClock);
+ virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
+ virtual bool isActive();
+
+#ifdef PMSA003I_ENABLE_PIN
+ void sleep();
+ uint32_t wakeUp();
+#endif
+
+private:
+ enum class State { IDLE, ACTIVE };
+ State state = State::ACTIVE;
+ TwoWire * bus;
+ uint8_t address;
+
+ uint16_t computedChecksum = 0;
+ uint16_t receivedChecksum = 0;
+
+ uint8_t buffer[PMSA003I_FRAME_LENGTH];
+};
diff --git a/src/modules/Telemetry/Sensor/SEN5XSensor.cpp b/src/modules/Telemetry/Sensor/SEN5XSensor.cpp
new file mode 100644
index 000000000..6e1ed89c9
--- /dev/null
+++ b/src/modules/Telemetry/Sensor/SEN5XSensor.cpp
@@ -0,0 +1,1001 @@
+#include "configuration.h"
+
+#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
+
+#include "../mesh/generated/meshtastic/telemetry.pb.h"
+#include "SEN5XSensor.h"
+#include "TelemetrySensor.h"
+#include "FSCommon.h"
+#include "SPILock.h"
+#include "SafeFile.h"
+#include
+#include
+#include // FLT_MAX
+
+SEN5XSensor::SEN5XSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_SEN5X, "SEN5X") {}
+
+bool SEN5XSensor::restoreClock(uint32_t currentClock){
+#ifdef SEN5X_I2C_CLOCK_SPEED
+ if (currentClock != SEN5X_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Restoring I2C clock to %uHz", currentClock);
+ return bus->setClock(currentClock);
+ }
+ return true;
+#endif
+}
+
+bool SEN5XSensor::getVersion()
+{
+ if (!sendCommand(SEN5X_GET_FIRMWARE_VERSION)){
+ LOG_ERROR("SEN5X: Error sending version command");
+ return false;
+ }
+ delay(20); // From Sensirion Datasheet
+
+ uint8_t versionBuffer[12];
+ size_t charNumber = readBuffer(&versionBuffer[0], 3);
+ if (charNumber == 0) {
+ LOG_ERROR("SEN5X: Error getting data ready flag value");
+ return false;
+ }
+
+ firmwareVer = versionBuffer[0] + (versionBuffer[1] / 10);
+ hardwareVer = versionBuffer[3] + (versionBuffer[4] / 10);
+ protocolVer = versionBuffer[5] + (versionBuffer[6] / 10);
+
+ LOG_INFO("SEN5X Firmware Version: %0.2f", firmwareVer);
+ LOG_INFO("SEN5X Hardware Version: %0.2f", hardwareVer);
+ LOG_INFO("SEN5X Protocol Version: %0.2f", protocolVer);
+
+ return true;
+}
+
+bool SEN5XSensor::findModel()
+{
+ if (!sendCommand(SEN5X_GET_PRODUCT_NAME)) {
+ LOG_ERROR("SEN5X: Error asking for product name");
+ return false;
+ }
+ delay(50); // From Sensirion Datasheet
+
+ const uint8_t nameSize = 48;
+ uint8_t name[nameSize];
+ size_t charNumber = readBuffer(&name[0], nameSize);
+ if (charNumber == 0) {
+ LOG_ERROR("SEN5X: Error getting device name");
+ return false;
+ }
+
+ // We only check the last character that defines the model SEN5X
+ switch(name[4])
+ {
+ case 48:
+ model = SEN50;
+ LOG_INFO("SEN5X: found sensor model SEN50");
+ break;
+ case 52:
+ model = SEN54;
+ LOG_INFO("SEN5X: found sensor model SEN54");
+ break;
+ case 53:
+ model = SEN55;
+ LOG_INFO("SEN5X: found sensor model SEN55");
+ break;
+ }
+
+ return true;
+}
+
+bool SEN5XSensor::sendCommand(uint16_t command)
+{
+ uint8_t nothing;
+ return sendCommand(command, ¬hing, 0);
+}
+
+bool SEN5XSensor::sendCommand(uint16_t command, uint8_t* buffer, uint8_t byteNumber)
+{
+ // At least we need two bytes for the command
+ uint8_t bufferSize = 2;
+
+ // Add space for CRC bytes (one every two bytes)
+ if (byteNumber > 0) bufferSize += byteNumber + (byteNumber / 2);
+
+ uint8_t toSend[bufferSize];
+ uint8_t i = 0;
+ toSend[i++] = static_cast((command & 0xFF00) >> 8);
+ toSend[i++] = static_cast((command & 0x00FF) >> 0);
+
+ // Prepare buffer with CRC every third byte
+ uint8_t bi = 0;
+ if (byteNumber > 0) {
+ while (bi < byteNumber) {
+ toSend[i++] = buffer[bi++];
+ toSend[i++] = buffer[bi++];
+ uint8_t calcCRC = sen5xCRC(&buffer[bi - 2]);
+ toSend[i++] = calcCRC;
+ }
+ }
+
+#ifdef SEN5X_I2C_CLOCK_SPEED
+ uint32_t currentClock;
+ currentClock = bus->getClock();
+ if (currentClock != SEN5X_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Changing I2C clock to %u", SEN5X_I2C_CLOCK_SPEED);
+ bus->setClock(SEN5X_I2C_CLOCK_SPEED);
+ }
+#endif
+
+ // Transmit the data
+ // LOG_DEBUG("Beginning connection to SEN5X: 0x%x. Size: %u", address, bufferSize);
+ // Note: this is necessary to allow for long-buffers
+ delay(20);
+ bus->beginTransmission(address);
+ size_t writtenBytes = bus->write(toSend, bufferSize);
+ uint8_t i2c_error = bus->endTransmission();
+
+#ifdef SEN5X_I2C_CLOCK_SPEED
+ restoreClock(currentClock);
+#endif
+
+ if (writtenBytes != bufferSize) {
+ LOG_ERROR("SEN5X: Error writting on I2C bus");
+ return false;
+ }
+
+ if (i2c_error != 0) {
+ LOG_ERROR("SEN5X: Error on I2C communication: %x", i2c_error);
+ return false;
+ }
+ return true;
+}
+
+uint8_t SEN5XSensor::readBuffer(uint8_t* buffer, uint8_t byteNumber)
+{
+#ifdef SEN5X_I2C_CLOCK_SPEED
+ uint32_t currentClock;
+ currentClock = bus->getClock();
+ if (currentClock != SEN5X_I2C_CLOCK_SPEED){
+ // LOG_DEBUG("Changing I2C clock to %u", SEN5X_I2C_CLOCK_SPEED);
+ bus->setClock(SEN5X_I2C_CLOCK_SPEED);
+ }
+#endif
+
+ size_t readBytes = bus->requestFrom(address, byteNumber);
+ if (readBytes != byteNumber) {
+ LOG_ERROR("SEN5X: Error reading I2C bus");
+ return 0;
+ }
+
+ uint8_t i = 0;
+ uint8_t receivedBytes = 0;
+ while (readBytes > 0) {
+ buffer[i++] = bus->read(); // Just as a reminder: i++ returns i and after that increments.
+ buffer[i++] = bus->read();
+ uint8_t recvCRC = bus->read();
+ uint8_t calcCRC = sen5xCRC(&buffer[i - 2]);
+ if (recvCRC != calcCRC) {
+ LOG_ERROR("SEN5X: Checksum error while receiving msg");
+ return 0;
+ }
+ readBytes -=3;
+ receivedBytes += 2;
+ }
+#ifdef SEN5X_I2C_CLOCK_SPEED
+ restoreClock(currentClock);
+#endif
+
+ return receivedBytes;
+}
+
+uint8_t SEN5XSensor::sen5xCRC(uint8_t* buffer)
+{
+ // This code is based on Sensirion's own implementation https://github.com/Sensirion/arduino-core/blob/41fd02cacf307ec4945955c58ae495e56809b96c/src/SensirionCrc.cpp
+ uint8_t crc = 0xff;
+
+ for (uint8_t i=0; i<2; i++){
+
+ crc ^= buffer[i];
+
+ for (uint8_t bit=8; bit>0; bit--) {
+ if (crc & 0x80)
+ crc = (crc << 1) ^ 0x31;
+ else
+ crc = (crc << 1);
+ }
+ }
+
+ return crc;
+}
+
+bool SEN5XSensor::I2Cdetect(TwoWire *_Wire, uint8_t address)
+{
+ _Wire->beginTransmission(address);
+ byte error = _Wire->endTransmission();
+
+ if (error == 0) return true;
+ else return false;
+}
+
+bool SEN5XSensor::idle()
+{
+ // From the datasheet:
+ // By default, the VOC algorithm resets its state to initial
+ // values each time a measurement is started,
+ // even if the measurement was stopped only for a short
+ // time. So, the VOC index output value needs a long time
+ // until it is stable again. This can be avoided by
+ // restoring the previously memorized algorithm state before
+ // starting the measure mode
+
+ // If the stabilisation period is not passed for SEN55, don't go to idle
+ if (model == SEN55) {
+ // Get VOC state before going to idle mode
+ vocValid = false;
+ if (vocStateFromSensor()) {
+ vocValid = vocStateValid();
+ // Check if we have time, and store it
+ uint32_t now; // If time is RTCQualityNone, it will return zero
+ now = getValidTime(RTCQuality::RTCQualityDevice);
+ if (now) {
+ // Check if state is valid (non-zero)
+ vocTime = now;
+ }
+ }
+
+ if (vocStateStable() && vocValid) {
+ saveState();
+ } else {
+ LOG_INFO("SEN5X: Not stopping measurement, vocState is not stable yet!");
+ return true;
+ }
+ }
+
+ if (!oneShotMode) {
+ LOG_INFO("SEN5X: Not stopping measurement, continuous mode!");
+ return true;
+ }
+
+ if (!sendCommand(SEN5X_STOP_MEASUREMENT)) {
+ LOG_ERROR("SEN5X: Error stoping measurement");
+ return false;
+ }
+
+ delay(200); // From Sensirion Datasheet
+ LOG_INFO("SEN5X: Stop measurement mode");
+
+ state = SEN5X_IDLE;
+ measureStarted = 0;
+ return true;
+}
+
+bool SEN5XSensor::vocStateRecent(uint32_t now){
+ if (now) {
+ uint32_t passed = now - vocTime; //in seconds
+
+ // Check if state is recent, less than 10 minutes (600 seconds)
+ if (passed < SEN5X_VOC_VALID_TIME && (now > SEN5X_VOC_VALID_DATE)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool SEN5XSensor::vocStateValid() {
+ if (!vocState[0] && !vocState[1] && !vocState[2] && !vocState[3] &&
+ !vocState[4] && !vocState[5] && !vocState[6] && !vocState[7]) {
+ LOG_DEBUG("SEN5X: VOC state is all 0, invalid");
+ return false;
+ } else {
+ LOG_DEBUG("SEN5X: VOC state is valid");
+ return true;
+ }
+}
+
+bool SEN5XSensor::vocStateToSensor()
+{
+ if (model != SEN55){
+ return true;
+ }
+
+ if (!vocStateValid()) {
+ LOG_INFO("SEN5X: VOC state is invalid, not sending");
+ return true;
+ }
+
+ if (!sendCommand(SEN5X_STOP_MEASUREMENT)) {
+ LOG_ERROR("SEN5X: Error stoping measurement");
+ return false;
+ }
+ delay(200); // From Sensirion Datasheet
+
+ LOG_DEBUG("SEN5X: Sending VOC state to sensor");
+ LOG_DEBUG("[%u, %u, %u, %u, %u, %u, %u, %u]",
+ vocState[0],vocState[1], vocState[2], vocState[3],
+ vocState[4],vocState[5], vocState[6], vocState[7]);
+
+ // Note: send command already takes into account the CRC
+ // buffer size increment needed
+ if (!sendCommand(SEN5X_RW_VOCS_STATE, vocState, SEN5X_VOC_STATE_BUFFER_SIZE)){
+ LOG_ERROR("SEN5X: Error sending VOC's state command'");
+ return false;
+ }
+
+ return true;
+}
+
+bool SEN5XSensor::vocStateFromSensor()
+{
+ if (model != SEN55){
+ return true;
+ }
+
+ LOG_INFO("SEN5X: Getting VOC state from sensor");
+ // Ask VOCs state from the sensor
+ if (!sendCommand(SEN5X_RW_VOCS_STATE)){
+ LOG_ERROR("SEN5X: Error sending VOC's state command'");
+ return false;
+ }
+
+ delay(20); // From Sensirion Datasheet
+
+ // Retrieve the data
+ // Allocate buffer to account for CRC
+ uint8_t vocBuffer[SEN5X_VOC_STATE_BUFFER_SIZE + (SEN5X_VOC_STATE_BUFFER_SIZE / 2)];
+ size_t receivedNumber = readBuffer(&vocBuffer[0], SEN5X_VOC_STATE_BUFFER_SIZE + (SEN5X_VOC_STATE_BUFFER_SIZE / 2));
+ delay(20); // From Sensirion Datasheet
+
+ if (receivedNumber == 0) {
+ LOG_DEBUG("SEN5X: Error getting VOC's state");
+ return false;
+ }
+
+ vocState[0] = vocBuffer[0];
+ vocState[1] = vocBuffer[1];
+ vocState[2] = vocBuffer[3];
+ vocState[3] = vocBuffer[4];
+ vocState[4] = vocBuffer[6];
+ vocState[5] = vocBuffer[7];
+ vocState[6] = vocBuffer[9];
+ vocState[7] = vocBuffer[10];
+
+ // Print the state (if debug is on)
+ LOG_DEBUG("SEN5X: VOC state retrieved from sensor: [%u, %u, %u, %u, %u, %u, %u, %u]",
+ vocState[0],vocState[1], vocState[2], vocState[3],
+ vocState[4],vocState[5], vocState[6], vocState[7]);
+
+ return true;
+}
+
+bool SEN5XSensor::loadState()
+{
+#ifdef FSCom
+ spiLock->lock();
+ auto file = FSCom.open(sen5XStateFileName, FILE_O_READ);
+ bool okay = false;
+ if (file) {
+ LOG_INFO("%s state read from %s", sensorName, sen5XStateFileName);
+ pb_istream_t stream = {&readcb, &file, meshtastic_SEN5XState_size};
+
+ if (!pb_decode(&stream, &meshtastic_SEN5XState_msg, &sen5xstate)) {
+ LOG_ERROR("Error: can't decode protobuf %s", PB_GET_ERROR(&stream));
+ } else {
+ lastCleaning = sen5xstate.last_cleaning_time;
+ lastCleaningValid = sen5xstate.last_cleaning_valid;
+ oneShotMode = sen5xstate.one_shot_mode;
+
+ if (model == SEN55) {
+ vocTime = sen5xstate.voc_state_time;
+ vocValid = sen5xstate.voc_state_valid;
+ // Unpack state
+ vocState[7] = (uint8_t)(sen5xstate.voc_state_array >> 56);
+ vocState[6] = (uint8_t)(sen5xstate.voc_state_array >> 48);
+ vocState[5] = (uint8_t)(sen5xstate.voc_state_array >> 40);
+ vocState[4] = (uint8_t)(sen5xstate.voc_state_array >> 32);
+ vocState[3] = (uint8_t)(sen5xstate.voc_state_array >> 24);
+ vocState[2] = (uint8_t)(sen5xstate.voc_state_array >> 16);
+ vocState[1] = (uint8_t)(sen5xstate.voc_state_array >> 8);
+ vocState[0] = (uint8_t) sen5xstate.voc_state_array;
+ }
+
+ // LOG_DEBUG("Loaded lastCleaning %u", lastCleaning);
+ // LOG_DEBUG("Loaded lastCleaningValid %u", lastCleaningValid);
+ // LOG_DEBUG("Loaded oneShotMode %s", oneShotMode ? "true" : "false");
+ // LOG_DEBUG("Loaded vocTime %u", vocTime);
+ // LOG_DEBUG("Loaded [%u, %u, %u, %u, %u, %u, %u, %u]",
+ // vocState[7], vocState[6], vocState[5], vocState[4], vocState[3], vocState[2], vocState[1], vocState[0]);
+ // LOG_DEBUG("Loaded %svalid VOC state", vocValid ? "" : "in");
+
+ okay = true;
+ }
+ file.close();
+ } else {
+ LOG_INFO("No %s state found (File: %s)", sensorName, sen5XStateFileName);
+ }
+ spiLock->unlock();
+ return okay;
+#else
+ LOG_ERROR("SEN5X: ERROR - Filesystem not implemented");
+#endif
+}
+
+bool SEN5XSensor::saveState()
+{
+ // TODO - This should be called before a reboot for VOC index storage
+ // is there a way to get notified?
+#ifdef FSCom
+ auto file = SafeFile(sen5XStateFileName);
+
+ sen5xstate.last_cleaning_time = lastCleaning;
+ sen5xstate.last_cleaning_valid = lastCleaningValid;
+ sen5xstate.one_shot_mode = oneShotMode;
+
+ if (model == SEN55) {
+ sen5xstate.has_voc_state_time = true;
+ sen5xstate.has_voc_state_valid = true;
+ sen5xstate.has_voc_state_array = true;
+
+ sen5xstate.voc_state_time = vocTime;
+ sen5xstate.voc_state_valid = vocValid;
+ // Unpack state (8 bytes)
+ sen5xstate.voc_state_array = (((uint64_t) vocState[7]) << 56) |
+ ((uint64_t) vocState[6] << 48) |
+ ((uint64_t) vocState[5] << 40) |
+ ((uint64_t) vocState[4] << 32) |
+ ((uint64_t) vocState[3] << 24) |
+ ((uint64_t) vocState[2] << 16) |
+ ((uint64_t) vocState[1] << 8) |
+ ((uint64_t) vocState[0]);
+ }
+
+ bool okay = false;
+
+ LOG_INFO("%s: state write to %s", sensorName, sen5XStateFileName);
+ pb_ostream_t stream = {&writecb, static_cast(&file), meshtastic_SEN5XState_size};
+
+ if (!pb_encode(&stream, &meshtastic_SEN5XState_msg, &sen5xstate)) {
+ LOG_ERROR("Error: can't encode protobuf %s", PB_GET_ERROR(&stream));
+ } else {
+ okay = true;
+ }
+
+ okay &= file.close();
+
+ if (okay)
+ LOG_INFO("%s: state write to %s successful", sensorName, sen5XStateFileName);
+
+ return okay;
+#else
+ LOG_ERROR("%s: ERROR - Filesystem not implemented", sensorName);
+#endif
+}
+
+bool SEN5XSensor::isActive(){
+ return state == SEN5X_MEASUREMENT || state == SEN5X_MEASUREMENT_2;
+}
+
+uint32_t SEN5XSensor::wakeUp(){
+ uint32_t now;
+ now = getValidTime(RTCQuality::RTCQualityDevice);
+ LOG_DEBUG("SEN5X: Waking up sensor");
+
+ // Check if state is recent, less than 10 minutes (600 seconds)
+ if (vocStateRecent(now) && vocStateValid()) {
+ if (!vocStateToSensor()){
+ LOG_ERROR("SEN5X: Sending VOC state to sensor failed");
+ }
+ } else {
+ LOG_DEBUG("SEN5X: No valid VOC state found. Ignoring");
+ }
+
+ if (!sendCommand(SEN5X_START_MEASUREMENT)) {
+ LOG_ERROR("SEN5X: Error starting measurement");
+ // TODO - what should this return?? Something actually on the default interval
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+ delay(50); // From Sensirion Datasheet
+
+ // TODO - This is currently "problematic"
+ // If time is updated in between reads, there is no way to
+ // keep track of how long it has passed
+ measureStarted = getTime();
+ state = SEN5X_MEASUREMENT;
+ if (state == SEN5X_MEASUREMENT)
+ LOG_INFO("SEN5X: Started measurement mode");
+ return SEN5X_WARMUP_MS_1;
+}
+
+bool SEN5XSensor::vocStateStable()
+{
+ uint32_t now;
+ now = getTime();
+ uint32_t sinceFirstMeasureStarted = (now - firstMeasureStarted);
+ LOG_DEBUG("sinceFirstMeasureStarted: %us", sinceFirstMeasureStarted);
+ return sinceFirstMeasureStarted > SEN55_VOC_STATE_WARMUP_S;
+}
+
+bool SEN5XSensor::startCleaning()
+{
+ // Note: we only should enter here if we have a valid RTC with at least
+ // RTCQuality::RTCQualityDevice
+ state = SEN5X_CLEANING;
+
+ // Note that cleaning command can only be run when the sensor is in measurement mode
+ if (!sendCommand(SEN5X_START_MEASUREMENT)) {
+ LOG_ERROR("SEN5X: Error starting measurment mode");
+ return false;
+ }
+ delay(50); // From Sensirion Datasheet
+
+ if (!sendCommand(SEN5X_START_FAN_CLEANING)) {
+ LOG_ERROR("SEN5X: Error starting fan cleaning");
+ return false;
+ }
+ delay(20); // From Sensirion Datasheet
+
+ // This message will be always printed so the user knows the device it's not hung
+ LOG_INFO("SEN5X: Started fan cleaning it will take 10 seconds...");
+
+ uint16_t started = millis();
+ while (millis() - started < 10500) {
+ // Serial.print(".");
+ delay(500);
+ }
+ LOG_INFO("SEN5X: Cleaning done!!");
+
+ // Save timestamp in flash so we know when a week has passed
+ uint32_t now;
+ now = getValidTime(RTCQuality::RTCQualityDevice);
+ // If time is not RTCQualityNone, it will return non-zero
+ lastCleaning = now;
+ lastCleaningValid = true;
+ saveState();
+
+ idle();
+ return true;
+}
+
+int32_t SEN5XSensor::runOnce()
+{
+ state = SEN5X_NOT_DETECTED;
+ LOG_INFO("Init sensor: %s", sensorName);
+ if (!hasSensor()) {
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+
+ bus = nodeTelemetrySensorsMap[sensorType].second;
+ address = (uint8_t)nodeTelemetrySensorsMap[sensorType].first;
+
+ delay(50); // without this there is an error on the deviceReset function
+
+ if (!sendCommand(SEN5X_RESET)) {
+ LOG_ERROR("SEN5X: Error reseting device");
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+ delay(200); // From Sensirion Datasheet
+
+ if (!findModel()) {
+ LOG_ERROR("SEN5X: error finding sensor model");
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+
+ // Check the firmware version
+ if (!getVersion()) return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ if (firmwareVer < 2) {
+ LOG_ERROR("SEN5X: error firmware is too old and will not work with this implementation");
+ return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
+ }
+ delay(200); // From Sensirion Datasheet
+
+ // Detection succeeded
+ state = SEN5X_IDLE;
+ status = 1;
+
+ // Load state
+ loadState();
+
+ // Check if it is time to do a cleaning
+ uint32_t now;
+ int32_t passed;
+ now = getValidTime(RTCQuality::RTCQualityDevice);
+
+ // If time is not RTCQualityNone, it will return non-zero
+ if (now) {
+ if (lastCleaningValid) {
+
+ passed = now - lastCleaning; // in seconds
+
+ if (passed > ONE_WEEK_IN_SECONDS && (now > SEN5X_VOC_VALID_DATE)) {
+ // If current date greater than 01/01/2018 (validity check)
+ LOG_INFO("SEN5X: More than a week (%us) since last cleaning in epoch (%us). Trigger, cleaning...", passed, lastCleaning);
+ startCleaning();
+ } else {
+ LOG_INFO("SEN5X: Cleaning not needed (%ds passed). Last cleaning date (in epoch): %us", passed, lastCleaning);
+ }
+ } else {
+ // We assume the device has just been updated or it is new,
+ // so no need to trigger a cleaning.
+ // Just save the timestamp to do a cleaning one week from now.
+ // Otherwise, we will never trigger cleaning in some cases
+ lastCleaning = now;
+ lastCleaningValid = true;
+ LOG_INFO("SEN5X: No valid last cleaning date found, saving it now: %us", lastCleaning);
+ saveState();
+ }
+
+ if (model == SEN55) {
+ if (!vocValid) {
+ LOG_INFO("SEN5X: No valid VOC's state found");
+ } else {
+ // Check if state is recent
+ if (vocStateRecent(now)) {
+ // If current date greater than 01/01/2018 (validity check)
+ // Send it to the sensor
+ LOG_INFO("SEN5X: VOC state is valid and recent");
+ vocStateToSensor();
+ } else {
+ LOG_INFO("SEN5X: VOC state is too old or date is invalid");
+ LOG_DEBUG("SEN5X: vocTime %u, Passed %u, and now %u", vocTime, passed, now);
+ }
+ }
+ }
+ } else {
+ // TODO - Should this actually ignore? We could end up never cleaning...
+ LOG_INFO("SEN5X: Not enough RTCQuality, ignoring saved state. Trying again later");
+ }
+
+ return initI2CSensor();
+}
+
+void SEN5XSensor::setup()
+{
+}
+
+bool SEN5XSensor::readValues()
+{
+ if (!sendCommand(SEN5X_READ_VALUES)){
+ LOG_ERROR("SEN5X: Error sending read command");
+ return false;
+ }
+ LOG_DEBUG("SEN5X: Reading PM Values");
+ delay(20); // From Sensirion Datasheet
+
+ uint8_t dataBuffer[24];
+ size_t receivedNumber = readBuffer(&dataBuffer[0], 24);
+ if (receivedNumber == 0) {
+ LOG_ERROR("SEN5X: Error getting values");
+ return false;
+ }
+
+ // Get the integers
+ uint16_t uint_pM1p0 = static_cast((dataBuffer[0] << 8) | dataBuffer[1]);
+ uint16_t uint_pM2p5 = static_cast((dataBuffer[2] << 8) | dataBuffer[3]);
+ uint16_t uint_pM4p0 = static_cast((dataBuffer[4] << 8) | dataBuffer[5]);
+ uint16_t uint_pM10p0 = static_cast((dataBuffer[6] << 8) | dataBuffer[7]);
+ int16_t int_humidity = static_cast((dataBuffer[8] << 8) | dataBuffer[9]);
+ int16_t int_temperature = static_cast((dataBuffer[10] << 8) | dataBuffer[11]);
+ int16_t int_vocIndex = static_cast((dataBuffer[12] << 8) | dataBuffer[13]);
+ int16_t int_noxIndex = static_cast((dataBuffer[14] << 8) | dataBuffer[15]);
+
+ // Convert values based on Sensirion Arduino lib
+ sen5xmeasurement.pM1p0 = !isnan(uint_pM1p0) ? uint_pM1p0 / 10 : UINT16_MAX;
+ sen5xmeasurement.pM2p5 = !isnan(uint_pM2p5) ? uint_pM2p5 / 10 : UINT16_MAX;
+ sen5xmeasurement.pM4p0 = !isnan(uint_pM4p0) ? uint_pM4p0 / 10 : UINT16_MAX;
+ sen5xmeasurement.pM10p0 = !isnan(uint_pM10p0) ? uint_pM10p0 / 10 : UINT16_MAX;
+ sen5xmeasurement.humidity = !isnan(int_humidity) ? int_humidity / 100.0f : FLT_MAX;
+ sen5xmeasurement.temperature = !isnan(int_temperature) ? int_temperature / 200.0f : FLT_MAX;
+ sen5xmeasurement.vocIndex = !isnan(int_vocIndex) ? int_vocIndex / 10.0f : FLT_MAX;
+ sen5xmeasurement.noxIndex = !isnan(int_noxIndex) ? int_noxIndex / 10.0f : FLT_MAX;
+
+ LOG_DEBUG("Got: pM1p0=%u, pM2p5=%u, pM4p0=%u, pM10p0=%u",
+ sen5xmeasurement.pM1p0, sen5xmeasurement.pM2p5,
+ sen5xmeasurement.pM4p0, sen5xmeasurement.pM10p0);
+
+ if (model == SEN54 || model == SEN55) {
+ LOG_DEBUG("Got: humidity=%.2f, temperature=%.2f, noxIndex=%.2f",
+ sen5xmeasurement.humidity, sen5xmeasurement.temperature,
+ sen5xmeasurement.noxIndex);
+ }
+
+ if (model == SEN55) {
+ LOG_DEBUG("Got: vocIndex=%.2f",
+ sen5xmeasurement.vocIndex);
+ }
+
+ return true;
+}
+
+bool SEN5XSensor::readPnValues(bool cumulative)
+{
+ if (!sendCommand(SEN5X_READ_PM_VALUES)){
+ LOG_ERROR("SEN5X: Error sending read command");
+ return false;
+ }
+
+ LOG_DEBUG("SEN5X: Reading PN Values");
+ delay(20); // From Sensirion Datasheet
+
+ uint8_t dataBuffer[30];
+ size_t receivedNumber = readBuffer(&dataBuffer[0], 30);
+ if (receivedNumber == 0) {
+ LOG_ERROR("SEN5X: Error getting PN values");
+ return false;
+ }
+
+ // Get the integers
+ // uint16_t uint_pM1p0 = static_cast((dataBuffer[0] << 8) | dataBuffer[1]);
+ // uint16_t uint_pM2p5 = static_cast((dataBuffer[2] << 8) | dataBuffer[3]);
+ // uint16_t uint_pM4p0 = static_cast((dataBuffer[4] << 8) | dataBuffer[5]);
+ // uint16_t uint_pM10p0 = static_cast((dataBuffer[6] << 8) | dataBuffer[7]);
+ uint16_t uint_pN0p5 = static_cast((dataBuffer[8] << 8) | dataBuffer[9]);
+ uint16_t uint_pN1p0 = static_cast((dataBuffer[10] << 8) | dataBuffer[11]);
+ uint16_t uint_pN2p5 = static_cast((dataBuffer[12] << 8) | dataBuffer[13]);
+ uint16_t uint_pN4p0 = static_cast((dataBuffer[14] << 8) | dataBuffer[15]);
+ uint16_t uint_pN10p0 = static_cast((dataBuffer[16] << 8) | dataBuffer[17]);
+ uint16_t uint_tSize = static_cast((dataBuffer[18] << 8) | dataBuffer[19]);
+
+ // Convert values based on Sensirion Arduino lib
+ // Multiply by 100 for converting from #/cm3 to #/0.1l for PN values
+ sen5xmeasurement.pN0p5 = !isnan(uint_pN0p5) ? uint_pN0p5 / 10 * 100: UINT32_MAX;
+ sen5xmeasurement.pN1p0 = !isnan(uint_pN1p0) ? uint_pN1p0 / 10 * 100: UINT32_MAX;
+ sen5xmeasurement.pN2p5 = !isnan(uint_pN2p5) ? uint_pN2p5 / 10 * 100: UINT32_MAX;
+ sen5xmeasurement.pN4p0 = !isnan(uint_pN4p0) ? uint_pN4p0 / 10 * 100: UINT32_MAX;
+ sen5xmeasurement.pN10p0 = !isnan(uint_pN10p0) ? uint_pN10p0 / 10 * 100: UINT32_MAX;
+ sen5xmeasurement.tSize = !isnan(uint_tSize) ? uint_tSize / 1000.0f : FLT_MAX;
+
+ // Remove accumuluative values:
+ // https://github.com/fablabbcn/smartcitizen-kit-2x/issues/85
+ if (!cumulative) {
+ sen5xmeasurement.pN10p0 -= sen5xmeasurement.pN4p0;
+ sen5xmeasurement.pN4p0 -= sen5xmeasurement.pN2p5;
+ sen5xmeasurement.pN2p5 -= sen5xmeasurement.pN1p0;
+ sen5xmeasurement.pN1p0 -= sen5xmeasurement.pN0p5;
+ }
+
+ LOG_DEBUG("Got: pN0p5=%u, pN1p0=%u, pN2p5=%u, pN4p0=%u, pN10p0=%u, tSize=%.2f",
+ sen5xmeasurement.pN0p5, sen5xmeasurement.pN1p0,
+ sen5xmeasurement.pN2p5, sen5xmeasurement.pN4p0,
+ sen5xmeasurement.pN10p0, sen5xmeasurement.tSize
+ );
+
+ return true;
+}
+
+// TODO - Decide if we want to have this here or not
+// bool SEN5XSensor::readRawValues()
+// {
+// if (!sendCommand(SEN5X_READ_RAW_VALUES)){
+// LOG_ERROR("SEN5X: Error sending read command");
+// return false;
+// }
+// delay(20); // From Sensirion Datasheet
+
+// uint8_t dataBuffer[12];
+// size_t receivedNumber = readBuffer(&dataBuffer[0], 12);
+// if (receivedNumber == 0) {
+// LOG_ERROR("SEN5X: Error getting Raw values");
+// return false;
+// }
+
+// // Get values
+// rawHumidity = static_cast((dataBuffer[0] << 8) | dataBuffer[1]);
+// rawTemperature = static_cast((dataBuffer[2] << 8) | dataBuffer[3]);
+// rawVoc = static_cast((dataBuffer[4] << 8) | dataBuffer[5]);
+// rawNox = static_cast((dataBuffer[6] << 8) | dataBuffer[7]);
+
+// return true;
+// }
+
+uint8_t SEN5XSensor::getMeasurements()
+{
+ // Try to get new data
+ if (!sendCommand(SEN5X_READ_DATA_READY)){
+ LOG_ERROR("SEN5X: Error sending command data ready flag");
+ return 2;
+ }
+ delay(20); // From Sensirion Datasheet
+
+ uint8_t dataReadyBuffer[3];
+ size_t charNumber = readBuffer(&dataReadyBuffer[0], 3);
+ if (charNumber == 0) {
+ LOG_ERROR("SEN5X: Error getting device version value");
+ return 2;
+ }
+
+ bool data_ready = dataReadyBuffer[1];
+
+ if (!data_ready) {
+ LOG_INFO("SEN5X: Data is not ready");
+ return 1;
+ }
+
+ if(!readValues()) {
+ LOG_ERROR("SEN5X: Error getting readings");
+ return 2;
+ }
+
+ if(!readPnValues(false)) {
+ LOG_ERROR("SEN5X: Error getting PN readings");
+ return 2;
+ }
+
+ // if(!readRawValues()) {
+ // LOG_ERROR("SEN5X: Error getting Raw readings");
+ // return 2;
+ // }
+
+ return 0;
+}
+
+int32_t SEN5XSensor::pendingForReady(){
+ uint32_t now;
+ now = getTime();
+ uint32_t sinceMeasureStarted = (now - measureStarted)*1000;
+ LOG_DEBUG("SEN5X: Since measure started: %ums", sinceMeasureStarted);
+ switch (state) {
+ case SEN5X_MEASUREMENT: {
+
+ if (sinceMeasureStarted < SEN5X_WARMUP_MS_1) {
+ LOG_INFO("SEN5X: not enough time passed since starting measurement");
+ return SEN5X_WARMUP_MS_1 - sinceMeasureStarted;
+ }
+
+ if (!firstMeasureStarted) {
+ firstMeasureStarted = now;
+ }
+
+ // Get PN values to check if we are above or below threshold
+ readPnValues(true);
+
+ // If the reading is low (the tyhreshold is in #/cm3) and second warmUp hasn't passed we return to come back later
+ if ((sen5xmeasurement.pN4p0 / 100) < SEN5X_PN4P0_CONC_THD && sinceMeasureStarted < SEN5X_WARMUP_MS_2) {
+ LOG_INFO("SEN5X: Concentration is low, we will ask again in the second warm up period");
+ state = SEN5X_MEASUREMENT_2;
+ // Report how many seconds are pending to cover the first warm up period
+ return SEN5X_WARMUP_MS_2 - sinceMeasureStarted;
+ }
+ return 0;
+ }
+ case SEN5X_MEASUREMENT_2: {
+ if (sinceMeasureStarted < SEN5X_WARMUP_MS_2) {
+ // Report how many seconds are pending to cover the first warm up period
+ return SEN5X_WARMUP_MS_2 - sinceMeasureStarted;
+ }
+ return 0;
+ }
+ default: {
+ return -1;
+ }
+ }
+}
+
+bool SEN5XSensor::getMetrics(meshtastic_Telemetry *measurement)
+{
+ LOG_INFO("SEN5X: Attempting to get metrics");
+ if (!isActive()){
+ LOG_INFO("SEN5X: not in measurement mode");
+ return false;
+ }
+
+ uint8_t response;
+ response = getMeasurements();
+
+ if (response == 0) {
+ if (sen5xmeasurement.pM1p0 != UINT16_MAX) {
+ measurement->variant.air_quality_metrics.has_pm10_standard = true;
+ measurement->variant.air_quality_metrics.pm10_standard = sen5xmeasurement.pM1p0;
+ }
+ if (sen5xmeasurement.pM2p5 != UINT16_MAX) {
+ measurement->variant.air_quality_metrics.has_pm25_standard = true;
+ measurement->variant.air_quality_metrics.pm25_standard = sen5xmeasurement.pM2p5;
+ }
+ if (sen5xmeasurement.pM4p0 != UINT16_MAX) {
+ measurement->variant.air_quality_metrics.has_pm40_standard = true;
+ measurement->variant.air_quality_metrics.pm40_standard = sen5xmeasurement.pM4p0;
+ }
+ if (sen5xmeasurement.pM10p0 != UINT16_MAX) {
+ measurement->variant.air_quality_metrics.has_pm100_standard = true;
+ measurement->variant.air_quality_metrics.pm100_standard = sen5xmeasurement.pM10p0;
+ }
+ if (sen5xmeasurement.pN0p5 != UINT32_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_05um = true;
+ measurement->variant.air_quality_metrics.particles_05um = sen5xmeasurement.pN0p5;
+ }
+ if (sen5xmeasurement.pN1p0 != UINT32_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_10um = true;
+ measurement->variant.air_quality_metrics.particles_10um = sen5xmeasurement.pN1p0;
+ }
+ if (sen5xmeasurement.pN2p5 != UINT32_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_25um = true;
+ measurement->variant.air_quality_metrics.particles_25um = sen5xmeasurement.pN2p5;
+ }
+ if (sen5xmeasurement.pN4p0 != UINT32_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_40um = true;
+ measurement->variant.air_quality_metrics.particles_40um = sen5xmeasurement.pN4p0;
+ }
+ if (sen5xmeasurement.pN10p0 != UINT32_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_100um = true;
+ measurement->variant.air_quality_metrics.particles_100um = sen5xmeasurement.pN10p0;
+ }
+ if (sen5xmeasurement.tSize != FLT_MAX) {
+ measurement->variant.air_quality_metrics.has_particles_tps = true;
+ measurement->variant.air_quality_metrics.particles_tps = sen5xmeasurement.tSize;
+ }
+
+ if (model == SEN54 || model == SEN55) {
+ if (sen5xmeasurement.humidity!= FLT_MAX) {
+ measurement->variant.air_quality_metrics.has_pm_humidity = true;
+ measurement->variant.air_quality_metrics.pm_humidity = sen5xmeasurement.humidity;
+ }
+ if (sen5xmeasurement.temperature!= FLT_MAX) {
+ measurement->variant.air_quality_metrics.has_pm_temperature = true;
+ measurement->variant.air_quality_metrics.pm_temperature = sen5xmeasurement.temperature;
+ }
+ if (sen5xmeasurement.noxIndex!= FLT_MAX) {
+ measurement->variant.air_quality_metrics.has_pm_nox_idx = true;
+ measurement->variant.air_quality_metrics.pm_nox_idx = sen5xmeasurement.noxIndex;
+ }
+ }
+
+ if (model == SEN55) {
+ if (sen5xmeasurement.noxIndex!= FLT_MAX) {
+ measurement->variant.air_quality_metrics.has_pm_voc_idx = true;
+ measurement->variant.air_quality_metrics.pm_voc_idx = sen5xmeasurement.vocIndex;
+ }
+ }
+
+
+ return true;
+ } else if (response == 1) {
+ // TODO return because data was not ready yet
+ // Should this return false?
+ idle();
+ return false;
+ } else if (response == 2) {
+ // Return with error for non-existing data
+ idle();
+ return false;
+ }
+
+ return true;
+}
+
+void SEN5XSensor::setMode(bool setOneShot) {
+ oneShotMode = setOneShot;
+}
+
+AdminMessageHandleResult SEN5XSensor::handleAdminMessage(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *request,
+ meshtastic_AdminMessage *response)
+{
+ AdminMessageHandleResult result;
+ result = AdminMessageHandleResult::NOT_HANDLED;
+
+
+ switch (request->which_payload_variant) {
+ case meshtastic_AdminMessage_sensor_config_tag:
+ if (!request->sensor_config.has_sen5x_config) {
+ result = AdminMessageHandleResult::NOT_HANDLED;
+ break;
+ }
+
+ // TODO - Add admin command to set temperature offset
+ // Check for temperature offset
+ // if (request->sensor_config.sen5x_config.has_set_temperature) {
+ // this->setTemperature(request->sensor_config.sen5x_config.set_temperature);
+ // }
+
+ // Check for one-shot/continuous mode request
+ if (request->sensor_config.sen5x_config.has_set_one_shot_mode) {
+ this->setMode(request->sensor_config.sen5x_config.set_one_shot_mode);
+ }
+
+ result = AdminMessageHandleResult::HANDLED;
+ break;
+
+ default:
+ result = AdminMessageHandleResult::NOT_HANDLED;
+ }
+
+ return result;
+}
+
+#endif
\ No newline at end of file
diff --git a/src/modules/Telemetry/Sensor/SEN5XSensor.h b/src/modules/Telemetry/Sensor/SEN5XSensor.h
new file mode 100644
index 000000000..d94bfe76c
--- /dev/null
+++ b/src/modules/Telemetry/Sensor/SEN5XSensor.h
@@ -0,0 +1,152 @@
+#include "configuration.h"
+
+#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
+
+#include "../mesh/generated/meshtastic/telemetry.pb.h"
+#include "TelemetrySensor.h"
+#include "Wire.h"
+#include "RTC.h"
+
+// Warm up times for SEN5X from the datasheet
+#ifndef SEN5X_WARMUP_MS_1
+#define SEN5X_WARMUP_MS_1 15000
+#endif
+
+#ifndef SEN5X_WARMUP_MS_2
+#define SEN5X_WARMUP_MS_2 30000
+#endif
+
+#ifndef SEN5X_I2C_CLOCK_SPEED
+#define SEN5X_I2C_CLOCK_SPEED 100000
+#endif
+
+/*
+Time after which the sensor can go to sleep, as the warmup period has passed
+and the VOCs sensor will is allowed to stop (although needs to recover the state
+each time)
+*/
+#ifndef SEN55_VOC_STATE_WARMUP_S
+// TODO for Testing 5' - Sensirion recommends 1h. We can try to test a smaller value
+#define SEN55_VOC_STATE_WARMUP_S 3600
+#endif
+
+#define ONE_WEEK_IN_SECONDS 604800
+
+struct _SEN5XMeasurements {
+ uint16_t pM1p0;
+ uint16_t pM2p5;
+ uint16_t pM4p0;
+ uint16_t pM10p0;
+ uint32_t pN0p5;
+ uint32_t pN1p0;
+ uint32_t pN2p5;
+ uint32_t pN4p0;
+ uint32_t pN10p0;
+ float tSize;
+ float humidity;
+ float temperature;
+ float vocIndex;
+ float noxIndex;
+};
+
+class SEN5XSensor : public TelemetrySensor
+{
+ private:
+ TwoWire * bus;
+ uint8_t address;
+
+ bool getVersion();
+ float firmwareVer = -1;
+ float hardwareVer = -1;
+ float protocolVer = -1;
+ bool findModel();
+
+ // Commands
+ #define SEN5X_RESET 0xD304
+ #define SEN5X_GET_PRODUCT_NAME 0xD014
+ #define SEN5X_GET_FIRMWARE_VERSION 0xD100
+ #define SEN5X_START_MEASUREMENT 0x0021
+ #define SEN5X_START_MEASUREMENT_RHT_GAS 0x0037
+ #define SEN5X_STOP_MEASUREMENT 0x0104
+ #define SEN5X_READ_DATA_READY 0x0202
+ #define SEN5X_START_FAN_CLEANING 0x5607
+ #define SEN5X_RW_VOCS_STATE 0x6181
+
+ #define SEN5X_READ_VALUES 0x03C4
+ #define SEN5X_READ_RAW_VALUES 0x03D2
+ #define SEN5X_READ_PM_VALUES 0x0413
+
+ #define SEN5X_VOC_VALID_TIME 600
+ #define SEN5X_VOC_VALID_DATE 1514764800
+
+ enum SEN5Xmodel { SEN5X_UNKNOWN = 0, SEN50 = 0b001, SEN54 = 0b010, SEN55 = 0b100 };
+ SEN5Xmodel model = SEN5X_UNKNOWN;
+
+ enum SEN5XState { SEN5X_OFF, SEN5X_IDLE, SEN5X_MEASUREMENT, SEN5X_MEASUREMENT_2, SEN5X_CLEANING, SEN5X_NOT_DETECTED };
+ SEN5XState state = SEN5X_OFF;
+ // Flag to work on one-shot (read and sleep), or continuous mode
+ bool oneShotMode = true;
+ void setMode(bool setOneShot);
+ bool vocStateValid();
+
+ bool sendCommand(uint16_t command);
+ bool sendCommand(uint16_t command, uint8_t* buffer, uint8_t byteNumber=0);
+ uint8_t readBuffer(uint8_t* buffer, uint8_t byteNumber); // Return number of bytes received
+ uint8_t sen5xCRC(uint8_t* buffer);
+ bool I2Cdetect(TwoWire *_Wire, uint8_t address);
+ bool restoreClock(uint32_t);
+ bool startCleaning();
+ uint8_t getMeasurements();
+ // bool readRawValues();
+ bool readPnValues(bool cumulative);
+ bool readValues();
+
+ uint32_t measureStarted = 0;
+ uint32_t firstMeasureStarted = 0;
+ _SEN5XMeasurements sen5xmeasurement;
+
+ protected:
+ // Store status of the sensor in this file
+ const char *sen5XStateFileName = "/prefs/sen5X.dat";
+ meshtastic_SEN5XState sen5xstate = meshtastic_SEN5XState_init_zero;
+
+ bool loadState();
+ bool saveState();
+
+ // Cleaning State
+ uint32_t lastCleaning = 0;
+ bool lastCleaningValid = false;
+
+ // VOC State
+ #define SEN5X_VOC_STATE_BUFFER_SIZE 8
+ uint8_t vocState[SEN5X_VOC_STATE_BUFFER_SIZE];
+ uint32_t vocTime = 0;
+ bool vocValid = false;
+
+ bool vocStateFromSensor();
+ bool vocStateToSensor();
+ bool vocStateStable();
+ bool vocStateRecent(uint32_t now);
+
+ virtual void setup() override;
+
+ public:
+
+ SEN5XSensor();
+ bool isActive();
+ uint32_t wakeUp();
+ bool idle();
+ virtual int32_t runOnce() override;
+ virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
+
+ // Sensirion recommends taking a reading after 15 seconds, if the Particle number reading is over 100#/cm3 the reading is OK, but if it is lower wait until 30 seconds and take it again.
+ // https://sensirion.com/resource/application_note/low_power_mode/sen5x
+ #define SEN5X_PN4P0_CONC_THD 100
+ // This value represents the time needed for pending data
+ int32_t pendingForReady();
+ AdminMessageHandleResult handleAdminMessage(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *request, meshtastic_AdminMessage *response) override;
+};
+
+
+
+#endif
\ No newline at end of file
diff --git a/src/serialization/MeshPacketSerializer.cpp b/src/serialization/MeshPacketSerializer.cpp
index 5a1f8ed7e..2a8af1778 100644
--- a/src/serialization/MeshPacketSerializer.cpp
+++ b/src/serialization/MeshPacketSerializer.cpp
@@ -149,18 +149,18 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (decoded->variant.air_quality_metrics.has_pm100_standard) {
msgPayload["pm100"] = new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm100_standard);
}
- if (decoded->variant.air_quality_metrics.has_pm10_environmental) {
- msgPayload["pm10_e"] =
- new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm10_environmental);
- }
- if (decoded->variant.air_quality_metrics.has_pm25_environmental) {
- msgPayload["pm25_e"] =
- new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm25_environmental);
- }
- if (decoded->variant.air_quality_metrics.has_pm100_environmental) {
- msgPayload["pm100_e"] =
- new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm100_environmental);
- }
+ // if (decoded->variant.air_quality_metrics.has_pm10_environmental) {
+ // msgPayload["pm10_e"] =
+ // new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm10_environmental);
+ // }
+ // if (decoded->variant.air_quality_metrics.has_pm25_environmental) {
+ // msgPayload["pm25_e"] =
+ // new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm25_environmental);
+ // }
+ // if (decoded->variant.air_quality_metrics.has_pm100_environmental) {
+ // msgPayload["pm100_e"] =
+ // new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm100_environmental);
+ // }
} else if (decoded->which_variant == meshtastic_Telemetry_power_metrics_tag) {
if (decoded->variant.power_metrics.has_ch1_voltage) {
msgPayload["voltage_ch1"] = new JSONValue(decoded->variant.power_metrics.ch1_voltage);
diff --git a/src/serialization/MeshPacketSerializer_nRF52.cpp b/src/serialization/MeshPacketSerializer_nRF52.cpp
index e0daa1a88..065c28827 100644
--- a/src/serialization/MeshPacketSerializer_nRF52.cpp
+++ b/src/serialization/MeshPacketSerializer_nRF52.cpp
@@ -120,15 +120,15 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (decoded->variant.air_quality_metrics.has_pm100_standard) {
jsonObj["payload"]["pm100"] = (unsigned int)decoded->variant.air_quality_metrics.pm100_standard;
}
- if (decoded->variant.air_quality_metrics.has_pm10_environmental) {
- jsonObj["payload"]["pm10_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm10_environmental;
- }
- if (decoded->variant.air_quality_metrics.has_pm25_environmental) {
- jsonObj["payload"]["pm25_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm25_environmental;
- }
- if (decoded->variant.air_quality_metrics.has_pm100_environmental) {
- jsonObj["payload"]["pm100_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm100_environmental;
- }
+ // if (decoded->variant.air_quality_metrics.has_pm10_environmental) {
+ // jsonObj["payload"]["pm10_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm10_environmental;
+ // }
+ // if (decoded->variant.air_quality_metrics.has_pm25_environmental) {
+ // jsonObj["payload"]["pm25_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm25_environmental;
+ // }
+ // if (decoded->variant.air_quality_metrics.has_pm100_environmental) {
+ // jsonObj["payload"]["pm100_e"] = (unsigned int)decoded->variant.air_quality_metrics.pm100_environmental;
+ // }
} else if (decoded->which_variant == meshtastic_Telemetry_power_metrics_tag) {
if (decoded->variant.power_metrics.has_ch1_voltage) {
jsonObj["payload"]["voltage_ch1"] = decoded->variant.power_metrics.ch1_voltage;