adding new bme

This commit is contained in:
richonguzman 2024-05-13 12:13:36 -04:00
parent c23c22f393
commit cc1a163f06
6 changed files with 150 additions and 111 deletions

View File

@ -5,7 +5,6 @@
#include <logger.h> #include <logger.h>
#define SEALEVELPRESSURE_HPA (1013.25) #define SEALEVELPRESSURE_HPA (1013.25)
#define HEIGHT_CORRECTION 0 // in meters
#define CORRECTION_FACTOR (8.2296) // for meters #define CORRECTION_FACTOR (8.2296) // for meters
extern Configuration Config; extern Configuration Config;
@ -13,69 +12,116 @@ extern logging::Logger logger;
float newHum, newTemp, newPress, newGas; float newHum, newTemp, newPress, newGas;
bool bmeSensorFound = false;
uint32_t bmeLastReading = -60000; uint32_t bmeLastReading = -60000;
int wxModuleType = 0;
uint8_t wxModuleAddress = 0x00;
namespace BME_Utils {
#ifdef BME280Sensor Adafruit_BME280 bme280;
Adafruit_BME280 bme; Adafruit_BME680 bme680;
#endif #ifdef HELTEC_V3_GPS
#ifdef BMP280Sensor Adafruit_BMP280 bmp280(&Wire1);
Adafruit_BMP280 bme; #else
#endif Adafruit_BMP280 bmp280;
#ifdef BME680Sensor #endif
Adafruit_BME680 bme;
#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() { void setup() {
if (Config.bme.active) { if (Config.bme.active) {
bool status; getWxModuleAddres();
#ifdef HELTEC_V3_GPS if (wxModuleAddress != 0x00) {
status = bme.begin(0x76, &Wire1); bool wxModuleFound = false;
#else #ifdef HELTEC_V3_GPS
status = bme.begin(0x76); if (bme280.begin(wxModuleAddress, &Wire1)) {
#endif logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME280 sensor found");
if (!status) { wxModuleType = 1;
show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", "", 2000); wxModuleFound = true;
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME/BMP sensor Active in config but not found! Check Wiring"); }
} else { if (!wxModuleFound) {
#ifdef BME280Sensor if (bme680.begin(wxModuleAddress, &Wire1)) {
bme.setSampling(Adafruit_BME280::MODE_FORCED, logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME680 sensor found");
Adafruit_BME280::SAMPLING_X1, wxModuleType = 3;
Adafruit_BME280::SAMPLING_X1, wxModuleFound = true;
Adafruit_BME280::SAMPLING_X1, }
Adafruit_BME280::FILTER_OFF }
); #else
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module init done!"); 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 #endif
#ifdef BMP280Sensor if (!wxModuleFound) {
bme.setSampling(Adafruit_BMP280::MODE_FORCED, if (bmp280.begin(wxModuleAddress)) {
Adafruit_BMP280::SAMPLING_X1, logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BMP280 sensor found");
Adafruit_BMP280::SAMPLING_X1, wxModuleType = 2;
Adafruit_BMP280::FILTER_OFF wxModuleFound = true;
); }
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module init done!"); }
#endif if (!wxModuleFound) {
#ifdef BME680Sensor show_display("ERROR", "", "BME/BMP sensor active", "but no sensor found...", "", 2000);
bme.setTemperatureOversampling(BME680_OS_1X); logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "BME", " BME/BMP sensor Active in config but not found! Check Wiring");
bme.setHumidityOversampling(BME680_OS_1X); } else {
bme.setPressureOversampling(BME680_OS_1X); switch (wxModuleType) {
bme.setIIRFilterSize(BME680_FILTER_SIZE_0); case 1:
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BMP680 Module init done!"); bme280.setSampling(Adafruit_BME280::MODE_FORCED,
#endif Adafruit_BME280::SAMPLING_X1,
bmeSensorFound = true; Adafruit_BME280::SAMPLING_X1,
} Adafruit_BME280::SAMPLING_X1,
} else { Adafruit_BME280::FILTER_OFF
#ifdef BME280Sensor );
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module not active in 'tracker_conf.json'"); logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BME", " BME280 Module init done!");
#endif break;
#ifdef BMP280Sensor case 2:
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP280 Module not active in 'tracker_conf.json'"); bmp280.setSampling(Adafruit_BMP280::MODE_FORCED,
#endif Adafruit_BMP280::SAMPLING_X1,
#ifdef BME680Sensor Adafruit_BMP280::SAMPLING_X1,
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BMP", " BMP680 Module not active in 'tracker_conf.json'"); Adafruit_BMP280::FILTER_OFF
#endif );
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 readDataSensor(String type) {
String wx, tempStr, humStr, presStr; String wx, tempStr, humStr, presStr;
uint32_t lastReading = millis() - bmeLastReading; uint32_t lastReading = millis() - bmeLastReading;
if (lastReading > 60 * 1000) { if (lastReading > 60 * 1000) {
#if defined(BME280Sensor) || defined(BMP280Sensor) switch (wxModuleType) {
bme.takeForcedMeasurement(); case 1: // BME280
newTemp = bme.readTemperature(); bme280.takeForcedMeasurement();
newPress = (bme.readPressure() / 100.0F); newTemp = bme280.readTemperature();
#ifdef BME280Sensor newPress = (bme280.readPressure() / 100.0F);
newHum = bme.readHumidity(); newHum = bme280.readHumidity();
#endif break;
#ifdef BMP280Sensor case 2: // BMP280
newHum = 0; bmp280.takeForcedMeasurement();
#endif newTemp = bmp280.readTemperature();
#endif newPress = (bmp280.readPressure() / 100.0F);
newHum = 0;
#ifdef BME680Sensor break;
bme.performReading(); case 3: // BME680
delay(50); bme680.performReading();
if (bme.endReading()) { delay(50);
newTemp = bme.temperature; if (bme680.endReading()) {
newPress = (bme.pressure / 100.0F); newTemp = bme680.temperature;
newHum = bme.humidity; newPress = (bme680.pressure / 100.0F);
newGas = bme.gas_resistance / 1000.0; // in Kilo ohms newHum = bme680.humidity;
} newGas = bme680.gas_resistance / 1000.0; // in Kilo ohms
#endif }
break;
}
bmeLastReading = millis(); bmeLastReading = millis();
} }
@ -218,26 +265,24 @@ namespace BME_Utils {
} }
return wx; return wx;
} else { } else {
tempStr = generateTempString(newTemp, type); tempStr = generateTempString(newTemp + Config.bme.temperatureCorrection, type);
#if defined(BME280Sensor) || defined(BME680Sensor) if (wxModuleType == 1 || wxModuleType == 3) {
humStr = generateHumString(newHum,type); humStr = generateHumString(newHum,type);
#endif } else if (wxModuleType == 2) {
#ifdef BMP280Sensor
humStr = ".."; humStr = "..";
#endif }
presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR), type); presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR), type);
if (type == "OLED") { if (type == "OLED") {
#if defined(BME280Sensor) || defined(BME680Sensor) if (wxModuleType == 1 || wxModuleType == 3) {
wx = tempStr + "C " + humStr + "% " + presStr + "hPa"; wx = tempStr + "C " + humStr + "% " + presStr + "hPa";
#endif } else if (wxModuleType == 2) {
#ifdef BMP280Sensor
wx = "T: " + tempStr + "C " + "P: " + presStr + "hPa"; wx = "T: " + tempStr + "C " + "P: " + presStr + "hPa";
#endif }
} else { } else {
wx = ".../...g...t" + tempStr + "r...p...P...h" + humStr + "b" + presStr; wx = ".../...g...t" + tempStr + "r...p...P...h" + humStr + "b" + presStr;
#ifdef BME680Sensor if (wxModuleType == 3) {
wx += "Gas: " + String(newGas) + "Kohms"; wx += "Gas: " + String(newGas) + "Kohms";
#endif }
} }
return wx; return wx;
} }

View File

@ -2,24 +2,15 @@
#define BME_UTILS_H_ #define BME_UTILS_H_
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_BME680.h>
#include <Arduino.h> #include <Arduino.h>
#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 <Adafruit_BME280.h>
#endif
#ifdef BMP280Sensor
#include <Adafruit_BMP280.h>
#endif
#ifdef BME680Sensor
#include <Adafruit_BME680.h>
#endif
namespace BME_Utils { namespace BME_Utils {
void getWxModuleAddres();
void setup(); void setup();
String generateTempString(float bmeTemp, String type); String generateTempString(float bmeTemp, String type);
String generateHumString(float bmeHum, String type); String generateHumString(float bmeHum, String type);

View File

@ -54,8 +54,9 @@ void Configuration::readFile(fs::FS &fs, const char *fileName) {
winlink.password = data["winlink"]["password"].as<String>(); winlink.password = data["winlink"]["password"].as<String>();
bme.active = data["bme"]["active"].as<bool>(); bme.active = data["bme"]["active"].as<bool>();
bme.sendTelemetry = data["bme"]["sendTelemetry"].as<bool>();
bme.heightCorrection = data["bme"]["heightCorrection"].as<int>(); bme.heightCorrection = data["bme"]["heightCorrection"].as<int>();
bme.temperatureCorrection = data["bme"]["temperatureCorrection"].as<float>();
bme.sendTelemetry = data["bme"]["sendTelemetry"].as<bool>();
notification.ledTx = data["notification"]["ledTx"].as<bool>(); notification.ledTx = data["notification"]["ledTx"].as<bool>();
notification.ledTxPin = data["notification"]["ledTxPin"].as<int>(); notification.ledTxPin = data["notification"]["ledTxPin"].as<int>();

View File

@ -39,8 +39,9 @@ public:
class BME { class BME {
public: public:
bool active; bool active;
bool sendTelemetry;
int heightCorrection; int heightCorrection;
float temperatureCorrection;
bool sendTelemetry;
}; };
class Notification { class Notification {

View File

@ -42,7 +42,8 @@ extern String winlinkBody;
extern String winlinkAlias; extern String winlinkAlias;
extern String winlinkAliasComplete; extern String winlinkAliasComplete;
extern bool winlinkCommentState; extern bool winlinkCommentState;
extern bool bmeSensorFound; extern int wxModuleType;
//extern bool bmeSensorFound;
String freqChangeWarning; String freqChangeWarning;
uint8_t lowBatteryPercent = 21; uint8_t lowBatteryPercent = 21;
@ -572,7 +573,7 @@ namespace MENU_Utils {
if (time_now % 10 < 5) { if (time_now % 10 < 5) {
fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse; fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse;
} else { } else {
if (bmeSensorFound) { if (wxModuleType != 0) {//bmeSensorFound) {
fourthRowMainMenu = BME_Utils::readDataSensor("OLED"); fourthRowMainMenu = BME_Utils::readDataSensor("OLED");
} else { } else {
fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse; fourthRowMainMenu = "A=" + fourthRowAlt + "m " + fourthRowSpeed + "km/h " + fourthRowCourse;

View File

@ -38,7 +38,7 @@ extern bool smartBeaconValue;
extern uint8_t winlinkStatus; extern uint8_t winlinkStatus;
extern bool winlinkCommentState; extern bool winlinkCommentState;
extern bool bmeSensorFound; extern int wxModuleType;// bmeSensorFound;
bool sendStandingUpdate = false; bool sendStandingUpdate = false;
uint8_t updateCounter = Config.sendCommentAfterXBeacons; uint8_t updateCounter = Config.sendCommentAfterXBeacons;
@ -404,7 +404,7 @@ namespace STATION_Utils {
} else { } 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")); 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"); packet += BME_Utils::readDataSensor("APRS");
} else { } else {
packet += ".../...g...t...r...p...P...h..b....."; packet += ".../...g...t...r...p...P...h..b.....";