Skip to content

Commit 7192461

Browse files
authored
Merge pull request #60 from Rocketct/master
added Air quality and imperial quantities
2 parents d8e0105 + 04c6050 commit 7192461

11 files changed

+277
-64
lines changed

keywords.txt

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Light KEYWORD1
1313
Pressure KEYWORD1
1414
IMUmodule KEYWORD1
1515
Env KEYWORD1
16+
AirQuality KEYWORD1
1617

1718
#######################################
1819
# Methods and Functions (KEYWORD2)
@@ -47,6 +48,13 @@ display KEYWORD2
4748

4849
leds KEYWORD2
4950

51+
readCO2 KEYWORD2
52+
readStaticIAQ KEYWORD2
53+
readIAQAccuracy KEYWORD2
54+
readIAQ KEYWORD2
55+
readGasResistor KEYWORD2
56+
readVOC KEYWORD2
57+
5058
#######################################
5159
# Constants (LITERAL1)
5260
#######################################

src/AirQualityClass.cpp

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
This file is part of the Arduino_MKRIoTCarrier library.
3+
Copyright (c) 2021 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "AirQualityClass.h"
21+
22+
// sets function called on slave write
23+
AirQualityClass::AirQualityClass( getRev_t getRevision)
24+
{
25+
board_revision = getRevision;
26+
}
27+
28+
AirQualityClass::~AirQualityClass()
29+
{
30+
}
31+
32+
int AirQualityClass::begin()
33+
{
34+
_revision = board_revision();
35+
if (_revision == BOARD_REVISION_2) {
36+
if (mkr_iot_carrier_rev2::iaqSensor == nullptr) {
37+
iaqSensor = new Bsec();
38+
iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire);
39+
if (checkIaqSensorStatus() == STATUS_ERROR){
40+
return 0;
41+
}
42+
43+
bsec_virtual_sensor_t sensorList[10] = {
44+
BSEC_OUTPUT_RAW_TEMPERATURE,
45+
BSEC_OUTPUT_RAW_PRESSURE,
46+
BSEC_OUTPUT_RAW_HUMIDITY,
47+
BSEC_OUTPUT_RAW_GAS,
48+
BSEC_OUTPUT_IAQ,
49+
BSEC_OUTPUT_STATIC_IAQ,
50+
BSEC_OUTPUT_CO2_EQUIVALENT,
51+
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
52+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
53+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
54+
};
55+
56+
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS);
57+
if (checkIaqSensorStatus() == STATUS_ERROR){
58+
return 0;
59+
}
60+
mkr_iot_carrier_rev2::iaqSensor = iaqSensor;
61+
} else {
62+
iaqSensor = mkr_iot_carrier_rev2::iaqSensor;
63+
}
64+
return 1;
65+
}
66+
return 0;
67+
}
68+
69+
int AirQualityClass::checkIaqSensorStatus(void)
70+
{
71+
if (iaqSensor->status != BSEC_OK) {
72+
if (iaqSensor->status < BSEC_OK) {
73+
return STATUS_ERROR;
74+
}
75+
}
76+
77+
if (iaqSensor->bme680Status != BME680_OK) {
78+
if (iaqSensor->bme680Status < BME680_OK) {
79+
return STATUS_ERROR;
80+
}
81+
}
82+
return STATUS_OK;
83+
}
84+
85+
void AirQualityClass::end()
86+
{
87+
if (_revision == BOARD_REVISION_2) {
88+
delete iaqSensor;
89+
iaqSensor = nullptr;
90+
}
91+
}
92+
93+
float AirQualityClass::readVOC()
94+
{
95+
if (_revision == BOARD_REVISION_2) {
96+
while(!iaqSensor->run()){ }
97+
float reading = iaqSensor->breathVocEquivalent;
98+
return reading;
99+
}
100+
}
101+
102+
float AirQualityClass::readGasResistor()
103+
{
104+
if (_revision == BOARD_REVISION_2) {
105+
while(!iaqSensor->run()){ }
106+
float reading = iaqSensor->gasResistance;
107+
return reading;
108+
}
109+
}
110+
111+
float AirQualityClass::readIAQ()
112+
{
113+
if (_revision == BOARD_REVISION_2) {
114+
while(!iaqSensor->run()){ }
115+
float reading = iaqSensor->iaq;
116+
return reading;
117+
}
118+
}
119+
120+
float AirQualityClass::readIAQAccuracy()
121+
{
122+
if (_revision == BOARD_REVISION_2) {
123+
while(!iaqSensor->run()){ }
124+
float reading = iaqSensor->iaqAccuracy;
125+
return reading;
126+
}
127+
}
128+
129+
float AirQualityClass::readStaticIAQ()
130+
{
131+
if (_revision == BOARD_REVISION_2) {
132+
while(!iaqSensor->run()){ }
133+
float reading = iaqSensor->staticIaq;
134+
return reading;
135+
}
136+
}
137+
138+
139+
float AirQualityClass::readCO2()
140+
{
141+
if (_revision == BOARD_REVISION_2) {
142+
while(!iaqSensor->run()){ }
143+
float reading = iaqSensor->co2Equivalent;
144+
return reading;
145+
}
146+
}
147+

src/AirQualityClass.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _AIRQUALITYCLASS_H_INCLUDED
2+
#define _AIRQUALITYCLASS_H_INCLUDED
3+
4+
#include <Arduino.h>
5+
#include <MKRIoTCarrierDefines.h>
6+
7+
class AirQualityClass {
8+
public:
9+
AirQualityClass(int (*)(void));
10+
~AirQualityClass();
11+
12+
int begin();
13+
void end();
14+
15+
float readCO2();
16+
float readStaticIAQ();
17+
float readIAQAccuracy();
18+
float readIAQ();
19+
float readGasResistor();
20+
float readVOC();
21+
22+
protected:
23+
Bsec* iaqSensor;
24+
25+
private:
26+
// Helper functions declarations
27+
int checkIaqSensorStatus(void);
28+
29+
private:
30+
31+
int (*board_revision)(void);
32+
int _revision;
33+
};
34+
35+
#endif //_AIRQUALITYCLASS_H_INCLUDED

src/Arduino_MKRIoTCarrier.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int MKRIoTCarrier::begin() {
7373
Relay2.begin();
7474

7575
//Sensors
76-
uint8_t sensorsOK = !Light.begin() << 0 | !Pressure.begin() << 1 | !IMUmodule.begin() << 2 | !Env.begin() << 3;
76+
uint8_t sensorsOK = !Light.begin() << 0 | !Pressure.begin() << 1 | !IMUmodule.begin() << 2 | !Env.begin() << 3 | !AirQuality.begin() << 4;
7777

7878

7979
//If some of the sensors are not connected

src/Arduino_MKRIoTCarrier.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <IMUClass.h> //IMU
2828
#include <PressureClass.h> //IMU
2929
#include <EnvClass.h> //IMU
30+
#include "AirQualityClass.h"
3031

3132
//RGB LEDs
3233
#include <Adafruit_DotStar.h>
@@ -38,7 +39,6 @@
3839

3940
#define BUZZER 7
4041

41-
4242
#define SD_CS 0
4343

4444
#define INT 6 //Every sensor interrupt pin , PULL-UP
@@ -82,6 +82,7 @@ class MKRIoTCarrier{
8282
PressureClass Pressure{MKRIoTCarrier::getBoardRevision};
8383
IMUClass IMUmodule{MKRIoTCarrier::getBoardRevision};
8484
EnvClass Env{MKRIoTCarrier::getBoardRevision};
85+
AirQualityClass AirQuality{MKRIoTCarrier::getBoardRevision};
8586

8687
//Misc
8788
//Relays

src/EnvClass.cpp

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
This file is part of the Arduino_LSM6DSOX library.
2+
This file is part of the Arduino_MKRIoTCarrier library.
33
Copyright (c) 2021 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -23,7 +23,6 @@
2323
// sets function called on slave write
2424
EnvClass::EnvClass( getRev_t getRevision )
2525
{
26-
//If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3
2726
board_revision = getRevision;
2827
}
2928

@@ -35,32 +34,34 @@ int EnvClass::begin()
3534
{
3635
_revision = board_revision();
3736
if (_revision == BOARD_REVISION_2) {
38-
if (iaqSensor == nullptr) {
37+
if (mkr_iot_carrier_rev2::iaqSensor == nullptr) {
3938
iaqSensor = new Bsec();
39+
iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire);
40+
if (checkIaqSensorStatus() == STATUS_ERROR){
41+
return 0;
42+
}
43+
44+
bsec_virtual_sensor_t sensorList[10] = {
45+
BSEC_OUTPUT_RAW_TEMPERATURE,
46+
BSEC_OUTPUT_RAW_PRESSURE,
47+
BSEC_OUTPUT_RAW_HUMIDITY,
48+
BSEC_OUTPUT_RAW_GAS,
49+
BSEC_OUTPUT_IAQ,
50+
BSEC_OUTPUT_STATIC_IAQ,
51+
BSEC_OUTPUT_CO2_EQUIVALENT,
52+
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
53+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
54+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
55+
};
56+
57+
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS);
58+
if (checkIaqSensorStatus() == STATUS_ERROR){
59+
return 0;
60+
}
61+
mkr_iot_carrier_rev2::iaqSensor = iaqSensor;
62+
} else {
63+
iaqSensor = mkr_iot_carrier_rev2::iaqSensor;
4064
}
41-
iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire);
42-
if (checkIaqSensorStatus() == STATUS_ERROR){
43-
return 0;
44-
}
45-
46-
bsec_virtual_sensor_t sensorList[10] = {
47-
BSEC_OUTPUT_RAW_TEMPERATURE,
48-
BSEC_OUTPUT_RAW_PRESSURE,
49-
BSEC_OUTPUT_RAW_HUMIDITY,
50-
BSEC_OUTPUT_RAW_GAS,
51-
BSEC_OUTPUT_IAQ,
52-
BSEC_OUTPUT_STATIC_IAQ,
53-
BSEC_OUTPUT_CO2_EQUIVALENT,
54-
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
55-
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
56-
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
57-
};
58-
59-
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS);
60-
if (checkIaqSensorStatus() == STATUS_ERROR){
61-
return 0;
62-
}
63-
6465
return 1;
6566
} else {
6667
if (HTS221 == nullptr) {
@@ -102,7 +103,12 @@ float EnvClass::readTemperature(int units /*= CELSIUS*/)
102103
{
103104
if (_revision == BOARD_REVISION_2) {
104105
while(!iaqSensor->run()){ }
105-
return iaqSensor->temperature;
106+
float reading = iaqSensor->temperature;
107+
if (units == FAHRENHEIT){
108+
return (reading * 9.0 / 5.0) + 32.0;
109+
} else {
110+
return reading;
111+
}
106112
}
107113
return HTS221->readTemperature(units);
108114
}

src/EnvClass.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class EnvClass {
1616
float readTemperature(int units = CELSIUS);
1717
float readHumidity();
1818

19+
protected:
20+
Bsec* iaqSensor;
21+
1922
private:
2023
int checkIaqSensorStatus(void);
2124

2225
HTS221Class* HTS221;
23-
Bsec* iaqSensor;
24-
//LSM6DS3Class& LSM6DS3 = IMU;
25-
//LSM6DSOXClass& LSM6DSOX = IMU;
2626

2727
private:
2828

src/IMUClass.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
This file is part of the Arduino_LSM6DSOX library.
2+
This file is part of the Arduino_MKRIoTCarrier library.
33
Copyright (c) 2021 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -22,7 +22,6 @@
2222
// sets function called on slave write
2323
IMUClass::IMUClass( getRev_t getRevision)
2424
{
25-
//If board_revision = 0, IMU module is LSM6DSOX, otherwise is LSM6DS3
2625
board_revision = getRevision;
2726
}
2827

@@ -33,6 +32,7 @@ IMUClass::~IMUClass()
3332
int IMUClass::begin()
3433
{
3534
_revision = board_revision();
35+
// If board_revision = BOARD_REVISION_2, IMU module is LSM6DSOX, otherwise is LSM6DS3
3636
if (_revision == BOARD_REVISION_2) {
3737
LSM6DSOX = new LSM6DSOXClass(Wire, LSM6DSOX_ADDRESS);
3838
if (LSM6DSOX == nullptr) return 0;

src/MKRIoTCarrierDefines.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace mkr_iot_carrier_rev1 {
6363
};
6464

6565
namespace mkr_iot_carrier_rev2 {
66+
static Bsec *iaqSensor = nullptr;
6667
enum relays {
6768
RELAY1 = 1,
6869
RELAY2 = 2,

0 commit comments

Comments
 (0)