-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e49db
commit 996a035
Showing
4 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
examples/basic_config_state_ULP_LP/basic_config_state_ULP_LP.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
#include <EEPROM.h> | ||
#include "bsec.h" | ||
#include "bsec_serialized_configurations_iaq.h" | ||
|
||
#define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day | ||
|
||
// Helper functions declarations | ||
void checkIaqSensorStatus(void); | ||
void errLeds(void); | ||
void loadState(void); | ||
void updateState(void); | ||
|
||
// Create an object of the class Bsec | ||
Bsec iaqSensor; | ||
uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0}; | ||
uint16_t stateUpdateCounter = 0; | ||
|
||
String output; | ||
|
||
// Entry point for the example | ||
void setup(void) | ||
{ | ||
EEPROM.begin(BSEC_MAX_STATE_BLOB_SIZE + 1); // 1st address for the length | ||
Serial.begin(115200); | ||
|
||
iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire); | ||
output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix); | ||
Serial.println(output); | ||
checkIaqSensorStatus(); | ||
|
||
iaqSensor.setConfig(bsec_config_iaq); | ||
checkIaqSensorStatus(); | ||
|
||
loadState(); | ||
|
||
bsec_virtual_sensor_t sensorList1[2] = { | ||
BSEC_OUTPUT_RAW_GAS, | ||
BSEC_OUTPUT_IAQ, | ||
}; | ||
|
||
iaqSensor.updateSubscription(sensorList1, 2, BSEC_SAMPLE_RATE_ULP); | ||
|
||
bsec_virtual_sensor_t sensorList2[5] = { | ||
BSEC_OUTPUT_RAW_TEMPERATURE, | ||
BSEC_OUTPUT_RAW_PRESSURE, | ||
BSEC_OUTPUT_RAW_HUMIDITY, | ||
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, | ||
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, | ||
}; | ||
|
||
iaqSensor.updateSubscription(sensorList2, 5, BSEC_SAMPLE_RATE_LP); | ||
checkIaqSensorStatus(); | ||
|
||
// Print the header | ||
output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%]"; | ||
Serial.println(output); | ||
} | ||
|
||
// Function that is looped forever | ||
void loop(void) | ||
{ | ||
unsigned long time_trigger = millis(); | ||
if (iaqSensor.run()) { // If new data is available | ||
output = String(time_trigger); | ||
output += ", " + String(iaqSensor.rawTemperature); | ||
output += ", " + String(iaqSensor.pressure); | ||
output += ", " + String(iaqSensor.rawHumidity); | ||
output += ", " + String(iaqSensor.gasResistance); | ||
output += ", " + String(iaqSensor.iaq); | ||
output += ", " + String(iaqSensor.iaqAccuracy); | ||
output += ", " + String(iaqSensor.temperature); | ||
output += ", " + String(iaqSensor.humidity); | ||
Serial.println(output); | ||
updateState(); | ||
} else { | ||
checkIaqSensorStatus(); | ||
} | ||
} | ||
|
||
// Helper function definitions | ||
void checkIaqSensorStatus(void) | ||
{ | ||
if (iaqSensor.status != BSEC_OK) { | ||
if (iaqSensor.status < BSEC_OK) { | ||
output = "BSEC error code : " + String(iaqSensor.status); | ||
Serial.println(output); | ||
for (;;) | ||
errLeds(); /* Halt in case of failure */ | ||
} else { | ||
output = "BSEC warning code : " + String(iaqSensor.status); | ||
Serial.println(output); | ||
} | ||
} | ||
|
||
if (iaqSensor.bme680Status != BME680_OK) { | ||
if (iaqSensor.bme680Status < BME680_OK) { | ||
output = "BME680 error code : " + String(iaqSensor.bme680Status); | ||
Serial.println(output); | ||
for (;;) | ||
errLeds(); /* Halt in case of failure */ | ||
} else { | ||
output = "BME680 warning code : " + String(iaqSensor.bme680Status); | ||
Serial.println(output); | ||
} | ||
} | ||
} | ||
|
||
void errLeds(void) | ||
{ | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
digitalWrite(LED_BUILTIN, HIGH); | ||
delay(100); | ||
digitalWrite(LED_BUILTIN, LOW); | ||
delay(100); | ||
} | ||
|
||
void loadState(void) | ||
{ | ||
if (EEPROM.read(0) == BSEC_MAX_STATE_BLOB_SIZE) { | ||
// Existing state in EEPROM | ||
Serial.println("Reading state from EEPROM"); | ||
|
||
for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) { | ||
bsecState[i] = EEPROM.read(i + 1); | ||
Serial.println(bsecState[i], HEX); | ||
} | ||
|
||
iaqSensor.setState(bsecState); | ||
checkIaqSensorStatus(); | ||
} else { | ||
// Erase the EEPROM with zeroes | ||
Serial.println("Erasing EEPROM"); | ||
|
||
for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE + 1; i++) | ||
EEPROM.write(i, 0); | ||
|
||
EEPROM.commit(); | ||
} | ||
} | ||
|
||
void updateState(void) | ||
{ | ||
bool update = false; | ||
if (stateUpdateCounter == 0) { | ||
/* First state update when IAQ accuracy is >= 3 */ | ||
if (iaqSensor.iaqAccuracy >= 3) { | ||
update = true; | ||
stateUpdateCounter++; | ||
} | ||
} else { | ||
/* Update every STATE_SAVE_PERIOD minutes */ | ||
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) { | ||
update = true; | ||
stateUpdateCounter++; | ||
} | ||
} | ||
|
||
if (update) { | ||
iaqSensor.getState(bsecState); | ||
checkIaqSensorStatus(); | ||
|
||
Serial.println("Writing state to EEPROM"); | ||
|
||
for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE ; i++) { | ||
EEPROM.write(i + 1, bsecState[i]); | ||
Serial.println(bsecState[i], HEX); | ||
} | ||
|
||
EEPROM.write(0, BSEC_MAX_STATE_BLOB_SIZE); | ||
EEPROM.commit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.