From cc1a163f06793dde003884517f3efbdd665a7ecd Mon Sep 17 00:00:00 2001 From: richonguzman Date: Mon, 13 May 2024 12:13:36 -0400 Subject: [PATCH] adding new bme --- src/bme_utils.cpp | 227 +++++++++++++++++++++++++----------------- src/bme_utils.h | 19 +--- src/configuration.cpp | 3 +- src/configuration.h | 3 +- src/menu_utils.cpp | 5 +- src/station_utils.cpp | 4 +- 6 files changed, 150 insertions(+), 111 deletions(-) diff --git a/src/bme_utils.cpp b/src/bme_utils.cpp index ca62548..517fb03 100644 --- a/src/bme_utils.cpp +++ b/src/bme_utils.cpp @@ -5,7 +5,6 @@ #include #define SEALEVELPRESSURE_HPA (1013.25) -#define HEIGHT_CORRECTION 0 // in meters #define CORRECTION_FACTOR (8.2296) // for meters extern Configuration Config; @@ -13,69 +12,116 @@ extern logging::Logger logger; float newHum, newTemp, newPress, newGas; -bool bmeSensorFound = false; uint32_t bmeLastReading = -60000; +int wxModuleType = 0; +uint8_t wxModuleAddress = 0x00; -namespace BME_Utils { - #ifdef BME280Sensor - Adafruit_BME280 bme; - #endif - #ifdef BMP280Sensor - Adafruit_BMP280 bme; - #endif - #ifdef BME680Sensor - Adafruit_BME680 bme; - #endif +Adafruit_BME280 bme280; +Adafruit_BME680 bme680; +#ifdef HELTEC_V3_GPS +Adafruit_BMP280 bmp280(&Wire1); +#else +Adafruit_BMP280 bmp280; +#endif + + + +namespace BME_Utils { + + void getWxModuleAddres() { + uint8_t err, addr; + for(addr = 1; addr < 0x7F; addr++) { + #ifdef HELTEC_V3_GPS + Wire1.beginTransmission(addr); + err = Wire1.endTransmission(); + #else + Wire.beginTransmission(addr); + err = Wire.endTransmission(); + #endif + if (err == 0) { + if (addr == 0x76 || addr == 0x77) { + wxModuleAddress = addr; + // + Serial.println("Sensor encontrado : " + String(wxModuleAddress,DEC)); + // + return; + } + } + } + } void setup() { if (Config.bme.active) { - bool status; - #ifdef HELTEC_V3_GPS - status = bme.begin(0x76, &Wire1); - #else - status = bme.begin(0x76); - #endif - if (!status) { - show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", "", 2000); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME/BMP sensor Active in config but not found! Check Wiring"); - } else { - #ifdef BME280Sensor - bme.setSampling(Adafruit_BME280::MODE_FORCED, - Adafruit_BME280::SAMPLING_X1, - Adafruit_BME280::SAMPLING_X1, - Adafruit_BME280::SAMPLING_X1, - Adafruit_BME280::FILTER_OFF - ); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module init done!"); + getWxModuleAddres(); + if (wxModuleAddress != 0x00) { + bool wxModuleFound = false; + #ifdef HELTEC_V3_GPS + if (bme280.begin(wxModuleAddress, &Wire1)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME280 sensor found"); + wxModuleType = 1; + wxModuleFound = true; + } + if (!wxModuleFound) { + if (bme680.begin(wxModuleAddress, &Wire1)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME680 sensor found"); + wxModuleType = 3; + wxModuleFound = true; + } + } + #else + if (bme280.begin(wxModuleAddress)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME280 sensor found"); + wxModuleType = 1; + wxModuleFound = true; + } + if (!wxModuleFound) { + if (bme680.begin(wxModuleAddress)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME680 sensor found"); + wxModuleType = 3; + wxModuleFound = true; + } + } #endif - #ifdef BMP280Sensor - bme.setSampling(Adafruit_BMP280::MODE_FORCED, - Adafruit_BMP280::SAMPLING_X1, - Adafruit_BMP280::SAMPLING_X1, - Adafruit_BMP280::FILTER_OFF - ); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module init done!"); - #endif - #ifdef BME680Sensor - bme.setTemperatureOversampling(BME680_OS_1X); - bme.setHumidityOversampling(BME680_OS_1X); - bme.setPressureOversampling(BME680_OS_1X); - bme.setIIRFilterSize(BME680_FILTER_SIZE_0); - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BMP680 Module init done!"); - #endif - bmeSensorFound = true; - } - } else { - #ifdef BME280Sensor - logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module not active in 'tracker_conf.json'"); - #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 + if (!wxModuleFound) { + if (bmp280.begin(wxModuleAddress)) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BMP280 sensor found"); + wxModuleType = 2; + wxModuleFound = true; + } + } + if (!wxModuleFound) { + show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", "", 2000); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME/BMP sensor Active in config but not found! Check Wiring"); + } else { + switch (wxModuleType) { + case 1: + bme280.setSampling(Adafruit_BME280::MODE_FORCED, + Adafruit_BME280::SAMPLING_X1, + Adafruit_BME280::SAMPLING_X1, + Adafruit_BME280::SAMPLING_X1, + Adafruit_BME280::FILTER_OFF + ); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module init done!"); + break; + case 2: + bmp280.setSampling(Adafruit_BMP280::MODE_FORCED, + Adafruit_BMP280::SAMPLING_X1, + Adafruit_BMP280::SAMPLING_X1, + Adafruit_BMP280::FILTER_OFF + ); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module init done!"); + break; + case 3: + bme680.setTemperatureOversampling(BME680_OS_1X); + bme680.setHumidityOversampling(BME680_OS_1X); + bme680.setPressureOversampling(BME680_OS_1X); + bme680.setIIRFilterSize(BME680_FILTER_SIZE_0); + logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BMP680 Module init done!"); + break; + } + } + } } } @@ -181,31 +227,32 @@ namespace BME_Utils { String readDataSensor(String type) { String wx, tempStr, humStr, presStr; - uint32_t lastReading = millis() - bmeLastReading; if (lastReading > 60 * 1000) { - #if defined(BME280Sensor) || defined(BMP280Sensor) - bme.takeForcedMeasurement(); - newTemp = bme.readTemperature(); - newPress = (bme.readPressure() / 100.0F); - #ifdef BME280Sensor - newHum = bme.readHumidity(); - #endif - #ifdef BMP280Sensor - newHum = 0; - #endif - #endif - - #ifdef BME680Sensor - bme.performReading(); - delay(50); - if (bme.endReading()) { - newTemp = bme.temperature; - newPress = (bme.pressure / 100.0F); - newHum = bme.humidity; - newGas = bme.gas_resistance / 1000.0; // in Kilo ohms - } - #endif + switch (wxModuleType) { + case 1: // BME280 + bme280.takeForcedMeasurement(); + newTemp = bme280.readTemperature(); + newPress = (bme280.readPressure() / 100.0F); + newHum = bme280.readHumidity(); + break; + case 2: // BMP280 + bmp280.takeForcedMeasurement(); + newTemp = bmp280.readTemperature(); + newPress = (bmp280.readPressure() / 100.0F); + newHum = 0; + break; + case 3: // BME680 + bme680.performReading(); + delay(50); + if (bme680.endReading()) { + newTemp = bme680.temperature; + newPress = (bme680.pressure / 100.0F); + newHum = bme680.humidity; + newGas = bme680.gas_resistance / 1000.0; // in Kilo ohms + } + break; + } bmeLastReading = millis(); } @@ -218,26 +265,24 @@ namespace BME_Utils { } return wx; } else { - tempStr = generateTempString(newTemp, type); - #if defined(BME280Sensor) || defined(BME680Sensor) + tempStr = generateTempString(newTemp + Config.bme.temperatureCorrection, type); + if (wxModuleType == 1 || wxModuleType == 3) { humStr = generateHumString(newHum,type); - #endif - #ifdef BMP280Sensor + } else if (wxModuleType == 2) { humStr = ".."; - #endif + } presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR), type); if (type == "OLED") { - #if defined(BME280Sensor) || defined(BME680Sensor) + if (wxModuleType == 1 || wxModuleType == 3) { wx = tempStr + "C " + humStr + "% " + presStr + "hPa"; - #endif - #ifdef BMP280Sensor + } else if (wxModuleType == 2) { wx = "T: " + tempStr + "C " + "P: " + presStr + "hPa"; - #endif + } } else { wx = ".../...g...t" + tempStr + "r...p...P...h" + humStr + "b" + presStr; - #ifdef BME680Sensor + if (wxModuleType == 3) { wx += "Gas: " + String(newGas) + "Kohms"; - #endif + } } return wx; } diff --git a/src/bme_utils.h b/src/bme_utils.h index c677d1e..5ec9dd0 100644 --- a/src/bme_utils.h +++ b/src/bme_utils.h @@ -2,24 +2,15 @@ #define BME_UTILS_H_ #include +#include +#include +#include #include -#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 - -#ifdef BME280Sensor - #include -#endif -#ifdef BMP280Sensor - #include -#endif -#ifdef BME680Sensor - #include -#endif - + namespace BME_Utils { + void getWxModuleAddres(); void setup(); String generateTempString(float bmeTemp, String type); String generateHumString(float bmeHum, String type); diff --git a/src/configuration.cpp b/src/configuration.cpp index 8dadb13..fca4fac 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -54,8 +54,9 @@ void Configuration::readFile(fs::FS &fs, const char *fileName) { winlink.password = data["winlink"]["password"].as(); bme.active = data["bme"]["active"].as(); - bme.sendTelemetry = data["bme"]["sendTelemetry"].as(); bme.heightCorrection = data["bme"]["heightCorrection"].as(); + bme.temperatureCorrection = data["bme"]["temperatureCorrection"].as(); + bme.sendTelemetry = data["bme"]["sendTelemetry"].as(); notification.ledTx = data["notification"]["ledTx"].as(); notification.ledTxPin = data["notification"]["ledTxPin"].as(); diff --git a/src/configuration.h b/src/configuration.h index 87e456d..4dfbfd8 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -39,8 +39,9 @@ public: class BME { public: bool active; - bool sendTelemetry; int heightCorrection; + float temperatureCorrection; + bool sendTelemetry; }; class Notification { diff --git a/src/menu_utils.cpp b/src/menu_utils.cpp index 9d694a0..e491110 100644 --- a/src/menu_utils.cpp +++ b/src/menu_utils.cpp @@ -42,7 +42,8 @@ extern String winlinkBody; extern String winlinkAlias; extern String winlinkAliasComplete; extern bool winlinkCommentState; -extern bool bmeSensorFound; +extern int wxModuleType; +//extern bool bmeSensorFound; String freqChangeWarning; uint8_t lowBatteryPercent = 21; @@ -572,7 +573,7 @@ namespace MENU_Utils { if (time_now % 10 < 5) { fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse; } else { - if (bmeSensorFound) { + if (wxModuleType != 0) {//bmeSensorFound) { fourthRowMainMenu = BME_Utils::readDataSensor("OLED"); } else { fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse; diff --git a/src/station_utils.cpp b/src/station_utils.cpp index 259a52a..7b0b2e9 100644 --- a/src/station_utils.cpp +++ b/src/station_utils.cpp @@ -38,7 +38,7 @@ extern bool smartBeaconValue; extern uint8_t winlinkStatus; extern bool winlinkCommentState; -extern bool bmeSensorFound; +extern int wxModuleType;// bmeSensorFound; bool sendStandingUpdate = false; uint8_t updateCounter = Config.sendCommentAfterXBeacons; @@ -404,7 +404,7 @@ namespace STATION_Utils { } else { packet = APRSPacketLib::generateGPSBeaconPacket(currentBeacon->callsign, "APLRT1", Config.path, "/", APRSPacketLib::encodeGPS(gps.location.lat(),gps.location.lng(), gps.course.deg(), gps.speed.knots(), currentBeacon->symbol, Config.sendAltitude, gps.altitude.feet(), sendStandingUpdate, "Wx")); } - if (bmeSensorFound) { + if (wxModuleType != 0) {//bmeSensorFound) { packet += BME_Utils::readDataSensor("APRS"); } else { packet += ".../...g...t...r...p...P...h..b.....";