From f1e712a363aa5b09f1a87aa99bf398e6f46406c2 Mon Sep 17 00:00:00 2001 From: richonguzman Date: Mon, 13 May 2024 14:00:07 -0400 Subject: [PATCH] bme autodetect --- data/igate_conf.json | 6 +- src/LoRa_APRS_iGate.cpp | 4 +- src/bme_utils.cpp | 206 +++++++++++++++++++++++++--------------- src/bme_utils.h | 23 ++--- src/configuration.cpp | 6 ++ src/configuration.h | 2 + src/utils.cpp | 6 +- 7 files changed, 149 insertions(+), 104 deletions(-) diff --git a/data/igate_conf.json b/data/igate_conf.json index 4260b1b..d05f67a 100644 --- a/data/igate_conf.json +++ b/data/igate_conf.json @@ -55,8 +55,10 @@ "port": 514 }, "bme": { - "active": false - }, + "active": false, + "heightCorrection": 0, + "temperatureCorrection": 0.0 + }, "ota": { "username": "", "password": "" diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index b77d61c..5982612 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -41,7 +41,7 @@ ________________________________________________________________________________ Configuration Config; WiFiClient espClient; -String versionDate = "2024.05.11"; +String versionDate = "2024.05.13"; uint8_t myWiFiAPIndex = 0; int myWiFiAPSize = Config.wifiAPs.size(); WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex]; @@ -62,8 +62,6 @@ long WiFiAutoAPTime = false; uint32_t lastBatteryCheck = 0; -uint32_t bmeLastReading = -60000; - String batteryVoltage; std::vector lastHeardStation; diff --git a/src/bme_utils.cpp b/src/bme_utils.cpp index e17a7c0..278583c 100644 --- a/src/bme_utils.cpp +++ b/src/bme_utils.cpp @@ -4,66 +4,118 @@ #include "display.h" #define SEALEVELPRESSURE_HPA (1013.25) -#define HEIGHT_CORRECTION 0 // in meters #define CORRECTION_FACTOR (8.2296) // for meters extern Configuration Config; extern String fifthLine; -extern uint32_t bmeLastReading; float newHum, newTemp, newPress, newGas; -bool bmeSensorFound = false; +int wxModuleType = 0; +uint8_t wxModuleAddress = 0x00; + +Adafruit_BME280 bme280; +Adafruit_BME680 bme680; +#ifdef HELTEC_V3_GPS +Adafruit_BMP280 bmp280(&Wire1); +#else +Adafruit_BMP280 bmp280; +#endif + namespace BME_Utils { - #ifdef BME280Sensor - Adafruit_BME280 bme; - #endif - #ifdef BMP280Sensor - Adafruit_BMP280 bme; - #endif - #ifdef BME680Sensor - Adafruit_BME680 bme; - #endif + void getWxModuleAddres() { + uint8_t err, addr; + for(addr = 1; addr < 0x7F; addr++) { + #ifdef HELTEC_V3 + Wire1.beginTransmission(addr); + err = Wire1.endTransmission(); + #else + Wire.beginTransmission(addr); + err = Wire.endTransmission(); + #endif + if (err == 0) { + if (addr == 0x76 || addr == 0x77) { + wxModuleAddress = addr; + return; + } + } + } + } void setup() { if (Config.bme.active) { - bool status; - status = bme.begin(0x76); // Don't forget to join pins for righ direction on BME280! - if (!status) { - Serial.println("Could not find a valid BME280 or BMP280 sensor, check wiring!"); - show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", 2000); - } 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 - ); - Serial.println("init : BME280 Module ... done!"); + getWxModuleAddres(); + if (wxModuleAddress != 0x00) { + bool wxModuleFound = false; + #ifdef HELTEC_V3 + if (bme280.begin(wxModuleAddress, &Wire1)) { + Serial.println("BME280 sensor found"); + wxModuleType = 1; + wxModuleFound = true; + } + if (!wxModuleFound) { + if (bme680.begin(wxModuleAddress, &Wire1)) { + Serial.println("BME680 sensor found"); + wxModuleType = 3; + wxModuleFound = true; + } + } + #else + if (bme280.begin(wxModuleAddress)) { + Serial.println("BME280 sensor found"); + wxModuleType = 1; + wxModuleFound = true; + } + if (!wxModuleFound) { + if (bme680.begin(wxModuleAddress)) { + Serial.println("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 - ); - Serial.println("init : BMP280 Module ... 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); - Serial.println("init : BME680 Module ... done!"); - #endif - bmeSensorFound = true; - } - } else { - Serial.println("(BME/BMP sensor not 'active' in 'igate_conf.json')"); + if (!wxModuleFound) { + if (bmp280.begin(wxModuleAddress)) { + Serial.println("BMP280 sensor found"); + wxModuleType = 2; + wxModuleFound = true; + } + } + if (!wxModuleFound) { + show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", 2000); + Serial.println("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 + ); + Serial.println("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 + ); + Serial.println("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); + Serial.println("BMP680 Module init done!"); + break; + } + } + } } } @@ -133,34 +185,31 @@ namespace BME_Utils { String readDataSensor() { 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(); + 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 (bme.endReading()) { - newTemp = bme.temperature; - newPress = (bme.pressure / 100.0F); - newHum = bme.humidity; - newGas = bme.gas_resistance / 1000.0; // in Kilo ohms + if (bme680.endReading()) { + newTemp = bme680.temperature; + newPress = (bme680.pressure / 100.0F); + newHum = bme680.humidity; + newGas = bme680.gas_resistance / 1000.0; // in Kilo ohms } - #endif - bmeLastReading = millis(); - } - + break; + } + if (isnan(newTemp) || isnan(newHum) || isnan(newPress)) { Serial.println("BME/BMP Module data failed"); wx = ".../...g...t...r...p...P...h..b....."; @@ -168,18 +217,17 @@ namespace BME_Utils { return wx; } else { tempStr = generateTempString((newTemp * 1.8) + 32); - #if defined(BME280Sensor) || defined(BME680Sensor) + if (wxModuleType == 1 || wxModuleType == 3) { humStr = generateHumString(newHum); - #endif - #ifdef BMP280Sensor + } else if (wxModuleType == 2) { humStr = ".."; - #endif - presStr = generatePresString(newPress + (HEIGHT_CORRECTION/CORRECTION_FACTOR)); + } + presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR)); fifthLine = "BME-> " + String(int(newTemp))+"C " + humStr + "% " + presStr.substring(0,4) + "hPa"; wx = ".../...g...t" + tempStr + "r...p...P...h" + humStr + "b" + presStr; - #ifdef BME680Sensor - wx += "Gas: " + String(newGas) + "Kohms "; - #endif + if (wxModuleType == 3) { + wx += "Gas: " + String(newGas) + "Kohms"; + } return wx; } } diff --git a/src/bme_utils.h b/src/bme_utils.h index 47a171e..4935a13 100644 --- a/src/bme_utils.h +++ b/src/bme_utils.h @@ -1,28 +1,17 @@ #ifndef BME_UTILS_H_ #define BME_UTILS_H_ -#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 +#include +#include +#include +#include namespace BME_Utils { -void setup(); - + void getWxModuleAddres(); + void setup(); String generateTempString(float bmeTemp); String generateHumString(float bmeHum); String generatePresString(float bmePress); diff --git a/src/configuration.cpp b/src/configuration.cpp index ea9b36a..83ff0d4 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -92,6 +92,8 @@ void Configuration::writeFile() { data["syslog"]["port"] = syslog.port; data["bme"]["active"] = bme.active; + data["bme"]["heightCorrection"] = bme.heightCorrection; + data["bme"]["temperatureCorrection"] = bme.temperatureCorrection; data["ota"]["username"] = ota.username; data["ota"]["password"] = ota.password; @@ -155,6 +157,8 @@ bool Configuration::readFile() { syslog.port = data["syslog"]["port"].as(); bme.active = data["bme"]["active"].as(); + bme.heightCorrection = data["bme"]["heightCorrection"].as(); + bme.temperatureCorrection = data["bme"]["temperatureCorrection"].as(); ota.username = data["ota"]["username"].as(); ota.password = data["ota"]["password"].as(); @@ -325,6 +329,8 @@ void Configuration::init() { syslog.port = 514; bme.active = false; + bme.heightCorrection = 0; + bme.temperatureCorrection = 0.0; ota.username = ""; ota.password = ""; diff --git a/src/configuration.h b/src/configuration.h index bef4ca9..51e3d3d 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -90,6 +90,8 @@ public: class BME { public: bool active; + int heightCorrection; + float temperatureCorrection; }; class OTA { diff --git a/src/utils.cpp b/src/utils.cpp index 2bede3d..e9d3851 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -36,7 +36,7 @@ extern String distance; extern uint32_t lastWiFiCheck; extern bool WiFiConnect; extern bool WiFiConnected; -extern bool bmeSensorFound; +extern int wxModuleType; namespace Utils { @@ -108,11 +108,11 @@ namespace Utils { activeStations(); - if (Config.bme.active && bmeSensorFound) { + if (Config.bme.active && wxModuleType != 0) { String sensorData = BME_Utils::readDataSensor(); beaconPacket += sensorData; secondaryBeaconPacket += sensorData; - } else if (Config.bme.active && !bmeSensorFound) { + } else if (Config.bme.active && wxModuleType == 0) { beaconPacket += ".../...g...t...r...p...P...h..b....."; secondaryBeaconPacket += ".../...g...t...r...p...P...h..b....."; }