diff --git a/lib/hardware/GwM5Grove.in b/lib/hardware/GwM5Grove.in index 6d9ce21a..75dad36a 100644 --- a/lib/hardware/GwM5Grove.in +++ b/lib/hardware/GwM5Grove.in @@ -143,3 +143,22 @@ #error "both serial devices already in use" #endif #endif + +#GROVE +#ifdef GWBMP280G1$GS$ + #ifndef M5_GROOVEIIC$GS$ + #define M5_GROOVEIIC$GS$ + #endif + GROOVE_IIC(BMP280,$Z$,1) + #define _GWBMP280 +#endif + +#GROVE +#ifdef GWBMP280G2$GS$ + #ifndef M5_GROOVEIIC$GS$ + #define M5_GROOVEIIC$GS$ + #endif + GROOVE_IIC(BMP280,$Z$,2) + #define _GWBMP280 +#endif + diff --git a/lib/iictask/GwBME280.cpp b/lib/iictask/GwBME280.cpp index 12dcc7e8..cc241134 100644 --- a/lib/iictask/GwBME280.cpp +++ b/lib/iictask/GwBME280.cpp @@ -68,16 +68,20 @@ class BME280Config : public IICSensorBase{ if (!device) return; GwLog *logger = api->getLogger(); + float pressure = N2kDoubleNA; + float temperature = N2kDoubleNA; + float humidity = N2kDoubleNA; + float computed = N2kDoubleNA; if (prAct) { - float pressure = device->readPressure(); - float computed = pressure + prOff; + pressure = device->readPressure(); + 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 = 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); @@ -88,6 +92,10 @@ class BME280Config : public IICSensorBase{ LOG_DEBUG(GwLog::DEBUG, "%s read humid=%02.0f", prefix.c_str(), humidity); sendN2kHumidity(api, *this, humidity, counterId); } + if (tmAct || prAct || (huAct && sensorId == 0x60)) + { + sendN2kEnvironmentalParameters(api, *this, temperature, humidity, computed,counterId); + } } #define CFG280(prefix) \ CFG_GET(prAct,prefix); \ diff --git a/lib/iictask/GwBMP280.cpp b/lib/iictask/GwBMP280.cpp new file mode 100644 index 00000000..11878bfd --- /dev/null +++ b/lib/iictask/GwBMP280.cpp @@ -0,0 +1,184 @@ +#include "GwBMP280.h" +#ifdef _GWIIC + #if defined(GWBMP280) || defined(GWBMP28011) || defined(GWBMP28012)|| defined(GWBMP28021)|| defined(GWBMP28022) + #define _GWBMP280 + #else + #undef _GWBMP280 + #endif +#else + #undef _GWBMP280 + #undef GWBMP280 + #undef GWBMP28011 + #undef GWBMP28012 + #undef GWBMP28021 + #undef GWBMP28022 +#endif +#ifdef _GWBMP280 + #include +#endif +#ifdef _GWBMP280 +#define TYPE "BMP280" +#define PRFX1 TYPE "11" +#define PRFX2 TYPE "12" +#define PRFX3 TYPE "21" +#define PRFX4 TYPE "22" +class BMP280Config : public IICSensorBase{ + public: + bool prAct=true; + bool tmAct=true; + tN2kTempSource tmSrc=tN2kTempSource::N2kts_InsideTemperature; + tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric; + tN2kHumiditySource huSrc=tN2kHumiditySource::N2khs_Undef; + String tmNam="Temperature"; + String prNam="Pressure"; + float tmOff=0; + float prOff=0; + Adafruit_BMP280 *device=nullptr; + uint32_t sensorId=-1; + BMP280Config(GwApi * api, const String &prfx):SensorBase(TYPE,api,prfx){ + } + virtual bool isActive(){return prAct||tmAct;} + virtual bool initDevice(GwApi *api,TwoWire *wire){ + GwLog *logger=api->getLogger(); + //Wire.begin(GWIIC_SDA,GWIIC_SCL); + device= new Adafruit_BMP280(wire); + if (! device->begin(addr)){ + LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at %d",prefix.c_str(),addr); + delete device; + device=nullptr; + return false; + } + sensorId=device->sensorID(); + LOG_DEBUG(GwLog::LOG, "initialized %s at %d, sensorId 0x%x", prefix.c_str(), addr, sensorId); + return (sensorId == 0x56 || sensorId == 0x57 || sensorId == 0x58)?true:false; + } + 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); + return isActive(); + } + virtual void measure(GwApi *api, TwoWire *wire, int counterId) + { + if (!device) + return; + GwLog *logger = api->getLogger(); + float pressure = N2kDoubleNA; + float temperature = N2kDoubleNA; + float humidity = N2kDoubleNA; + float computed = N2kDoubleNA; + if (prAct) + { + pressure = device->readPressure(); + 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) + { + 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 (tmAct || prAct ) + { + sendN2kEnvironmentalParameters(api, *this, temperature, humidity, computed,counterId); + } + } + #define CFGBMP280(prefix) \ + CFG_GET(prAct,prefix); \ + CFG_GET(tmAct,prefix);\ + CFG_GET(tmSrc,prefix);\ + CFG_GET(iid,prefix);\ + CFG_GET(intv,prefix);\ + CFG_GET(tmNam,prefix);\ + CFG_GET(prNam,prefix);\ + CFG_GET(tmOff,prefix);\ + CFG_GET(prOff,prefix); + + virtual void readConfig(GwConfigHandler *cfg) override + { + if (prefix == PRFX1) + { + busId = 1; + addr = 0x76; + CFGBMP280(BMP28011); + ok=true; + } + if (prefix == PRFX2) + { + busId = 1; + addr = 0x77; + CFGBMP280(BMP28012); + ok=true; + } + if (prefix == PRFX3) + { + busId = 2; + addr = 0x76; + CFGBMP280(BMP28021); + ok=true; + } + if (prefix == PRFX4) + { + busId = 2; + addr = 0x77; + CFGBMP280(BMP28022); + } + intv *= 1000; + } +}; + +static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){ + return new BMP280Config(api,prfx); +}); +IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors){ + #if defined(GWBMP280) || defined(GWBMP28011) + { + auto *cfg=creator(api,PRFX1); + //BMP280Config *cfg=new BMP280Config(api,PRFX1); + sensors.add(api,cfg); + CHECK_IIC1(); + #pragma message "GWBMP28011 defined" + } + #endif + #if defined(GWBMP28012) + { + auto *cfg=creator(api,PRFX2); + //BMP280Config *cfg=new BMP280Config(api,PRFX2); + sensors.add(api,cfg); + CHECK_IIC1(); + #pragma message "GWBMP28012 defined" + } + #endif + #if defined(GWBMP28021) + { + auto *cfg=creator(api,PRFX3); + //BMP280Config *cfg=new BMP280Config(api,PRFX3); + sensors.add(api,cfg); + CHECK_IIC2(); + #pragma message "GWBMP28021 defined" + } + #endif + #if defined(GWBMP28022) + { + auto *cfg=creator(api,PRFX4); + //BMP280Config *cfg=new BMP280Config(api,PRFX4); + sensors.add(api,cfg); + CHECK_IIC1(); + #pragma message "GWBMP28022 defined" + } + #endif + return creator; +} +#else +IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors){ + return IICSensorBase::Creator(); +} + +#endif + + diff --git a/lib/iictask/GwBMP280.h b/lib/iictask/GwBMP280.h new file mode 100644 index 00000000..97fd4fe1 --- /dev/null +++ b/lib/iictask/GwBMP280.h @@ -0,0 +1,6 @@ +#ifndef _GWBMP280_H +#define _GWBMP280_H +#include "GwIicSensors.h" +IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors); +#endif + diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index dcb7f3ff..bade5d0b 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -102,6 +102,15 @@ void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){ api->increment(counterId,cfg.prefix+String("temp")); } +template +void sendN2kEnvironmentalParameters(GwApi *api,CFG &cfg,double tmValue, double huValue, double prValue, int counterId){ + tN2kMsg msg; + SetN2kEnvironmentalParameters(msg,1,cfg.tmSrc,tmValue,cfg.huSrc,huValue,prValue); + api->sendN2kMessage(msg); + api->increment(counterId,cfg.prefix+String("hum")); + api->increment(counterId,cfg.prefix+String("press")); + api->increment(counterId,cfg.prefix+String("temp")); +} #ifndef _GWI_IIC1 #define CHECK_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA) @@ -114,4 +123,4 @@ void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){ #define CHECK_IIC2() #endif -#endif \ No newline at end of file +#endif diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index fee671f2..22a7167f 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -21,6 +21,7 @@ static std::vector iicGroveList; #include "GwIicSensors.h" #include "GwHardware.h" #include "GwBME280.h" +#include "GwBMP280.h" #include "GwQMP6988.h" #include "GwSHT3X.h" #include @@ -92,6 +93,7 @@ void initIicTask(GwApi *api){ creators.push_back(registerSHT3X(api,sensors)); creators.push_back(registerQMP6988(api,sensors)); creators.push_back(registerBME280(api,sensors)); + creators.push_back(registerBMP280(api,sensors)); #ifdef _GWI_IIC1 addGroveItems(creators,api,sensors,"1",_GWI_IIC1); #endif diff --git a/lib/iictask/config.json b/lib/iictask/config.json index ec7d6130..54da57e7 100644 --- a/lib/iictask/config.json +++ b/lib/iictask/config.json @@ -530,5 +530,204 @@ } } ] + }, + { + "type": "array", + "name": "BMP280", + "replace": [ + { + "b": "1", + "i": "11", + "n": "93" + }, + { + "b": "1", + "i": "12", + "n": "92" + }, + { + "b": "2", + "i": "21", + "n": "103" + }, + { + "b": "2", + "i": "22", + "n": "102" + } + ], + "children": [ + { + "name": "BMP280$itmAct", + "label": "BMP280-$i Temp", + "type": "boolean", + "default": "true", + "description": "Enable the $i. I2C BMP280 temp sensor (bus $b)", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$itmSrc", + "label": "BMP280-$i Temp Type", + "type": "list", + "default": "2", + "description": "the NMEA2000 source type for the temperature", + "list": [ + { + "l": "SeaTemperature", + "v": "0" + }, + { + "l": "OutsideTemperature", + "v": "1" + }, + { + "l": "InsideTemperature", + "v": "2" + }, + { + "l": "EngineRoomTemperature", + "v": "3" + }, + { + "l": "MainCabinTemperature", + "v": "4" + }, + { + "l": "LiveWellTemperature", + "v": "5" + }, + { + "l": "BaitWellTemperature", + "v": "6" + }, + { + "l": "RefridgerationTemperature", + "v": "7" + }, + { + "l": "HeatingSystemTemperature", + "v": "8" + }, + { + "l": "DewPointTemperature", + "v": "9" + }, + { + "l": "ApparentWindChillTemperature", + "v": "10" + }, + { + "l": "TheoreticalWindChillTemperature", + "v": "11" + }, + { + "l": "HeatIndexTemperature", + "v": "12" + }, + { + "l": "FreezerTemperature", + "v": "13" + }, + { + "l": "ExhaustGasTemperature", + "v": "14" + }, + { + "l": "ShaftSealTemperature", + "v": "15" + } + ], + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$itmOff", + "label": "BMP280-$i Temperature Offset", + "type": "number", + "description": "offset (in °) to be added to the BMP280 temperature measurements", + "default": "0", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$iprAct", + "label": "BMP280-$i Pressure", + "type": "boolean", + "default": "true", + "description": "Enable the $i. I2C BMP280 pressure sensor (bus $b)", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$iprOff", + "label": "BMP280 Pressure Offset", + "type": "number", + "description": "offset (in pa) to be added to the BMP280 pressure measurements", + "default": "0", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$iiid", + "label": "BMP280-$i N2K iid", + "type": "number", + "default": "$n", + "description": "the N2K instance id for the BMP280 Temperature", + "category": "iicsensors$b", + "min": 0, + "max": 253, + "check": "checkMinMax", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$iintv", + "label": "BMP280-$i Interval", + "type": "number", + "default": 2, + "description": "Interval(s) to query BME280 Temperature (1...300)", + "category": "iicsensors$b", + "min": 1, + "max": 300, + "check": "checkMinMax", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$itmNam", + "label": "BMP280-$i Temp XDR", + "type": "String", + "default": "BTemp$i", + "description": "set the XDR transducer name for the BMP280 Temperature, leave empty to disable NMEA0183 XDR ", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + }, + { + "name": "BMP280$iprNam", + "label": "BMP280-$i Pressure XDR", + "type": "String", + "default": "BPressure$i", + "description": "set the XDR transducer name for the BMP280 Pressure, leave empty to disable NMEA0183 XDR", + "category": "iicsensors$b", + "capabilities": { + "BMP280$i": "true" + } + } + ] } -] \ No newline at end of file +] diff --git a/lib/iictask/platformio.ini b/lib/iictask/platformio.ini index db82892d..a7857972 100644 --- a/lib/iictask/platformio.ini +++ b/lib/iictask/platformio.ini @@ -35,3 +35,16 @@ build_flags= -D M5_GROOVEIIC -D M5_CAN_KIT ${env.build_flags} + +[env:m5stack-atom-envbps] +extends = sensors +board = m5stack-atom +lib_deps = + ${env.lib_deps} + ${sensors.lib_deps} +build_flags= + #-D M5_ENVBPS + -D GWBMP280 + -D M5_GROOVEIIC + -D M5_CAN_KIT + ${env.build_flags} diff --git a/platformio.ini b/platformio.ini index b249ef8c..f774c6bc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,8 +60,9 @@ lib_deps = Wire SPI adafruit/Adafruit BME280 Library @ 2.2.2 + adafruit/Adafruit BMP280 Library@^2.6.8 adafruit/Adafruit BusIO @ 1.14.5 - adafruit/Adafruit Unified Sensor @ 1.1.13 + adafruit/Adafruit Unified Sensor @ 1.1.13 [env:m5stack-atom] board = m5stack-atom