uses BBC micro:bit to measure and display indoor air quality using Bosch BME680 and/or Sensirion SGP30
uses Bosch BME680 and/or Sensirion SGP30 sensors to measure indor air quality
sensors should be connected to BBC micro:bit using i2c
commands are received and data is being sent using uBit / nordic radio protocol
display
---
last line always indicates: - first dot: bme680 detected - second dot: sgp30 detected - third dot: sgp 30 setting humidity/temperature - fourth dor: sgp30 measuring - fith dot: bme680 measuring
the detect dots should be in a stable state (not blinking) the measuring dots should be blinking (constant light means: measurement failed)
if only one bme680 is present: - first 3 lines indicate gas resistence (air quality / more dots == worse quality) - fourth line indicates humidity level
if only sgp30 is present: - first two lines indicate SGP30 VOC level - third and fourth line indicate sgp30 CO2 level
if both sensors are present: - first line indicates SGP30 VOC level - second line line indicates sgp30 CO2 level - third line indicates bme680 gas resistence (air quality) - fourth line indicates bme 680 humidity level
buttons
-
B display state, switches betweeen
- full bright
- low light
- display off
AB reset sgp30 baseline in non volatile storage
data logging
--
during measurements the minimum and mximum values for each measured value
(temperature, air pressure, humidity,gas resistance, VOC, CO2)
are being stored in non volatile storage
those (and the last measurement results) are being shown when btn A has been pressed
Diff: NvStore/IaqNonVolatileStore.cpp
- Revision:
- 49:bbb506b58e6e
- Parent:
- 47:881bfe77a00a
- Child:
- 50:63442fd5e709
--- a/NvStore/IaqNonVolatileStore.cpp Thu Mar 21 13:09:58 2019 +0000 +++ b/NvStore/IaqNonVolatileStore.cpp Sun Jan 16 15:42:13 2022 +0000 @@ -1,8 +1,6 @@ #include "IaqNonVolatileStore.h" #include "MovingAverage.h" -#include "StringTool.h" - /* from microbit.storage: Key Value pairs have a fixed length key of 16 bytes, and a fixed length value of 32 bytes. @@ -10,27 +8,25 @@ which means that 21 Key Value pairs can be stored. */ -#define GAS_MAX_STORE_KEY "bme_max_res" // 10 indexed values -#define GAS_MIN_STORE_KEY "bme_min_res" // 11 -#define TEMP_MAX_STORE_KEY "temp_max" // 12 -#define TEMP_MIN_STORE_KEY "temp_min" // 13 -#define PRESS_MAX_STORE_KEY "press_max" // 14 -#define PRESS_MIN_STORE_KEY "press_min" // 15 -#define HUM_MAX_STORE_KEY "hum_max" // 16 -#define HUM_MIN_STORE_KEY "hum_min" // 17 -#define VOC_MAX_STORE_KEY "voc_max" // 18 -#define CO_MAX_STORE_KEY "co_max" // 19 +#define GAS_MAX_STORE_KEY "bme_max_res" // 1 +#define GAS_MIN_STORE_KEY "bme_min_res" // 2 +#define TEMP_MAX_STORE_KEY "temp_max" // 3 +#define TEMP_MIN_STORE_KEY "temp_min" // 4 +#define PRESS_MAX_STORE_KEY "press_max" // 5 +#define PRESS_MIN_STORE_KEY "press_min" // 6 +#define HUM_MAX_STORE_KEY "hum_max" // 7 +#define HUM_MIN_STORE_KEY "hum_min" // 8 +#define VOC_MAX_STORE_KEY "voc_max" // 9 +#define CO_MAX_STORE_KEY "co_max" // 10 +#define SGP30_ECO_BASE_KEY "eco2_base" // 11 +#define SGP30_VOC_BASE_KEY "tvoc_base" // 12 + const int IaqNonVolatileStore::AVERAGE_BUFFER_SIZE = 10; IaqNonVolatileStore::IaqNonVolatileStore(MicroBit* uBit) { _uBit = uBit; - - for (int i=0 ; i<10 ; ++i){ - const char* key = StringTool::createIndexedKey0_9(i, GAS_MAX_STORE_KEY); - _maxGas[i] = getStored(key, 0); - delete [] key; - } + _maxGas = getStored(GAS_MAX_STORE_KEY, 0); _minGas = getStored(GAS_MIN_STORE_KEY, 0xffffffff); _tempMax = getStored16(TEMP_MAX_STORE_KEY, 0xffff); @@ -54,46 +50,47 @@ } void IaqNonVolatileStore::clear() { - for (int i=0 ; i<10 ; ++i){ - const char* key = StringTool::createIndexedKey0_9(i, GAS_MAX_STORE_KEY); - _uBit->storage.remove(key); - delete [] key; - } - + _uBit->storage.remove(GAS_MAX_STORE_KEY); _uBit->storage.remove(GAS_MIN_STORE_KEY); + _uBit->storage.remove(PRESS_MAX_STORE_KEY); + _uBit->storage.remove(PRESS_MIN_STORE_KEY); + _uBit->storage.remove(VOC_MAX_STORE_KEY); + _uBit->storage.remove(SGP30_ECO_BASE_KEY); + _uBit->storage.remove(SGP30_VOC_BASE_KEY); + resetTmpHumCo2(); +} + +void IaqNonVolatileStore::resetTmpHumCo2() { _uBit->storage.remove(TEMP_MAX_STORE_KEY); _uBit->storage.remove(TEMP_MIN_STORE_KEY); - _uBit->storage.remove(PRESS_MAX_STORE_KEY); - _uBit->storage.remove(PRESS_MIN_STORE_KEY); _uBit->storage.remove(HUM_MAX_STORE_KEY); _uBit->storage.remove(HUM_MIN_STORE_KEY); - _uBit->storage.remove(VOC_MAX_STORE_KEY); _uBit->storage.remove(CO_MAX_STORE_KEY); + _tempMax = 0xffff; + _tempMin = 0x7fff; + _humMax = 0; + _humMin = 0xffffffff; + _coMax =0; } -uint32_t IaqNonVolatileStore::getGasMax(const uint32_t humidity) { - return _maxGas[getIndex(humidity)]; +uint32_t IaqNonVolatileStore::getGasMax() { + return _maxGas; } uint32_t IaqNonVolatileStore::getGasMin(){ return _minGas; } -void IaqNonVolatileStore::updateGas(uint32_t gas, const uint32_t humidity) { - const char index = getIndex(humidity); - const char* key = StringTool::createIndexedKey0_9(index, GAS_MAX_STORE_KEY); - if (!gasWithinReasonableRangeFromAverage(gas) && gas > _maxGas[index]) { - gas = _maxGas[index]; - } - update(gas, &(_maxGas[index]), &_minGas, key, GAS_MIN_STORE_KEY); - delete [] key; +void IaqNonVolatileStore::updateGas(uint32_t gas) { + update(gas, &_maxGas, &_minGas, GAS_MAX_STORE_KEY, GAS_MIN_STORE_KEY); } -char IaqNonVolatileStore::getIndex(const uint32_t humidity) { - char index = humidity/10000; - if (index>9) index = 9; - return index; +void IaqNonVolatileStore::updatePress(const uint32_t pressure) { + update(pressure, &_pressMax, &_pressMin, PRESS_MAX_STORE_KEY, PRESS_MIN_STORE_KEY); +} +void IaqNonVolatileStore::updateHumidity(const uint32_t humidity) { + update(humidity, &_humMax, &_humMin, HUM_MAX_STORE_KEY, HUM_MIN_STORE_KEY); } int16_t IaqNonVolatileStore::getTempMax(){ @@ -104,6 +101,30 @@ return _tempMin; } +void IaqNonVolatileStore::storeIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) { + _uBit->storage.put(SGP30_ECO_BASE_KEY, (uint8_t *)eco2_base, sizeof(int16_t)); + _uBit->storage.put(SGP30_VOC_BASE_KEY, (uint8_t *)tvoc_base, sizeof(int16_t)); +} + +bool IaqNonVolatileStore::getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base) { + bool ok = false; + KeyValuePair* storedEcoBase = _uBit->storage.get(SGP30_ECO_BASE_KEY); + KeyValuePair* storedVocBase = _uBit->storage.get(SGP30_VOC_BASE_KEY); + if (storedEcoBase!=NULL && storedVocBase!=NULL) { + memcpy(&eco2_base, storedEcoBase->value, sizeof(int16_t)); + delete storedEcoBase; + memcpy(&tvoc_base, storedVocBase->value, sizeof(int16_t)); + delete storedVocBase; + ok = true; + } + return ok; +} + +void IaqNonVolatileStore::clearIQQBaseline(){ + _uBit->storage.remove(SGP30_ECO_BASE_KEY); + _uBit->storage.remove(SGP30_VOC_BASE_KEY); +} + void IaqNonVolatileStore::updateTemp(const int16_t temp) { if (_tempMax < temp){ _tempMax = temp; @@ -123,10 +144,6 @@ return _pressMin; } -void IaqNonVolatileStore::updatePress(const uint32_t pressure) { - update(pressure, &_pressMax, &_pressMin, PRESS_MAX_STORE_KEY, PRESS_MIN_STORE_KEY); -} - uint32_t IaqNonVolatileStore::getHumMax(){ return _humMax; } @@ -135,10 +152,6 @@ return _humMin; } -void IaqNonVolatileStore::updateHumidity(const uint32_t humidity) { - update(humidity, &_humMax, &_humMin, HUM_MAX_STORE_KEY, HUM_MIN_STORE_KEY); -} - uint32_t IaqNonVolatileStore::getVocMax(){ return _vocMax; } @@ -166,13 +179,6 @@ } } -bool IaqNonVolatileStore::gasWithinReasonableRangeFromAverage(const uint32_t gas) { - if (gas==0) return false; - _movingAverage->add(gas); - const uint32_t average = _movingAverage->average(); - return (average!=MovingAverage::INVALID) && ((average*12) > gas*10); - } - const uint32_t* IaqNonVolatileStore::debugInfo() { return _movingAverage->debugInfo(); }