From 444f307f49a0dcf02053d5832ac9b06183f1a692 Mon Sep 17 00:00:00 2001 From: richonguzman Date: Sun, 24 Dec 2023 10:03:00 -0300 Subject: [PATCH] updated Bat Reading and BME680 Test --- platformio.ini | 1 + src/bme_utils.cpp | 49 +++++++++++++++++++++++++++++++++------------ src/bme_utils.h | 12 ++++++++--- src/power_utils.cpp | 7 ++++--- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/platformio.ini b/platformio.ini index c02465a..0745ed2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,6 +34,7 @@ lib_deps = adafruit/Adafruit Unified Sensor@^1.1.9 adafruit/Adafruit BME280 Library@^2.2.2 adafruit/Adafruit BMP280 Library@^2.6.8 + adafruit/Adafruit BME680 Library@^2.0.4 h2zero/NimBLE-Arduino@^1.4.1 check_tool = cppcheck diff --git a/src/bme_utils.cpp b/src/bme_utils.cpp index 84e02a4..802d575 100644 --- a/src/bme_utils.cpp +++ b/src/bme_utils.cpp @@ -14,11 +14,15 @@ extern logging::Logger logger; namespace BME_Utils { - #ifndef BMPSensor + #ifdef BME280Sensor Adafruit_BME280 bme; - #else + #endif + #ifdef BMP280Sensor Adafruit_BMP280 bme; #endif + #ifdef BME680Sensor + Adafruit_BME680 bme; + #endif void setup() { if (Config.bme.active) { @@ -29,18 +33,26 @@ namespace BME_Utils { logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME280 Active in config but not found! Check Wiring"); while (1); } else { - #ifndef BMPSensor + #ifdef BME280Sensor logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module init done!"); - #else + #endif + #ifdef BMP280Sensor logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module init done!"); #endif + #ifdef BME680Sensor + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BMP680 Module init done!"); + #endif } } else { - #ifndef BMPSensor + #ifdef BME280Sensor logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module not active in 'tracker_conf.json'"); - #else + #endif + #ifdef BMP280Sensor logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module not active in 'tracker_conf.json'"); #endif + #ifdef BME680Sensor + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP680 Module not active in 'tracker_conf.json'"); + #endif } } @@ -146,14 +158,20 @@ namespace BME_Utils { String readDataSensor(String type) { String wx, tempStr, humStr, presStr; - float newTemp = bme.readTemperature(); float newHum; - #ifndef BMPSensor + + float newTemp = bme.readTemperature(); + #if defined(BME280Sensor) || defined(BME680Sensor) newHum = bme.readHumidity(); - #else + #endif + #ifdef BMP280Sensor newHum = 0; #endif float newPress = (bme.readPressure() / 100.0F); + + #ifdef BME680Sensor + float newGas = bme.gas_resistance / 1000.0; // in Kilo ohms + #endif if (isnan(newTemp) || isnan(newHum) || isnan(newPress)) { Serial.println("BME280 Module data failed"); @@ -165,20 +183,25 @@ namespace BME_Utils { return wx; } else { tempStr = generateTempString(newTemp, type); - #ifndef BMPSensor + #if defined(BME280Sensor) || defined(BME680Sensor) humStr = generateHumString(newHum,type); - #else + #endif + #ifdef BMP280Sensor humStr = ".."; #endif presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR), type); if (type == "OLED") { - #ifndef BMPSensor + #if defined(BME280Sensor) || defined(BME680Sensor) wx = tempStr + "C " + humStr + "% " + presStr + "hPa"; - #else + #endif + #ifdef BMP280Sensor wx = "T: " + tempStr + "C " + "P: " + presStr + "hPa"; #endif } else { wx = ".../...g...t" + tempStr + "r...p...P...h" + humStr + "b" + presStr; + #ifdef BME680Sensor + wx += "Gas: " + String(newGas) + " k ohms"; + #endif } return wx; } diff --git a/src/bme_utils.h b/src/bme_utils.h index 93c7f03..8d95f1b 100644 --- a/src/bme_utils.h +++ b/src/bme_utils.h @@ -4,13 +4,19 @@ #include #include -//#define BMPSensor // uncoment this line if BMP280 Module is connected instead of BME280 +//#define BME280Sensor // its set by default but you should comment it with "//" +//#define BMP280Sensor // and delete "//" from the one you want to use. +#define BME680Sensor -#ifndef BMPSensor +#ifdef BME280Sensor #include -#else +#endif +#ifdef BMP280Sensor #include #endif +#ifdef BME680Sensor +#include +#endif namespace BME_Utils { diff --git a/src/power_utils.cpp b/src/power_utils.cpp index 89be328..dcd670b 100644 --- a/src/power_utils.cpp +++ b/src/power_utils.cpp @@ -9,6 +9,8 @@ extern Configuration Config; extern logging::Logger logger; extern bool disableGPS; +int lora32BatReadingCorr = 6; // % of correction to higher value to reflect the real battery voltage (adjust this to your needs) + // cppcheck-suppress unusedFunction bool PowerManagement::begin(TwoWire &port) { #if defined(TTGO_T_Beam_V0_7) || defined(ESP32_DIY_LoRa_GPS) || defined(TTGO_T_LORA_V2_1_GPS) || defined(TTGO_T_LORA_V2_1_TNC) || defined(ESP32_DIY_1W_LoRa_GPS) @@ -139,11 +141,10 @@ double PowerManagement::getBatteryVoltage() { // the measured voltage is inaccurate du to known nonlinearity and ~100mV offset of the ESP32 A/D converter int adc_value; double voltage; -// analogSetAttenuation(ADC_11db); // this is the default setting (range 0-3.3V) - adc_value = analogRead(35); // ADC1_CHANNEL_7 + adc_value = analogRead(35); voltage = (adc_value * 3.3 ) / 4095; voltage += 0.1; // add 0.1V offset - voltage = 2 * voltage; // multiply result by two because the voltage was measured after the 1:2 divider + voltage = 2 * voltage * (1 + lora32BatReadingCorr/100) ; // multiply result by two because the voltage was measured after the 1:2 divider return voltage; #endif #if defined(TTGO_T_Beam_V1_0) || defined(TTGO_T_Beam_V1_0_SX1268)