Skip to content

Commit

Permalink
Merge pull request #60 from Rocketct/master
Browse files Browse the repository at this point in the history
added Air quality and imperial quantities
  • Loading branch information
facchinm authored Oct 20, 2022
2 parents d8e0105 + 04c6050 commit 7192461
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 64 deletions.
8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Light KEYWORD1
Pressure KEYWORD1
IMUmodule KEYWORD1
Env KEYWORD1
AirQuality KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -47,6 +48,13 @@ display KEYWORD2

leds KEYWORD2

readCO2 KEYWORD2
readStaticIAQ KEYWORD2
readIAQAccuracy KEYWORD2
readIAQ KEYWORD2
readGasResistor KEYWORD2
readVOC KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
Expand Down
147 changes: 147 additions & 0 deletions src/AirQualityClass.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}

35 changes: 35 additions & 0 deletions src/AirQualityClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _AIRQUALITYCLASS_H_INCLUDED
#define _AIRQUALITYCLASS_H_INCLUDED

#include <Arduino.h>
#include <MKRIoTCarrierDefines.h>

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
2 changes: 1 addition & 1 deletion src/Arduino_MKRIoTCarrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Arduino_MKRIoTCarrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <IMUClass.h> //IMU
#include <PressureClass.h> //IMU
#include <EnvClass.h> //IMU
#include "AirQualityClass.h"

//RGB LEDs
#include <Adafruit_DotStar.h>
Expand All @@ -38,7 +39,6 @@

#define BUZZER 7


#define SD_CS 0

#define INT 6 //Every sensor interrupt pin , PULL-UP
Expand Down Expand 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
Expand Down
60 changes: 33 additions & 27 deletions src/EnvClass.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/EnvClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions src/IMUClass.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/MKRIoTCarrierDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace mkr_iot_carrier_rev1 {
};

namespace mkr_iot_carrier_rev2 {
static Bsec *iaqSensor = nullptr;
enum relays {
RELAY1 = 1,
RELAY2 = 2,
Expand Down
Loading

0 comments on commit 7192461

Please sign in to comment.