bme autodetect

This commit is contained in:
richonguzman 2024-05-13 14:00:07 -04:00
parent 7753618756
commit f1e712a363
7 changed files with 149 additions and 104 deletions

View file

@ -55,8 +55,10 @@
"port": 514
},
"bme": {
"active": false
},
"active": false,
"heightCorrection": 0,
"temperatureCorrection": 0.0
},
"ota": {
"username": "",
"password": ""

View file

@ -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<String> lastHeardStation;

View file

@ -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;
}
}

View file

@ -1,28 +1,17 @@
#ifndef BME_UTILS_H_
#define BME_UTILS_H_
#include <Arduino.h>
#include <Adafruit_Sensor.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
#include <Adafruit_BME280.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_BME680.h>
#include <Arduino.h>
namespace BME_Utils {
void setup();
void getWxModuleAddres();
void setup();
String generateTempString(float bmeTemp);
String generateHumString(float bmeHum);
String generatePresString(float bmePress);

View file

@ -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<int>();
bme.active = data["bme"]["active"].as<bool>();
bme.heightCorrection = data["bme"]["heightCorrection"].as<int>();
bme.temperatureCorrection = data["bme"]["temperatureCorrection"].as<float>();
ota.username = data["ota"]["username"].as<String>();
ota.password = data["ota"]["password"].as<String>();
@ -325,6 +329,8 @@ void Configuration::init() {
syslog.port = 514;
bme.active = false;
bme.heightCorrection = 0;
bme.temperatureCorrection = 0.0;
ota.username = "";
ota.password = "";

View file

@ -90,6 +90,8 @@ public:
class BME {
public:
bool active;
int heightCorrection;
float temperatureCorrection;
};
class OTA {

View file

@ -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.....";
}