restructure bme280 handling

This commit is contained in:
andreas 2023-11-03 17:25:12 +01:00
parent 41b9f1e675
commit f2e5115d93
2 changed files with 218 additions and 90 deletions

View File

@ -277,6 +277,17 @@
#endif #endif
#define _GWIIC #define _GWIIC
#endif #endif
#ifdef GWIIC_SDA2
#ifndef GWIIC_SCL2
#error "you must both define GWIIC_SDA2 and GWIIC_SCL2"
#endif
#endif
#ifdef GWIIC_SCL2
#ifndef GWIIC_SDA2
#error "you must both define GWIIC_SDA and GWIIC_SCL2"
#endif
#define _GWIIC
#endif
#ifndef GWLED_TYPE #ifndef GWLED_TYPE

View File

@ -13,6 +13,11 @@
#else #else
#undef _GWQMP6988 #undef _GWQMP6988
#endif #endif
#if defined(GWBME280) || defined(GWBME2801) || defined(GWBME2802)|| defined(GWBME2803)|| defined(GWBME2804)
#define _GWBME280
#else
#undef _GWBME280
#endif
#else #else
#undef _GWSHT3X #undef _GWSHT3X
#undef GWSHT3X #undef GWSHT3X
@ -26,6 +31,12 @@
#undef GWQMP69882 #undef GWQMP69882
#undef GWQMP69883 #undef GWQMP69883
#undef GWQMP69884 #undef GWQMP69884
#undef _GWBME280
#undef GWBME280
#undef GWBME2801
#undef GWBME2802
#undef GWBME2803
#undef GWBME2804
#endif #endif
#ifdef _GWSHT3X #ifdef _GWSHT3X
#include "SHT3X.h" #include "SHT3X.h"
@ -37,7 +48,7 @@
#include "N2kMessages.h" #include "N2kMessages.h"
#include "GwHardware.h" #include "GwHardware.h"
#include "GwXdrTypeMappings.h" #include "GwXdrTypeMappings.h"
#ifdef GWBME280 #ifdef _GWBME280
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#endif #endif
@ -146,6 +157,8 @@ class SensorBase{
virtual ~SensorBase(){} virtual ~SensorBase(){}
}; };
using SensorList=std::vector<SensorBase*>;
#ifndef GWIIC_SDA #ifndef GWIIC_SDA
#define GWIIC_SDA -1 #define GWIIC_SDA -1
#endif #endif
@ -162,8 +175,6 @@ class SensorBase{
#define CFG_GET(name,prefix) \ #define CFG_GET(name,prefix) \
cfg->getValue(name, GwConfigDefinitions::prefix ## name) cfg->getValue(name, GwConfigDefinitions::prefix ## name)
#define CBME280(name) \
CFG_GET(name,BME2801)
#ifdef _GWSHT3X #ifdef _GWSHT3X
class SHT3XConfig : public SensorBase{ class SHT3XConfig : public SensorBase{
@ -374,43 +385,190 @@ class QMP6988Config : public SensorBase{
}; };
#endif #endif
class BME280Config{ #ifdef _GWBME280
class BME280Config : public SensorBase{
public: public:
const String prefix="BME2801";
bool prAct=true; bool prAct=true;
bool tmAct=true; bool tmAct=true;
bool huAct=true; bool huAct=true;
tN2kTempSource tmSrc=tN2kTempSource::N2kts_InsideTemperature; tN2kTempSource tmSrc=tN2kTempSource::N2kts_InsideTemperature;
tN2kHumiditySource huSrc=tN2kHumiditySource::N2khs_InsideHumidity; tN2kHumiditySource huSrc=tN2kHumiditySource::N2khs_InsideHumidity;
tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric; tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric;
int iid=99;
long intv=2000;
String tmNam="Temperature"; String tmNam="Temperature";
String huNam="Humidity"; String huNam="Humidity";
String prNam="Pressure"; String prNam="Pressure";
float tmOff=0; float tmOff=0;
float prOff=0; float prOff=0;
BME280Config(GwConfigHandler *cfg){ Adafruit_BME280 *device=nullptr;
CBME280(prAct); uint32_t sensorId=-1;
CBME280(tmAct); BME280Config(GwApi * api, const String &prfx):SensorBase(api,prfx){
CBME280(huAct); }
CBME280(tmSrc); virtual bool isActive(){return prAct||huAct||tmAct;}
CBME280(huSrc); virtual bool initDevice(GwApi *api,TwoWire *wire){
CBME280(iid); GwLog *logger=api->getLogger();
CBME280(intv); device= new Adafruit_BME280();
intv*=1000; if (! device->begin(addr,wire)){
CBME280(tmNam); LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at %d",prefix.c_str(),addr);
CBME280(huNam); delete device;
CBME280(prNam); device=nullptr;
CBME280(tmOff); return false;
CBME280(prOff); }
if (tmOff != 0){
device->setTemperatureCompensation(tmOff);
}
sensorId=device->sensorID();
LOG_DEBUG(GwLog::LOG, "initialized %s at %d, sensorId 0x%x", prefix.c_str(), addr, sensorId);
return (huAct && sensorId == 0x60) || tmAct || prAct;
}
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str());
api->addCapability(prefix,"true");
addPressureXdr(api,*this);
addTempXdr(api,*this);
addHumidXdr(api,*this);
return isActive();
}
virtual void measure(GwApi *api, TwoWire *wire, int counterId)
{
if (!device)
return;
GwLog *logger = api->getLogger();
if (prAct)
{
float pressure = device->readPressure();
float computed = pressure + prOff;
LOG_DEBUG(GwLog::DEBUG, "%s measure %2.0fPa, computed %2.0fPa", prefix.c_str(), pressure, computed);
sendN2kPressure(api, *this, computed, counterId);
}
if (tmAct)
{
float temperature = device->readTemperature(); // offset is handled internally
temperature = CToKelvin(temperature);
LOG_DEBUG(GwLog::DEBUG, "%s measure temp=%2.1f", prefix.c_str(), temperature);
sendN2kTemperature(api, *this, temperature, counterId);
}
if (huAct && sensorId == 0x60)
{
float humidity = device->readHumidity();
LOG_DEBUG(GwLog::DEBUG, "%s read humid=%02.0f", prefix.c_str(), humidity);
sendN2kHumidity(api, *this, humidity, counterId);
}
}
virtual void readConfig(GwConfigHandler *cfg)
{
if (prefix == "BME2801")
{
busId = 1;
addr = 0x76;
#undef CG
#define CG(name) CFG_GET(name, BME2801)
CG(prAct);
CG(tmAct);
CG(huAct);
CG(tmSrc);
CG(huSrc);
CG(iid);
CG(intv);
CG(tmNam);
CG(huNam);
CG(prNam);
CG(tmOff);
CG(prOff);
}
if (prefix == "BME2802")
{
busId = 1;
addr = 0x77;
#undef CG
#define CG(name) CFG_GET(name, BME2802)
CG(prAct);
CG(tmAct);
CG(huAct);
CG(tmSrc);
CG(huSrc);
CG(iid);
CG(intv);
CG(tmNam);
CG(huNam);
CG(prNam);
CG(tmOff);
CG(prOff);
}
if (prefix == "BME2803")
{
busId = 2;
addr = 0x76;
#undef CG
#define CG(name) CFG_GET(name, BME2803)
CG(prAct);
CG(tmAct);
CG(huAct);
CG(tmSrc);
CG(huSrc);
CG(iid);
CG(intv);
CG(tmNam);
CG(huNam);
CG(prNam);
CG(tmOff);
CG(prOff);
}
if (prefix == "BME2804")
{
busId = 1;
addr = 0x77;
#undef CG
#define CG(name) CFG_GET(name, BME2804)
CG(prAct);
CG(tmAct);
CG(huAct);
CG(tmSrc);
CG(huSrc);
CG(iid);
CG(intv);
CG(tmNam);
CG(huNam);
CG(prNam);
CG(tmOff);
CG(prOff);
}
intv *= 1000;
} }
}; };
void registerBME280(GwApi *api,SensorList &sensors){
GwLog *logger=api->getLogger();
#if defined(GWBME280) || defined(GWBME2801)
BME280Config *cfg=new BME280Config(api,"BME2801");
LOG_DEBUG(GwLog::LOG,"%s configured",cfg->prefix.c_str());
sensors.push_back(cfg);
#endif
#if defined(GWBME2802)
BME280Config *cfg=new BME280Config(api,"BME2802");
LOG_DEBUG(GwLog::LOG,"%s configured",cfg->prefix.c_str());
sensors.push_back(cfg);
#endif
#if defined(GWBME2803)
BME280Config *cfg=new BME280Config(api,"BME2803");
LOG_DEBUG(GwLog::LOG,"%s configured",cfg->prefix.c_str());
sensors.push_back(cfg);
#endif
#if defined(GWBME2804)
BME280Config *cfg=new BME280Config(api,"BME2804");
LOG_DEBUG(GwLog::LOG,"%s configured",cfg->prefix.c_str());
sensors.push_back(cfg);
#endif
}
#else
void registerBME280(GwApi *api,SensorList &sensors){
}
#endif
void runIicTask(GwApi *api); void runIicTask(GwApi *api);
static std::vector<SensorBase*> sensors; static SensorList sensors;
void initIicTask(GwApi *api){ void initIicTask(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
@ -476,21 +634,7 @@ void initIicTask(GwApi *api){
sensors.push_back(scfg); sensors.push_back(scfg);
} }
#endif #endif
#ifdef GWBME280 registerBME280(api,sensors);
LOG_DEBUG(GwLog::LOG,"BME280 configured");
BME280Config bme280Config(api->getConfig());
api->addCapability(bme280Config.prefix,"true");
bool bme280Active=false;
if (addPressureXdr(api,bme280Config)) bme280Active=true;
if (addTempXdr(api,bme280Config)) bme280Active=true;
if (addHumidXdr(api,bme280Config)) bme280Active=true;
if (! bme280Active){
LOG_DEBUG(GwLog::DEBUG,"BME280 configured but disabled");
}
else{
addTask=true;
}
#endif
for (auto it=sensors.begin();it != sensors.end();it++){ for (auto it=sensors.begin();it != sensors.end();it++){
if ((*it)->preinit(api)) addTask=true; if ((*it)->preinit(api)) addTask=true;
} }
@ -517,6 +661,13 @@ void runIicTask(GwApi *api){
switch (busId) switch (busId)
{ {
case 1: case 1:
{
if (GWIIC_SDA < 0 || GWIIC_SCL < 0)
{
LOG_DEBUG(GwLog::ERROR, "IIC 1 invalid config %d,%d",
(int)GWIIC_SDA, (int)GWIIC_SCL);
}
else
{ {
bool rt = Wire.begin(GWIIC_SDA, GWIIC_SCL); bool rt = Wire.begin(GWIIC_SDA, GWIIC_SCL);
if (!rt) if (!rt)
@ -529,9 +680,18 @@ void runIicTask(GwApi *api){
buses[busId] = &Wire; buses[busId] = &Wire;
} }
} }
}
break; break;
case 2: case 2:
{ {
if (GWIIC_SDA2 < 0 || GWIIC_SCL2 < 0)
{
LOG_DEBUG(GwLog::ERROR, "IIC 2 invalid config %d,%d",
(int)GWIIC_SDA2, (int)GWIIC_SCL2);
}
else
{
bool rt = Wire1.begin(GWIIC_SDA2, GWIIC_SCL2); bool rt = Wire1.begin(GWIIC_SDA2, GWIIC_SCL2);
if (!rt) if (!rt)
{ {
@ -543,6 +703,7 @@ void runIicTask(GwApi *api){
buses[busId] = &Wire1; buses[busId] = &Wire1;
} }
} }
}
break; break;
default: default:
LOG_DEBUG(GwLog::ERROR, "invalid bus id %d at config %s", busId, (*it)->prefix.c_str()); LOG_DEBUG(GwLog::ERROR, "invalid bus id %d at config %s", busId, (*it)->prefix.c_str());
@ -571,51 +732,7 @@ void runIicTask(GwApi *api){
}); });
} }
} }
#ifdef GWBME280
int baddr=GWBME280;
if (baddr < 0) baddr=0x76;
BME280Config bme280Config(api->getConfig());
if (bme280Config.tmAct || bme280Config.prAct|| bme280Config.huAct){
Adafruit_BME280 *bme280=new Adafruit_BME280();
if (bme280->begin(baddr,&Wire)){
uint32_t sensorId=bme280->sensorID();
bool hasHumidity=sensorId == 0x60; //BME280, else BMP280
if (bme280Config.tmOff != 0){
bme280->setTemperatureCompensation(bme280Config.tmOff);
}
if (hasHumidity || bme280Config.tmAct || bme280Config.prAct)
{
LOG_DEBUG(GwLog::LOG, "initialized BME280 at %d, sensorId 0x%x", baddr, sensorId);
timers.addAction(bme280Config.intv, [logger, api, bme280, bme280Config, counterId, hasHumidity](){
if (bme280Config.prAct){
float pressure=bme280->readPressure();
float computed=pressure+bme280Config.prOff;
LOG_DEBUG(GwLog::DEBUG,"BME280 measure %2.0fPa, computed %2.0fPa",pressure,computed);
sendN2kPressure(api,bme280Config,computed,counterId);
}
if (bme280Config.tmAct){
float temperature=bme280->readTemperature(); //offset is handled internally
temperature=CToKelvin(temperature);
LOG_DEBUG(GwLog::DEBUG,"BME280 measure temp=%2.1f",temperature);
sendN2kTemperature(api,bme280Config,temperature,counterId);
}
if (bme280Config.huAct && hasHumidity){
float humidity=bme280->readHumidity();
LOG_DEBUG(GwLog::DEBUG,"BME280 read humid=%02.0f",humidity);
sendN2kHumidity(api,bme280Config,humidity,counterId);
}
});
runLoop = true;
}
else{
LOG_DEBUG(GwLog::ERROR,"BME280 only humidity active, but sensor does not have it");
}
}
else{
LOG_DEBUG(GwLog::ERROR,"unable to initialize BME280 sensor at address %d",baddr);
}
}
#endif
if (! runLoop){ if (! runLoop){
LOG_DEBUG(GwLog::LOG,"nothing to do for IIC task, finish"); LOG_DEBUG(GwLog::LOG,"nothing to do for IIC task, finish");
vTaskDelete(NULL); vTaskDelete(NULL);