Skip to content

Commit 1284ad8

Browse files
committed
added support for air sensor api
Added support for BME68x's air quality measure functionalities
1 parent bb79446 commit 1284ad8

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

src/AirQaulityClass.cpp

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 "AirQaulityClass.h"
21+
22+
// sets function called on slave write
23+
AirQaulityClass::AirQaulityClass( getRev_t getRevision)
24+
{
25+
//If board_revision = 1, IMU module is LSM6DSOX, otherwise is LSM6DS3
26+
board_revision = getRevision;
27+
}
28+
29+
AirQaulityClass::~AirQaulityClass()
30+
{
31+
}
32+
33+
int AirQaulityClass::begin()
34+
{
35+
_revision = board_revision();
36+
if (_revision == BOARD_REVISION_2) {
37+
if (iaqSensor == nullptr) {
38+
iaqSensor = new Bsec();
39+
}
40+
iaqSensor->begin(BME680_I2C_ADDR_PRIMARY, Wire);
41+
if (checkIaqSensorStatus() == STATUS_ERROR){
42+
return 0;
43+
}
44+
45+
bsec_virtual_sensor_t sensorList[10] = {
46+
BSEC_OUTPUT_RAW_TEMPERATURE,
47+
BSEC_OUTPUT_RAW_PRESSURE,
48+
BSEC_OUTPUT_RAW_HUMIDITY,
49+
BSEC_OUTPUT_RAW_GAS,
50+
BSEC_OUTPUT_IAQ,
51+
BSEC_OUTPUT_STATIC_IAQ,
52+
BSEC_OUTPUT_CO2_EQUIVALENT,
53+
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
54+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
55+
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
56+
};
57+
58+
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONTINUOUS);
59+
if (checkIaqSensorStatus() == STATUS_ERROR){
60+
return 0;
61+
}
62+
return 1;
63+
}
64+
return 0;
65+
}
66+
67+
int AirQaulityClass::checkIaqSensorStatus(void)
68+
{
69+
if (iaqSensor->status != BSEC_OK) {
70+
if (iaqSensor->status < BSEC_OK) {
71+
return STATUS_ERROR;
72+
}
73+
}
74+
75+
if (iaqSensor->bme680Status != BME680_OK) {
76+
if (iaqSensor->bme680Status < BME680_OK) {
77+
return STATUS_ERROR;
78+
}
79+
}
80+
return STATUS_OK;
81+
}
82+
83+
void AirQaulityClass::end()
84+
{
85+
if (_revision == BOARD_REVISION_2) {
86+
delete iaqSensor;
87+
iaqSensor = nullptr;
88+
}
89+
}
90+
91+
float AirQaulityClass::readVOC()
92+
{
93+
if (_revision == BOARD_REVISION_2) {
94+
while(!iaqSensor->run()){ }
95+
float reading = iaqSensor->breathVocEquivalent;
96+
return reading;
97+
}
98+
}
99+
100+
float AirQaulityClass::readGasResistor()
101+
{
102+
if (_revision == BOARD_REVISION_2) {
103+
while(!iaqSensor->run()){ }
104+
float reading = iaqSensor->gasResistance;
105+
return reading;
106+
}
107+
}
108+
109+
float AirQaulityClass::readIAQ()
110+
{
111+
if (_revision == BOARD_REVISION_2) {
112+
while(!iaqSensor->run()){ }
113+
float reading = iaqSensor->iaq;
114+
return reading;
115+
}
116+
}
117+
118+
float AirQaulityClass::readIAQAccuracy()
119+
{
120+
if (_revision == BOARD_REVISION_2) {
121+
while(!iaqSensor->run()){ }
122+
float reading = iaqSensor->iaqAccuracy;
123+
return reading;
124+
}
125+
}
126+
127+
float AirQaulityClass::readStaticIAQ()
128+
{
129+
if (_revision == BOARD_REVISION_2) {
130+
while(!iaqSensor->run()){ }
131+
float reading = iaqSensor->staticIaq;
132+
return reading;
133+
}
134+
}
135+
136+
137+
float AirQaulityClass::readCO2()
138+
{
139+
if (_revision == BOARD_REVISION_2) {
140+
while(!iaqSensor->run()){ }
141+
float reading = iaqSensor->co2Equivalent;
142+
return reading;
143+
}
144+
}
145+

src/AirQaulityClass.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _AIRQUALITYCLASS_H_INCLUDED
2+
#define _AIRQUALITYCLASS_H_INCLUDED
3+
4+
#include <Arduino.h>
5+
#include <MKRIoTCarrierDefines.h>
6+
7+
class AirQaulityClass {
8+
public:
9+
AirQaulityClass(int (*)(void));
10+
~AirQaulityClass();
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+
private:
23+
// Helper functions declarations
24+
int checkIaqSensorStatus(void);
25+
26+
Bsec *iaqSensor;
27+
//LSM6DS3Class& LSM6DS3 = IMU;
28+
//LSM6DSOXClass& LSM6DSOX = IMU;
29+
30+
private:
31+
32+
int (*board_revision)(void);
33+
int _revision;
34+
};
35+
36+
#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
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 "AirQaulityClass.h"
3031

3132
//RGB LEDs
3233
#include <Adafruit_DotStar.h>
@@ -82,6 +83,7 @@ class MKRIoTCarrier{
8283
PressureClass Pressure{MKRIoTCarrier::getBoardRevision};
8384
IMUClass IMUmodule{MKRIoTCarrier::getBoardRevision};
8485
EnvClass Env{MKRIoTCarrier::getBoardRevision};
86+
AirQaulityClass AirQuality{MKRIoTCarrier::getBoardRevision};
8587

8688
//Misc
8789
//Relays

0 commit comments

Comments
 (0)