From 98d94d9423066518c9f61aff7a0564b1651ef842 Mon Sep 17 00:00:00 2001 From: Normunds Gavars Date: Thu, 22 May 2025 00:19:00 +0300 Subject: [PATCH] Remove sensor wrapper classes and simplify. Switch to Adafruit libs for sensors. --- src/helpers/sensors/AHTX0Sensor.h | 39 ------ src/helpers/sensors/BME280Sensor.h | 55 -------- .../sensors/EnvironmentSensorManager.cpp | 118 ++++++++++-------- .../sensors/EnvironmentSensorManager.h | 40 +++--- src/helpers/sensors/INA3221Sensor.h | 71 ----------- variants/promicro/platformio.ini | 15 +-- 6 files changed, 97 insertions(+), 241 deletions(-) delete mode 100644 src/helpers/sensors/AHTX0Sensor.h delete mode 100644 src/helpers/sensors/BME280Sensor.h delete mode 100644 src/helpers/sensors/INA3221Sensor.h diff --git a/src/helpers/sensors/AHTX0Sensor.h b/src/helpers/sensors/AHTX0Sensor.h deleted file mode 100644 index 04af3057..00000000 --- a/src/helpers/sensors/AHTX0Sensor.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include -#include - -#define TELEM_AHTX_ADDRESS 0x38 // AHT10, AHT20 temperature and humidity sensor I2C address - -static Adafruit_AHTX0 AHTX0; - -class AHTX0Sensor { - bool initialized = false; -public: - void begin() { - if (AHTX0.begin(&Wire, 0, TELEM_AHTX_ADDRESS)) { - MESH_DEBUG_PRINTLN("Found AHT10/AHT20 at address: %02X", TELEM_AHTX_ADDRESS); - initialized = true; - } else { - initialized = false; - MESH_DEBUG_PRINTLN("AHT10/AHT20 was not found at I2C address %02X", TELEM_AHTX_ADDRESS); - } - } - - bool isInitialized() const { return initialized; }; - - float getRelativeHumidity() const { - if (initialized) { - sensors_event_t humidity, temp; - AHTX0.getEvent(&humidity, &temp); - return humidity.relative_humidity; - } - } - - float getTemperature() const { - if (initialized) { - sensors_event_t humidity, temp; - AHTX0.getEvent(&humidity, &temp); - return temp.temperature; - } - } -}; diff --git a/src/helpers/sensors/BME280Sensor.h b/src/helpers/sensors/BME280Sensor.h deleted file mode 100644 index 1226fce5..00000000 --- a/src/helpers/sensors/BME280Sensor.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include -#include - -#define TELEM_BME280_ADDRESS 0x76 // BME280 environmental sensor I2C address -#define SEALEVELPRESSURE_HPA (1013.25) // Athmospheric pressure at sea level - -static Adafruit_BME280 BME280; - -class BME280Sensor { - bool initialized = false; -public: - void begin() { - if (BME280.begin(TELEM_BME280_ADDRESS, &Wire)) { - MESH_DEBUG_PRINTLN("Found BME280 at address: %02X", TELEM_BME280_ADDRESS); - MESH_DEBUG_PRINTLN("BME sensor ID: %02X", BME280.sensorID); - initialized = true; - } else { - initialized = false; - MESH_DEBUG_PRINTLN("BME280 was not found at I2C address %02X", TELEM_BME280_ADDRESS); - } - } - - bool isInitialized() const { return initialized; }; - - float getRelativeHumidity() const { - if (initialized) { - return BME280.readHumidity();; - } - } - - float getTemperature() const { - if (initialized) { - return BME280.readTemperature();; - } - } - - float getBarometricPressure() const { - if (initialized) { - return BME280.readPressure(); - } - } - - float getAltitude() const { - if (initialized) { - return BME280.readAltitude(SEALEVELPRESSURE_HPA); - } - } - - void setTemperatureCompensation(float delta) { - if (initialized) { - BME280.setTemperatureCompensation(delta); - } - } -}; diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index cf931470..6d39e905 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -1,10 +1,49 @@ #include "EnvironmentSensorManager.h" +static Adafruit_AHTX0 AHTX0; +static Adafruit_BME280 BME280; +static Adafruit_INA3221 INA3221; +static Adafruit_INA219 INA219(TELEM_INA219_ADDRESS); + bool EnvironmentSensorManager::begin() { - INA3221_sensor.begin(); - INA219_sensor.begin(); - AHTX0_sensor.begin(); - BME280_sensor.begin(); + + if (INA3221.begin(TELEM_INA3221_ADDRESS, &Wire)) { + MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", TELEM_INA3221_ADDRESS); + MESH_DEBUG_PRINTLN("%04X %04X", INA3221.getDieID(), INA3221.getManufacturerID()); + + for(int i = 0; i < 3; i++) { + INA3221.setShuntResistance(i, TELEM_INA3221_SHUNT_VALUE); + } + INA3221_initialized = true; + } else { + INA3221_initialized = false; + MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS); + } + + if (INA219.begin(&Wire)) { + MESH_DEBUG_PRINTLN("Found INA219 at address: %02X", TELEM_INA219_ADDRESS); + INA219_initialized = true; + } else { + INA219_initialized = false; + MESH_DEBUG_PRINTLN("INA219 was not found at I2C address %02X", TELEM_INA219_ADDRESS); + } + + if (AHTX0.begin(&Wire, 0, TELEM_AHTX_ADDRESS)) { + MESH_DEBUG_PRINTLN("Found AHT10/AHT20 at address: %02X", TELEM_AHTX_ADDRESS); + AHTX0_initialized = true; + } else { + AHTX0_initialized = false; + MESH_DEBUG_PRINTLN("AHT10/AHT20 was not found at I2C address %02X", TELEM_AHTX_ADDRESS); + } + + if (BME280.begin(TELEM_BME280_ADDRESS, &Wire)) { + MESH_DEBUG_PRINTLN("Found BME280 at address: %02X", TELEM_BME280_ADDRESS); + MESH_DEBUG_PRINTLN("BME sensor ID: %02X", BME280.sensorID); + BME280_initialized = true; + } else { + BME280_initialized = false; + MESH_DEBUG_PRINTLN("BME280 was not found at I2C address %02X", TELEM_BME280_ADDRESS); + } return true; } @@ -12,63 +51,40 @@ bool EnvironmentSensorManager::begin() { bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) { next_available_channel = TELEM_CHANNEL_SELF + 1; if (requester_permissions & TELEM_PERM_ENVIRONMENT) { - if (INA3221_sensor.isInitialized()) { - for(int i = 0; i < 3; i++) { + if (INA3221_initialized) { + for(int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { // add only enabled INA3221 channels to telemetry - if (INA3221_sensor.getChannelEnabled(i)) { - telemetry.addVoltage(next_available_channel, INA3221_sensor.getVoltage(i)); - telemetry.addCurrent(next_available_channel, INA3221_sensor.getCurrent(i)); - telemetry.addPower(next_available_channel, INA3221_sensor.getPower(i)); + if (INA3221.isChannelEnabled(i)) { + float voltage = INA3221.getBusVoltage(i); + float current = INA3221.getCurrentAmps(i); + telemetry.addVoltage(next_available_channel, voltage); + telemetry.addCurrent(next_available_channel, current); + telemetry.addPower(next_available_channel, voltage * current); next_available_channel++; } } } - if (INA219_sensor.isInitialized()) { - telemetry.addVoltage(next_available_channel, INA219_sensor.getVoltage()); - telemetry.addCurrent(next_available_channel, INA219_sensor.getCurrent()); - telemetry.addPower(next_available_channel, INA219_sensor.getPower()); + if (INA219_initialized) { + telemetry.addVoltage(next_available_channel, INA219.getBusVoltage_V()); + telemetry.addCurrent(next_available_channel, INA219.getCurrent_mA() / 1000); + telemetry.addPower(next_available_channel, INA219.getPower_mW() / 1000); next_available_channel++; } - if (AHTX0_sensor.isInitialized()) { - telemetry.addTemperature(TELEM_CHANNEL_SELF, AHTX0_sensor.getTemperature()); - telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, AHTX0_sensor.getRelativeHumidity()); + if (AHTX0_initialized) { + sensors_event_t humidity, temp; + AHTX0.getEvent(&humidity, &temp); + + telemetry.addTemperature(TELEM_CHANNEL_SELF, temp.temperature); + telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, humidity.relative_humidity); } - if (BME280_sensor.isInitialized()) { - telemetry.addTemperature(TELEM_CHANNEL_SELF, BME280_sensor.getTemperature()); - telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, BME280_sensor.getRelativeHumidity()); - telemetry.addBarometricPressure(TELEM_CHANNEL_SELF, BME280_sensor.getBarometricPressure()); - telemetry.addAltitude(TELEM_CHANNEL_SELF, BME280_sensor.getAltitude()); + + if (BME280_initialized) { + telemetry.addTemperature(TELEM_CHANNEL_SELF, BME280.readTemperature()); + telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, BME280.readHumidity()); + telemetry.addBarometricPressure(TELEM_CHANNEL_SELF, BME280.readPressure()); + telemetry.addAltitude(TELEM_CHANNEL_SELF, BME280.readAltitude(TELEM_BME280_SEALEVELPRESSURE_HPA)); } } return true; -} - -int EnvironmentSensorManager::getNumSettings() const { - return NUM_SENSOR_SETTINGS; -} - -const char* EnvironmentSensorManager::getSettingName(int i) const { - if (i >= 0 && i < NUM_SENSOR_SETTINGS) { - return INA3221_CHANNEL_NAMES[i]; - } - return NULL; -} - -const char* EnvironmentSensorManager::getSettingValue(int i) const { - if (i >= 0 && i < NUM_SENSOR_SETTINGS) { - return INA3221_sensor.getChannelEnabled(i) == true ? "1" : "0"; - } - return NULL; -} - -bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) { - for (int i = 0; i < NUM_SENSOR_SETTINGS; i++) { - if (strcmp(name, INA3221_CHANNEL_NAMES[i]) == 0) { - bool channel_enabled = strcmp(value, "1") == 0 ? true : false; - INA3221_sensor.setChannelEnabled(i, channel_enabled); - return true; - } - } - return false; } \ No newline at end of file diff --git a/src/helpers/sensors/EnvironmentSensorManager.h b/src/helpers/sensors/EnvironmentSensorManager.h index 318de001..2852805a 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.h +++ b/src/helpers/sensors/EnvironmentSensorManager.h @@ -1,33 +1,37 @@ #pragma once #include -#include "INA3221Sensor.h" -#include "INA219Sensor.h" -#include "AHTX0Sensor.h" -#include "BME280Sensor.h" +#include +#include +#include +#include +#include #define NUM_SENSOR_SETTINGS 3 -#define TELEM_INA3221_SETTING_CH1 "INA3221-1" -#define TELEM_INA3221_SETTING_CH2 "INA3221-2" -#define TELEM_INA3221_SETTING_CH3 "INA3221-3" +#define TELEM_AHTX_ADDRESS 0x38 // AHT10, AHT20 temperature and humidity sensor I2C address +#define TELEM_BME280_ADDRESS 0x76 // BME280 environmental sensor I2C address +#define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current sensor I2C address +#define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address + +#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts +#define TELEM_INA3221_NUM_CHANNELS 3 + +#define TELEM_INA219_SHUNT_VALUE 0.100 // shunt value in ohms (may differ between manufacturers) +#define TELEM_INA219_MAX_CURRENT 5 + +#define TELEM_BME280_SEALEVELPRESSURE_HPA (1013.25) // Athmospheric pressure at sea level class EnvironmentSensorManager : public SensorManager { -// INA3221 channels in telemetry -const char * INA3221_CHANNEL_NAMES[NUM_SENSOR_SETTINGS] = { TELEM_INA3221_SETTING_CH1, TELEM_INA3221_SETTING_CH2, TELEM_INA3221_SETTING_CH3}; - protected: int next_available_channel = TELEM_CHANNEL_SELF + 1; - INA3221Sensor INA3221_sensor; - AHTX0Sensor AHTX0_sensor; - INA219Sensor INA219_sensor; - BME280Sensor BME280_sensor; + + bool INA3221_initialized = false; + bool INA219_initialized = false; + bool BME280_initialized = false; + bool AHTX0_initialized = false; public: EnvironmentSensorManager(){}; bool begin() override; bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override; - int getNumSettings() const override; - const char* getSettingName(int i) const override; - const char* getSettingValue(int i) const override; - bool setSettingValue(const char* name, const char* value) override; }; diff --git a/src/helpers/sensors/INA3221Sensor.h b/src/helpers/sensors/INA3221Sensor.h deleted file mode 100644 index f3618a9f..00000000 --- a/src/helpers/sensors/INA3221Sensor.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include -#include - -#define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current sensor I2C address -#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts - -#define NUM_CHANNELS 3 - -static INA3221 INA_3221(TELEM_INA3221_ADDRESS, &Wire); - -class INA3221Sensor { - bool initialized = false; - -public: - void begin() { - if (INA_3221.begin()) { - MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", INA_3221.getAddress()); - MESH_DEBUG_PRINTLN("%04X %04X %04X", INA_3221.getDieID(), INA_3221.getManufacturerID(), INA_3221.getConfiguration()); - - for(int i = 0; i < 3; i++) { - INA_3221.setShuntR(i, TELEM_INA3221_SHUNT_VALUE); - } - initialized = true; - } else { - initialized = false; - MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS); - } - } - - bool isInitialized() const { return initialized; } - - int numChannels() const { return NUM_CHANNELS; } - - float getVoltage(int channel) const { - if (initialized && channel >=0 && channel < NUM_CHANNELS) { - return INA_3221.getBusVoltage(channel); - } - return 0; - } - - float getCurrent(int channel) const { - if (initialized && channel >=0 && channel < NUM_CHANNELS) { - return INA_3221.getCurrent(channel); - } - return 0; - } - - float getPower (int channel) const { - if (initialized && channel >=0 && channel < NUM_CHANNELS) { - return INA_3221.getPower(channel); - } - return 0; - } - - bool setChannelEnabled(int channel, bool enabled) { - if (initialized && channel >=0 && channel < NUM_CHANNELS) { - INA_3221.enableChannel(channel); - return true; - } - return false; - } - - bool getChannelEnabled(int channel) const { - if (initialized && channel >=0 && channel < NUM_CHANNELS) { - return INA_3221.getEnableChannel(channel); - } - return false; - } -}; diff --git a/variants/promicro/platformio.ini b/variants/promicro/platformio.ini index 7b6771e0..e70223e2 100644 --- a/variants/promicro/platformio.ini +++ b/variants/promicro/platformio.ini @@ -19,10 +19,10 @@ build_src_filter = ${nrf52840_base.build_src_filter} +<../variants/promicro> lib_deps= ${nrf52840_base.lib_deps} adafruit/Adafruit SSD1306 @ ^2.5.13 - robtillaart/INA3221 @ ^0.4.1 - robtillaart/INA219 @ ^0.4.1 - adafruit/Adafruit AHTX0@^2.0.5 - adafruit/Adafruit BME280 Library@^2.3.0 + adafruit/Adafruit INA3221 Library @ ^1.0.1 + adafruit/Adafruit INA219 @ ^1.2.3 + adafruit/Adafruit AHTX0 @ ^2.0.5 + adafruit/Adafruit BME280 Library @ ^2.3.0 [env:Faketec_Repeater] extends = Faketec @@ -124,9 +124,10 @@ build_src_filter = + +<../variants/promicro> lib_deps= ${nrf52840_base.lib_deps} - robtillaart/INA3221 @ ^0.4.1 - robtillaart/INA219 @ ^0.4.1 - adafruit/Adafruit AHTX0@^2.0.5 + adafruit/Adafruit INA3221 Library @ ^1.0.1 + adafruit/Adafruit INA219 @ ^1.2.3 + adafruit/Adafruit AHTX0 @ ^2.0.5 + adafruit/Adafruit BME280 Library @ ^2.3.0 [env:ProMicroLLCC68_Repeater] extends = ProMicroLLCC68