From b92e561af7fe66cdf45812fa1b471315ae0b8c3b Mon Sep 17 00:00:00 2001 From: Riccardo Date: Wed, 19 Oct 2022 16:33:01 +0200 Subject: [PATCH 1/3] added imperial quantities support Added support for imperial quanties for pressure and IMU sensor --- src/EnvClass.cpp | 60 +++++++++++++++++++++------------------ src/EnvClass.h | 6 ++-- src/PressureClass.cpp | 66 ++++++++++++++++++++++++++----------------- src/PressureClass.h | 9 +++--- 4 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/EnvClass.cpp b/src/EnvClass.cpp index 57c971b..7477576 100644 --- a/src/EnvClass.cpp +++ b/src/EnvClass.cpp @@ -1,5 +1,5 @@ /* - This file is part of the Arduino_LSM6DSOX library. + This file is part of the Arduino_MKRIoTCarrier library. Copyright (c) 2021 Arduino SA. All rights reserved. This library is free software; you can redistribute it and/or @@ -23,7 +23,6 @@ // sets function called on slave write EnvClass::EnvClass( getRev_t getRevision ) { - //If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3 board_revision = getRevision; } @@ -35,32 +34,34 @@ int EnvClass::begin() { _revision = board_revision(); if (_revision == BOARD_REVISION_2) { - if (iaqSensor == nullptr) { + if (mkr_iot_carrier_rev2::iaqSensor == nullptr) { iaqSensor = new Bsec(); + iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } + + bsec_virtual_sensor_t sensorList[10] = { + BSEC_OUTPUT_RAW_TEMPERATURE, + BSEC_OUTPUT_RAW_PRESSURE, + BSEC_OUTPUT_RAW_HUMIDITY, + BSEC_OUTPUT_RAW_GAS, + BSEC_OUTPUT_IAQ, + BSEC_OUTPUT_STATIC_IAQ, + BSEC_OUTPUT_CO2_EQUIVALENT, + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, + }; + + iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } + mkr_iot_carrier_rev2::iaqSensor = iaqSensor; + } else { + iaqSensor = mkr_iot_carrier_rev2::iaqSensor; } - iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); - if (checkIaqSensorStatus() == STATUS_ERROR){ - return 0; - } - - bsec_virtual_sensor_t sensorList[10] = { - BSEC_OUTPUT_RAW_TEMPERATURE, - BSEC_OUTPUT_RAW_PRESSURE, - BSEC_OUTPUT_RAW_HUMIDITY, - BSEC_OUTPUT_RAW_GAS, - BSEC_OUTPUT_IAQ, - BSEC_OUTPUT_STATIC_IAQ, - BSEC_OUTPUT_CO2_EQUIVALENT, - BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, - BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, - BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, - }; - - iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); - if (checkIaqSensorStatus() == STATUS_ERROR){ - return 0; - } - return 1; } else { if (HTS221 == nullptr) { @@ -102,7 +103,12 @@ float EnvClass::readTemperature(int units /*= CELSIUS*/) { if (_revision == BOARD_REVISION_2) { while(!iaqSensor->run()){ } - return iaqSensor->temperature; + float reading = iaqSensor->temperature; + if (units == FAHRENHEIT){ + return (reading * 9.0 / 5.0) + 32.0; + } else { + return reading; + } } return HTS221->readTemperature(units); } diff --git a/src/EnvClass.h b/src/EnvClass.h index d95afee..50c14af 100644 --- a/src/EnvClass.h +++ b/src/EnvClass.h @@ -16,13 +16,13 @@ class EnvClass { float readTemperature(int units = CELSIUS); float readHumidity(); + protected: + Bsec* iaqSensor; + private: int checkIaqSensorStatus(void); HTS221Class* HTS221; - Bsec* iaqSensor; - //LSM6DS3Class& LSM6DS3 = IMU; - //LSM6DSOXClass& LSM6DSOX = IMU; private: diff --git a/src/PressureClass.cpp b/src/PressureClass.cpp index 74ab451..0ba134d 100644 --- a/src/PressureClass.cpp +++ b/src/PressureClass.cpp @@ -1,5 +1,5 @@ /* - This file is part of the Arduino_LSM6DSOX library. + This file is part of the Arduino_MKRIoTCarrier library. Copyright (c) 2021 Arduino SA. All rights reserved. This library is free software; you can redistribute it and/or @@ -22,7 +22,6 @@ // sets function called on slave write PressureClass::PressureClass( getRev_t getRevision) { - //If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3 board_revision = getRevision; } @@ -34,30 +33,33 @@ int PressureClass::begin() { _revision = board_revision(); if (_revision == BOARD_REVISION_2) { - if (iaqSensor == nullptr) { + if (mkr_iot_carrier_rev2::iaqSensor == nullptr) { iaqSensor = new Bsec(); - } - iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); - if (checkIaqSensorStatus() == STATUS_ERROR){ - return 0; - } + iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } - bsec_virtual_sensor_t sensorList[10] = { - BSEC_OUTPUT_RAW_TEMPERATURE, - BSEC_OUTPUT_RAW_PRESSURE, - BSEC_OUTPUT_RAW_HUMIDITY, - BSEC_OUTPUT_RAW_GAS, - BSEC_OUTPUT_IAQ, - BSEC_OUTPUT_STATIC_IAQ, - BSEC_OUTPUT_CO2_EQUIVALENT, - BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, - BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, - BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, - }; + bsec_virtual_sensor_t sensorList[10] = { + BSEC_OUTPUT_RAW_TEMPERATURE, + BSEC_OUTPUT_RAW_PRESSURE, + BSEC_OUTPUT_RAW_HUMIDITY, + BSEC_OUTPUT_RAW_GAS, + BSEC_OUTPUT_IAQ, + BSEC_OUTPUT_STATIC_IAQ, + BSEC_OUTPUT_CO2_EQUIVALENT, + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, + }; - iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); - if (checkIaqSensorStatus() == STATUS_ERROR){ - return 0; + iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } + mkr_iot_carrier_rev2::iaqSensor = iaqSensor; + } else { + iaqSensor = mkr_iot_carrier_rev2::iaqSensor; } return 1; } else { @@ -101,16 +103,28 @@ float PressureClass::readPressure(int units) { if (_revision == BOARD_REVISION_2) { while(!iaqSensor->run()){ } - return iaqSensor->pressure/1000; + float reading = iaqSensor->pressure/1000; + if (units == MILLIBAR) { // 1 kPa = 10 millibar + return reading * 10; + } else if (units == PSI) { // 1 kPa = 0.145038 PSI + return reading * 0.145038; + } else { + return reading; + } } return LPS22HB->readPressure(units); } -float PressureClass::readTemperature() +float PressureClass::readTemperature(int units /*= CELSIUS*/) { if (_revision == BOARD_REVISION_2) { while(!iaqSensor->run()){} - return iaqSensor->temperature; + float reading = iaqSensor->temperature; + if (units == FAHRENHEIT){ + return (reading * 9.0 / 5.0) + 32.0; + } else { + return reading; + } } return LPS22HB->readTemperature(); } diff --git a/src/PressureClass.h b/src/PressureClass.h index 36ea9a8..9b51de8 100644 --- a/src/PressureClass.h +++ b/src/PressureClass.h @@ -13,16 +13,17 @@ class PressureClass { void end(); float readPressure(int units = KILOPASCAL); - float readTemperature(void); + float readTemperature(int units = CELSIUS); + + protected: + Bsec* iaqSensor; private: // Helper functions declarations int checkIaqSensorStatus(void); LPS22HBClass* LPS22HB; - Bsec *iaqSensor; - //LSM6DS3Class& LSM6DS3 = IMU; - //LSM6DSOXClass& LSM6DSOX = IMU; + private: From c166d6ff61b1117aa7d974ebab8c4145dcecd1e2 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Wed, 19 Oct 2022 16:36:12 +0200 Subject: [PATCH 2/3] added support for air sensor api Added support for BME68x's air quality measure functionalities --- keywords.txt | 8 ++ src/AirQualityClass.cpp | 147 ++++++++++++++++++++++++++++++++++ src/AirQualityClass.h | 35 ++++++++ src/Arduino_MKRIoTCarrier.cpp | 2 +- src/Arduino_MKRIoTCarrier.h | 3 +- src/MKRIoTCarrierDefines.h | 1 + 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 src/AirQualityClass.cpp create mode 100644 src/AirQualityClass.h diff --git a/keywords.txt b/keywords.txt index 7b9e229..4a4129d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,6 +13,7 @@ Light KEYWORD1 Pressure KEYWORD1 IMUmodule KEYWORD1 Env KEYWORD1 +AirQuality KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -47,6 +48,13 @@ display KEYWORD2 leds KEYWORD2 +readCO2 KEYWORD2 +readStaticIAQ KEYWORD2 +readIAQAccuracy KEYWORD2 +readIAQ KEYWORD2 +readGasResistor KEYWORD2 +readVOC KEYWORD2 + ####################################### # Constants (LITERAL1) ####################################### diff --git a/src/AirQualityClass.cpp b/src/AirQualityClass.cpp new file mode 100644 index 0000000..4f92b4d --- /dev/null +++ b/src/AirQualityClass.cpp @@ -0,0 +1,147 @@ +/* + This file is part of the Arduino_MKRIoTCarrier library. + Copyright (c) 2021 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "AirQualityClass.h" + +// sets function called on slave write +AirQualityClass::AirQualityClass( getRev_t getRevision) +{ + board_revision = getRevision; +} + +AirQualityClass::~AirQualityClass() +{ +} + +int AirQualityClass::begin() +{ + _revision = board_revision(); + if (_revision == BOARD_REVISION_2) { + if (mkr_iot_carrier_rev2::iaqSensor == nullptr) { + iaqSensor = new Bsec(); + iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } + + bsec_virtual_sensor_t sensorList[10] = { + BSEC_OUTPUT_RAW_TEMPERATURE, + BSEC_OUTPUT_RAW_PRESSURE, + BSEC_OUTPUT_RAW_HUMIDITY, + BSEC_OUTPUT_RAW_GAS, + BSEC_OUTPUT_IAQ, + BSEC_OUTPUT_STATIC_IAQ, + BSEC_OUTPUT_CO2_EQUIVALENT, + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, + }; + + iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS); + if (checkIaqSensorStatus() == STATUS_ERROR){ + return 0; + } + mkr_iot_carrier_rev2::iaqSensor = iaqSensor; + } else { + iaqSensor = mkr_iot_carrier_rev2::iaqSensor; + } + return 1; + } + return 0; +} + +int AirQualityClass::checkIaqSensorStatus(void) +{ + if (iaqSensor->status != BSEC_OK) { + if (iaqSensor->status < BSEC_OK) { + return STATUS_ERROR; + } + } + + if (iaqSensor->bme680Status != BME680_OK) { + if (iaqSensor->bme680Status < BME680_OK) { + return STATUS_ERROR; + } + } + return STATUS_OK; +} + +void AirQualityClass::end() +{ + if (_revision == BOARD_REVISION_2) { + delete iaqSensor; + iaqSensor = nullptr; + } +} + +float AirQualityClass::readVOC() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->breathVocEquivalent; + return reading; + } +} + +float AirQualityClass::readGasResistor() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->gasResistance; + return reading; + } +} + +float AirQualityClass::readIAQ() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->iaq; + return reading; + } +} + +float AirQualityClass::readIAQAccuracy() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->iaqAccuracy; + return reading; + } +} + +float AirQualityClass::readStaticIAQ() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->staticIaq; + return reading; + } +} + + +float AirQualityClass::readCO2() +{ + if (_revision == BOARD_REVISION_2) { + while(!iaqSensor->run()){ } + float reading = iaqSensor->co2Equivalent; + return reading; + } +} + diff --git a/src/AirQualityClass.h b/src/AirQualityClass.h new file mode 100644 index 0000000..d3f458d --- /dev/null +++ b/src/AirQualityClass.h @@ -0,0 +1,35 @@ +#ifndef _AIRQUALITYCLASS_H_INCLUDED +#define _AIRQUALITYCLASS_H_INCLUDED + +#include +#include + +class AirQualityClass { + public: + AirQualityClass(int (*)(void)); + ~AirQualityClass(); + + int begin(); + void end(); + + float readCO2(); + float readStaticIAQ(); + float readIAQAccuracy(); + float readIAQ(); + float readGasResistor(); + float readVOC(); + + protected: + Bsec* iaqSensor; + + private: + // Helper functions declarations + int checkIaqSensorStatus(void); + + private: + + int (*board_revision)(void); + int _revision; +}; + +#endif //_AIRQUALITYCLASS_H_INCLUDED \ No newline at end of file diff --git a/src/Arduino_MKRIoTCarrier.cpp b/src/Arduino_MKRIoTCarrier.cpp index 44e07c0..8cecf20 100644 --- a/src/Arduino_MKRIoTCarrier.cpp +++ b/src/Arduino_MKRIoTCarrier.cpp @@ -73,7 +73,7 @@ int MKRIoTCarrier::begin() { Relay2.begin(); //Sensors - uint8_t sensorsOK = !Light.begin() << 0 | !Pressure.begin() << 1 | !IMUmodule.begin() << 2 | !Env.begin() << 3; + uint8_t sensorsOK = !Light.begin() << 0 | !Pressure.begin() << 1 | !IMUmodule.begin() << 2 | !Env.begin() << 3 | !AirQuality.begin() << 4; //If some of the sensors are not connected diff --git a/src/Arduino_MKRIoTCarrier.h b/src/Arduino_MKRIoTCarrier.h index 7c40ab3..344317d 100644 --- a/src/Arduino_MKRIoTCarrier.h +++ b/src/Arduino_MKRIoTCarrier.h @@ -27,6 +27,7 @@ #include //IMU #include //IMU #include //IMU +#include "AirQualityClass.h" //RGB LEDs #include @@ -38,7 +39,6 @@ #define BUZZER 7 - #define SD_CS 0 #define INT 6 //Every sensor interrupt pin , PULL-UP @@ -82,6 +82,7 @@ class MKRIoTCarrier{ PressureClass Pressure{MKRIoTCarrier::getBoardRevision}; IMUClass IMUmodule{MKRIoTCarrier::getBoardRevision}; EnvClass Env{MKRIoTCarrier::getBoardRevision}; + AirQualityClass AirQuality{MKRIoTCarrier::getBoardRevision}; //Misc //Relays diff --git a/src/MKRIoTCarrierDefines.h b/src/MKRIoTCarrierDefines.h index 509d49d..592f909 100644 --- a/src/MKRIoTCarrierDefines.h +++ b/src/MKRIoTCarrierDefines.h @@ -63,6 +63,7 @@ namespace mkr_iot_carrier_rev1 { }; namespace mkr_iot_carrier_rev2 { + static Bsec *iaqSensor = nullptr; enum relays { RELAY1 = 1, RELAY2 = 2, From 04c60506c676a5dabfc1ce06736fbb5134991f03 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Thu, 20 Oct 2022 14:22:32 +0200 Subject: [PATCH 3/3] Properly commented the IMU's selection on IMUClass --- src/IMUClass.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IMUClass.cpp b/src/IMUClass.cpp index bc7aee2..cc8e442 100644 --- a/src/IMUClass.cpp +++ b/src/IMUClass.cpp @@ -1,5 +1,5 @@ /* - This file is part of the Arduino_LSM6DSOX library. + This file is part of the Arduino_MKRIoTCarrier library. Copyright (c) 2021 Arduino SA. All rights reserved. This library is free software; you can redistribute it and/or @@ -22,7 +22,6 @@ // sets function called on slave write IMUClass::IMUClass( getRev_t getRevision) { - //If board_revision = 0, IMU module is LSM6DSOX, otherwise is LSM6DS3 board_revision = getRevision; } @@ -33,6 +32,7 @@ IMUClass::~IMUClass() int IMUClass::begin() { _revision = board_revision(); + // If board_revision = BOARD_REVISION_2, IMU module is LSM6DSOX, otherwise is LSM6DS3 if (_revision == BOARD_REVISION_2) { LSM6DSOX = new LSM6DSOXClass(Wire, LSM6DSOX_ADDRESS); if (LSM6DSOX == nullptr) return 0;