LoRa_APRS_iGate/src/bme_utils.cpp

239 lines
8.3 KiB
C++
Raw Normal View History

2023-06-18 00:28:40 +02:00
#include "bme_utils.h"
#include "configuration.h"
#include "gps_utils.h"
#include "display.h"
2023-08-05 17:20:06 +02:00
#define SEALEVELPRESSURE_HPA (1013.25)
#define CORRECTION_FACTOR (8.2296) // for meters
2023-07-16 22:47:24 +02:00
2024-05-14 05:30:15 +02:00
extern Configuration Config;
extern String fifthLine;
2023-06-18 00:28:40 +02:00
2024-05-13 20:00:07 +02:00
int wxModuleType = 0;
uint8_t wxModuleAddress = 0x00;
2024-05-14 05:30:15 +02:00
float newHum, newTemp, newPress, newGas;
2024-05-13 20:00:07 +02:00
Adafruit_BME280 bme280;
2024-05-14 08:01:24 +02:00
#ifdef HELTEC_V3
2024-05-13 20:00:07 +02:00
Adafruit_BMP280 bmp280(&Wire1);
#else
Adafruit_BMP280 bmp280;
2024-05-14 17:43:29 +02:00
Adafruit_BME680 bme680;
2024-05-13 20:00:07 +02:00
#endif
2024-05-08 21:05:43 +02:00
2024-05-14 14:47:57 +02:00
2023-06-18 00:28:40 +02:00
namespace BME_Utils {
2024-05-13 20:00:07 +02:00
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) {
//Serial.println(addr); this shows any connected board to I2C
2024-05-13 20:00:07 +02:00
if (addr == 0x76 || addr == 0x77) {
wxModuleAddress = addr;
return;
}
}
}
}
2023-06-18 00:28:40 +02:00
2024-02-24 14:09:05 +01:00
void setup() {
if (Config.bme.active) {
2024-05-13 20:00:07 +02:00
getWxModuleAddres();
if (wxModuleAddress != 0x00) {
bool wxModuleFound = false;
#ifdef HELTEC_V3
if (bme280.begin(wxModuleAddress, &Wire1)) {
Serial.println("BME280 sensor found");
wxModuleType = 1;
wxModuleFound = true;
}
#else
if (bme280.begin(wxModuleAddress)) {
Serial.println("BME280 sensor found");
wxModuleType = 1;
wxModuleFound = true;
}
2024-05-14 17:43:29 +02:00
if (!wxModuleFound) {
if (bme680.begin(wxModuleAddress)) {
Serial.println("BME680 sensor found");
wxModuleType = 3;
wxModuleFound = true;
}
}
2024-02-24 14:09:05 +01:00
#endif
2024-05-14 08:01:24 +02:00
if (!wxModuleFound) {
2024-05-14 14:47:57 +02:00
if (bmp280.begin(wxModuleAddress)) {
Serial.println("BMP280 sensor found");
wxModuleType = 2;
2024-05-14 08:01:24 +02:00
wxModuleFound = true;
}
}
2024-05-13 20:00:07 +02:00
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;
2024-05-14 17:43:29 +02:00
case 3:
#ifndef HELTEC_V3
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!");
#endif
break;
2024-05-13 20:00:07 +02:00
}
}
}
2024-02-24 14:09:05 +01:00
}
2023-06-18 00:28:40 +02:00
}
2024-06-06 05:49:16 +02:00
const String generateTempString(const float bmeTemp) {
2024-02-24 14:09:05 +01:00
String strTemp;
strTemp = String((int)bmeTemp);
switch (strTemp.length()) {
case 1:
return "00" + strTemp;
case 2:
return "0" + strTemp;
case 3:
return strTemp;
default:
return "-999";
}
2023-12-24 15:30:19 +01:00
}
2023-06-18 00:28:40 +02:00
2024-06-06 05:49:16 +02:00
const String generateHumString(const float bmeHum) {
2024-02-24 14:09:05 +01:00
String strHum;
strHum = String((int)bmeHum);
switch (strHum.length()) {
case 1:
return "0" + strHum;
case 2:
return strHum;
case 3:
if ((int)bmeHum == 100) {
2024-05-14 08:01:24 +02:00
return "00";
2024-02-24 14:09:05 +01:00
} else {
2024-05-14 08:01:24 +02:00
return "-99";
2024-02-24 14:09:05 +01:00
}
default:
return "-99";
2023-12-24 15:30:19 +01:00
}
}
2023-06-18 00:28:40 +02:00
2024-06-06 05:49:16 +02:00
const String generatePresString(const float bmePress) {
String strPress = String((int)bmePress);
String decPress = String(int((bmePress - int(bmePress)) * 10));
2024-02-24 14:09:05 +01:00
switch (strPress.length()) {
case 1:
return "000" + strPress + decPress;
2024-02-24 14:09:05 +01:00
case 2:
return "00" + strPress + decPress;
2024-02-24 14:09:05 +01:00
case 3:
return "0" + strPress + decPress;
2024-02-24 14:09:05 +01:00
case 4:
return strPress + decPress;
2024-02-24 14:09:05 +01:00
case 5:
return strPress;
default:
return "-99999";
}
2023-12-24 15:30:19 +01:00
}
2023-06-18 00:28:40 +02:00
2024-06-06 05:49:16 +02:00
const String readDataSensor() {
2024-02-24 14:09:05 +01:00
String wx, tempStr, humStr, presStr;
2024-05-13 20:00:07 +02:00
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;
2024-05-14 17:43:29 +02:00
case 3: // BME680
#ifndef HELTEC_V3
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
}
#endif
break;
2024-05-13 20:00:07 +02:00
}
2024-02-24 14:09:05 +01:00
if (isnan(newTemp) || isnan(newHum) || isnan(newPress)) {
Serial.println("BME/BMP Module data failed");
wx = ".../...g...t...r...p...P...h..b.....";
fifthLine = "";
return wx;
} else {
2024-05-13 20:40:30 +02:00
tempStr = generateTempString(((newTemp + Config.bme.temperatureCorrection) * 1.8) + 32);
2024-05-13 20:00:07 +02:00
if (wxModuleType == 1 || wxModuleType == 3) {
2024-05-11 18:59:07 +02:00
humStr = generateHumString(newHum);
2024-05-13 20:00:07 +02:00
} else if (wxModuleType == 2) {
2024-05-11 18:59:07 +02:00
humStr = "..";
2024-05-13 20:00:07 +02:00
}
presStr = generatePresString(newPress + (Config.bme.heightCorrection/CORRECTION_FACTOR));
2024-05-30 22:27:07 +02:00
fifthLine = "BME-> ";
fifthLine += String(int(newTemp + Config.bme.temperatureCorrection));
fifthLine += "C ";
fifthLine += humStr;
fifthLine += "% ";
fifthLine += presStr.substring(0,4);
fifthLine += "hPa";
wx = ".../...g...t";
wx += tempStr;
wx += "r...p...P...h";
wx += humStr;
wx += "b";
wx += presStr;
2024-05-13 20:00:07 +02:00
if (wxModuleType == 3) {
2024-05-30 22:27:07 +02:00
wx += "Gas: ";
wx += String(newGas);
wx += "Kohms";
2024-05-13 20:00:07 +02:00
}
2024-02-24 14:09:05 +01:00
return wx;
}
2023-12-24 15:30:19 +01:00
}
2023-06-18 00:28:40 +02:00
}