Skip to content

Commit f62bf66

Browse files
committed
added support for air sensor api
Added support for BME68x's air quality measure functionalities
1 parent 9de2751 commit f62bf66

6 files changed

+195
-2
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

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
This file is part of the Arduino_LSM6DSOX 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+
//If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3
26+
board_revision = getRevision;
27+
}
28+
29+
AirQualityClass::~AirQualityClass()
30+
{
31+
}
32+
33+
int AirQualityClass::begin()
34+
{
35+
_revision = board_revision();
36+
if (_revision == BOARD_REVISION_2) {
37+
if (mkr_iot_carrier_rev2::iaqSensor == nullptr) {
38+
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;
64+
}
65+
return 1;
66+
}
67+
return 0;
68+
}
69+
70+
int AirQualityClass::checkIaqSensorStatus(void)
71+
{
72+
if (iaqSensor->status != BSEC_OK) {
73+
if (iaqSensor->status < BSEC_OK) {
74+
return STATUS_ERROR;
75+
}
76+
}
77+
78+
if (iaqSensor->bme680Status != BME680_OK) {
79+
if (iaqSensor->bme680Status < BME680_OK) {
80+
return STATUS_ERROR;
81+
}
82+
}
83+
return STATUS_OK;
84+
}
85+
86+
void AirQualityClass::end()
87+
{
88+
if (_revision == BOARD_REVISION_2) {
89+
delete iaqSensor;
90+
iaqSensor = nullptr;
91+
}
92+
}
93+
94+
float AirQualityClass::readVOC()
95+
{
96+
if (_revision == BOARD_REVISION_2) {
97+
while(!iaqSensor->run()){ }
98+
float reading = iaqSensor->breathVocEquivalent;
99+
return reading;
100+
}
101+
}
102+
103+
float AirQualityClass::readGasResistor()
104+
{
105+
if (_revision == BOARD_REVISION_2) {
106+
while(!iaqSensor->run()){ }
107+
float reading = iaqSensor->gasResistance;
108+
return reading;
109+
}
110+
}
111+
112+
float AirQualityClass::readIAQ()
113+
{
114+
if (_revision == BOARD_REVISION_2) {
115+
while(!iaqSensor->run()){ }
116+
float reading = iaqSensor->iaq;
117+
return reading;
118+
}
119+
}
120+
121+
float AirQualityClass::readIAQAccuracy()
122+
{
123+
if (_revision == BOARD_REVISION_2) {
124+
while(!iaqSensor->run()){ }
125+
float reading = iaqSensor->iaqAccuracy;
126+
return reading;
127+
}
128+
}
129+
130+
float AirQualityClass::readStaticIAQ()
131+
{
132+
if (_revision == BOARD_REVISION_2) {
133+
while(!iaqSensor->run()){ }
134+
float reading = iaqSensor->staticIaq;
135+
return reading;
136+
}
137+
}
138+
139+
140+
float AirQualityClass::readCO2()
141+
{
142+
if (_revision == BOARD_REVISION_2) {
143+
while(!iaqSensor->run()){ }
144+
float reading = iaqSensor->co2Equivalent;
145+
return reading;
146+
}
147+
}
148+

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/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)