uses BBC micro:bit to measure and display indoor air quality using Bosch BME680 and/or Sensirion SGP30

Dependencies:   microbit

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

Committer:
jsa1969
Date:
Tue Dec 04 16:53:34 2018 +0000
Revision:
10:8e6b71a46871
Parent:
9:5150afa50eb6
Child:
11:6844a5578b4f
store bme680 max gas resistence

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsa1969 0:cef60cc92da0 1 /*
jsa1969 0:cef60cc92da0 2 The MIT License (MIT)
jsa1969 0:cef60cc92da0 3
jsa1969 0:cef60cc92da0 4 Copyright (c) 2016 British Broadcasting Corporation.
jsa1969 0:cef60cc92da0 5 This software is provided by Lancaster University by arrangement with the BBC.
jsa1969 0:cef60cc92da0 6
jsa1969 0:cef60cc92da0 7 Permission is hereby granted, free of charge, to any person obtaining a
jsa1969 0:cef60cc92da0 8 copy of this software and associated documentation files (the "Software"),
jsa1969 0:cef60cc92da0 9 to deal in the Software without restriction, including without limitation
jsa1969 0:cef60cc92da0 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
jsa1969 0:cef60cc92da0 11 and/or sell copies of the Software, and to permit persons to whom the
jsa1969 0:cef60cc92da0 12 Software is furnished to do so, subject to the following conditions:
jsa1969 0:cef60cc92da0 13
jsa1969 0:cef60cc92da0 14 The above copyright notice and this permission notice shall be included in
jsa1969 0:cef60cc92da0 15 all copies or substantial portions of the Software.
jsa1969 0:cef60cc92da0 16
jsa1969 0:cef60cc92da0 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jsa1969 0:cef60cc92da0 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jsa1969 0:cef60cc92da0 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
jsa1969 0:cef60cc92da0 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jsa1969 0:cef60cc92da0 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jsa1969 0:cef60cc92da0 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
jsa1969 0:cef60cc92da0 23 DEALINGS IN THE SOFTWARE.
jsa1969 0:cef60cc92da0 24 */
jsa1969 0:cef60cc92da0 25
jsa1969 0:cef60cc92da0 26 #include "MicroBit.h"
jsa1969 0:cef60cc92da0 27
jsa1969 0:cef60cc92da0 28 #include "bme680.h"
jsa1969 2:544117df8c65 29 #include "sgp30.h"
jsa1969 2:544117df8c65 30 #include "dotmath.h"
jsa1969 2:544117df8c65 31
jsa1969 1:f9245fb53737 32 #include "physics.h"
jsa1969 0:cef60cc92da0 33
jsa1969 0:cef60cc92da0 34 MicroBit uBit;
jsa1969 2:544117df8c65 35 Bme680* bme680 = NULL;
jsa1969 2:544117df8c65 36 struct bme680_field_data* bme680Data = NULL;
jsa1969 2:544117df8c65 37 Sgp30* sgp30 = NULL;
jsa1969 9:5150afa50eb6 38 I2cCallbacks* callbacks;
jsa1969 0:cef60cc92da0 39
jsa1969 0:cef60cc92da0 40 bool cancelMeasureLoop=false;
jsa1969 2:544117df8c65 41 bool loopRunning=false;
jsa1969 0:cef60cc92da0 42
jsa1969 10:8e6b71a46871 43 void storeGasMax(uint32_t gasMax) {
jsa1969 10:8e6b71a46871 44 uBit.storage.put("bme_max_res", (uint8_t *)&gasMax, sizeof(uint32_t));
jsa1969 10:8e6b71a46871 45 }
jsa1969 10:8e6b71a46871 46
jsa1969 10:8e6b71a46871 47 uint32_t getStoredGasMax() {
jsa1969 10:8e6b71a46871 48 uint32_t gasMax = 0;
jsa1969 10:8e6b71a46871 49 KeyValuePair* storedGasMax = uBit.storage.get("bme_max_res");
jsa1969 10:8e6b71a46871 50 if (storedGasMax!=NULL) {
jsa1969 10:8e6b71a46871 51 memcpy(&gasMax, storedGasMax->value, sizeof(uint32_t));
jsa1969 10:8e6b71a46871 52 delete storedGasMax;
jsa1969 10:8e6b71a46871 53 uBit.display.scroll((int)gasMax);
jsa1969 10:8e6b71a46871 54 }
jsa1969 10:8e6b71a46871 55 return gasMax;
jsa1969 10:8e6b71a46871 56 }
jsa1969 10:8e6b71a46871 57
jsa1969 1:f9245fb53737 58 int measureBme680(){
jsa1969 1:f9245fb53737 59 if (bme680!=NULL && bme680Data!=NULL) {
jsa1969 1:f9245fb53737 60 return bme680->measure(
jsa1969 1:f9245fb53737 61 bme680Data,
jsa1969 2:544117df8c65 62 bme680Data->temperature==0 ?
jsa1969 2:544117df8c65 63 uBit.thermometer.getTemperature()
jsa1969 2:544117df8c65 64 :bme680Data->temperature,
jsa1969 3:6084ab9ff0c9 65 150, 250);
jsa1969 1:f9245fb53737 66 } else {
jsa1969 1:f9245fb53737 67 return BME680_E_DEV_NOT_FOUND;
jsa1969 1:f9245fb53737 68 }
jsa1969 0:cef60cc92da0 69 }
jsa1969 0:cef60cc92da0 70
jsa1969 2:544117df8c65 71 void displayPixels(int ystart, int pixels, int maxpixels) {
jsa1969 2:544117df8c65 72 int y = ystart;
jsa1969 2:544117df8c65 73 for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
jsa1969 2:544117df8c65 74 if (i==5) {
jsa1969 2:544117df8c65 75 ++y;
jsa1969 2:544117df8c65 76 i=0;
jsa1969 2:544117df8c65 77 }
jsa1969 2:544117df8c65 78 int pixelsused = i+((y-ystart)*5);
jsa1969 2:544117df8c65 79 uBit.display.image.setPixelValue(i, y, pixelsused<pixels? 255 : 0);
jsa1969 2:544117df8c65 80 }
jsa1969 2:544117df8c65 81 }
jsa1969 2:544117df8c65 82
jsa1969 7:80429cbd095f 83 void measureLoop() {
jsa1969 2:544117df8c65 84 if (loopRunning) return;
jsa1969 2:544117df8c65 85 loopRunning = true;
jsa1969 2:544117df8c65 86
jsa1969 2:544117df8c65 87 if (bme680!=NULL || sgp30!=NULL) {
jsa1969 2:544117df8c65 88 while (!cancelMeasureLoop){
jsa1969 9:5150afa50eb6 89 if (bme680!=NULL) {
jsa1969 9:5150afa50eb6 90 uBit.display.image.setPixelValue(4, 4, 255);
jsa1969 9:5150afa50eb6 91 if (measureBme680()==MICROBIT_OK) {
jsa1969 2:544117df8c65 92 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 3:6084ab9ff0c9 93 int bmeY = sgp30!=NULL ? 2 : 0;
jsa1969 3:6084ab9ff0c9 94 int bmeMaxPixels = sgp30!=NULL ? 5 : 15;
jsa1969 9:5150afa50eb6 95 displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), bmeMaxPixels),
jsa1969 9:5150afa50eb6 96 bmeMaxPixels);
jsa1969 2:544117df8c65 97 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 10:8e6b71a46871 98 storeGasMax(bme680->gasResistanceMax());
jsa1969 2:544117df8c65 99 }
jsa1969 2:544117df8c65 100 }
jsa1969 2:544117df8c65 101
jsa1969 2:544117df8c65 102 if (sgp30!=NULL) {
jsa1969 3:6084ab9ff0c9 103 int sgpMaxPixels = 10;
jsa1969 3:6084ab9ff0c9 104 int secondLineStart = 2;
jsa1969 2:544117df8c65 105 if (bme680!=NULL) {
jsa1969 3:6084ab9ff0c9 106 sgpMaxPixels = 5;
jsa1969 3:6084ab9ff0c9 107 secondLineStart = 1;
jsa1969 2:544117df8c65 108 uBit.display.image.setPixelValue(2, 4, 255);
jsa1969 2:544117df8c65 109 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 2:544117df8c65 110 uBit.display.image.setPixelValue(2, 4, 0);
jsa1969 2:544117df8c65 111 }
jsa1969 2:544117df8c65 112 uBit.sleep(10);
jsa1969 2:544117df8c65 113 }
jsa1969 2:544117df8c65 114 uBit.display.image.setPixelValue(3, 4, 255);
jsa1969 2:544117df8c65 115 if (sgp30->IAQmeasure()) {
jsa1969 2:544117df8c65 116 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 2:544117df8c65 117 int co2Dots = min (5, sgp30->eCO2 /10000);
jsa1969 5:9a04d6d0c2ff 118 displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
jsa1969 3:6084ab9ff0c9 119 displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
jsa1969 2:544117df8c65 120 }
jsa1969 2:544117df8c65 121 }
jsa1969 3:6084ab9ff0c9 122 uBit.sleep(bme680!=NULL ? 850 : 950);
jsa1969 2:544117df8c65 123 }
jsa1969 2:544117df8c65 124 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 2:544117df8c65 125 } else {
jsa1969 2:544117df8c65 126 uBit.display.scroll("no dev");
jsa1969 2:544117df8c65 127 }
jsa1969 2:544117df8c65 128 cancelMeasureLoop=false;
jsa1969 2:544117df8c65 129 loopRunning = false;
jsa1969 2:544117df8c65 130 }
jsa1969 2:544117df8c65 131
jsa1969 9:5150afa50eb6 132 void initSensors() {
jsa1969 9:5150afa50eb6 133 if (bme680!=NULL){
jsa1969 9:5150afa50eb6 134 delete bme680;
jsa1969 9:5150afa50eb6 135 }
jsa1969 10:8e6b71a46871 136
jsa1969 10:8e6b71a46871 137 uint32_t gasMax = getStoredGasMax();
jsa1969 10:8e6b71a46871 138
jsa1969 10:8e6b71a46871 139 bme680 = new Bme680(callbacks, gasMax);
jsa1969 9:5150afa50eb6 140 int code = bme680->init();
jsa1969 9:5150afa50eb6 141 if (code == MICROBIT_OK){
jsa1969 9:5150afa50eb6 142 if (bme680Data==NULL) {
jsa1969 9:5150afa50eb6 143 bme680Data = new struct bme680_field_data;
jsa1969 9:5150afa50eb6 144 }
jsa1969 9:5150afa50eb6 145 code = bme680->measure(
jsa1969 9:5150afa50eb6 146 bme680Data,
jsa1969 9:5150afa50eb6 147 uBit.thermometer.getTemperature(),
jsa1969 9:5150afa50eb6 148 100, 100);
jsa1969 9:5150afa50eb6 149 }
jsa1969 9:5150afa50eb6 150 if (code != MICROBIT_OK){
jsa1969 9:5150afa50eb6 151 delete bme680;
jsa1969 9:5150afa50eb6 152 bme680 = NULL;
jsa1969 9:5150afa50eb6 153 delete bme680Data;
jsa1969 9:5150afa50eb6 154 bme680Data = NULL;
jsa1969 9:5150afa50eb6 155 uBit.display.scroll(code);
jsa1969 9:5150afa50eb6 156 } else {
jsa1969 9:5150afa50eb6 157 bme680->resetGasResistenceMax();
jsa1969 9:5150afa50eb6 158 uBit.display.image.setPixelValue(0, 4, 255);
jsa1969 9:5150afa50eb6 159 }
jsa1969 9:5150afa50eb6 160
jsa1969 9:5150afa50eb6 161 if (sgp30!=NULL){
jsa1969 9:5150afa50eb6 162 delete sgp30;
jsa1969 9:5150afa50eb6 163 }
jsa1969 9:5150afa50eb6 164 sgp30 = new Sgp30(callbacks);
jsa1969 9:5150afa50eb6 165 if (sgp30->test() && sgp30->begin()) {
jsa1969 9:5150afa50eb6 166 uBit.display.image.setPixelValue(1, 4, 255);
jsa1969 9:5150afa50eb6 167 } else {
jsa1969 9:5150afa50eb6 168 delete sgp30;
jsa1969 9:5150afa50eb6 169 sgp30 = NULL;
jsa1969 9:5150afa50eb6 170 }
jsa1969 9:5150afa50eb6 171 }
jsa1969 9:5150afa50eb6 172
jsa1969 0:cef60cc92da0 173 void onButtonA(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 174 {
jsa1969 7:80429cbd095f 175 measureLoop();
jsa1969 0:cef60cc92da0 176 }
jsa1969 0:cef60cc92da0 177
jsa1969 0:cef60cc92da0 178 void onButtonB(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 179 {
jsa1969 2:544117df8c65 180 cancelMeasureLoop=true;
jsa1969 2:544117df8c65 181 while (loopRunning) {
jsa1969 2:544117df8c65 182 uBit.sleep(10);
jsa1969 2:544117df8c65 183 }
jsa1969 8:1bdb50e03d39 184 if (bme680Data!=NULL) {
jsa1969 8:1bdb50e03d39 185 uBit.display.scroll("h");
jsa1969 8:1bdb50e03d39 186 uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
jsa1969 8:1bdb50e03d39 187 uBit.display.scroll("g");
jsa1969 8:1bdb50e03d39 188 uBit.display.scroll((int)bme680Data->gas_resistance);
jsa1969 8:1bdb50e03d39 189 }
jsa1969 8:1bdb50e03d39 190 if (bme680!=NULL) {
jsa1969 8:1bdb50e03d39 191 uBit.display.scroll("+");
jsa1969 8:1bdb50e03d39 192 uBit.display.scroll((int)bme680->gasResistanceMax());
jsa1969 8:1bdb50e03d39 193 }
jsa1969 8:1bdb50e03d39 194 if (sgp30!=NULL) {
jsa1969 8:1bdb50e03d39 195 uBit.display.scroll("v");
jsa1969 8:1bdb50e03d39 196 uBit.display.scroll((int)sgp30->TVOC);
jsa1969 8:1bdb50e03d39 197 uBit.display.scroll("c");
jsa1969 8:1bdb50e03d39 198 uBit.display.scroll((int)sgp30->eCO2);
jsa1969 8:1bdb50e03d39 199 }
jsa1969 0:cef60cc92da0 200 }
jsa1969 0:cef60cc92da0 201
jsa1969 9:5150afa50eb6 202 void onButtonAB(MicroBitEvent evt)
jsa1969 9:5150afa50eb6 203 {
jsa1969 9:5150afa50eb6 204 initSensors();
jsa1969 9:5150afa50eb6 205 }
jsa1969 9:5150afa50eb6 206
jsa1969 0:cef60cc92da0 207 int main()
jsa1969 0:cef60cc92da0 208 {
jsa1969 0:cef60cc92da0 209 // Initialise the micro:bit runtime.
jsa1969 0:cef60cc92da0 210 uBit.init();
jsa1969 0:cef60cc92da0 211
jsa1969 0:cef60cc92da0 212 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 213 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 9:5150afa50eb6 214 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
jsa1969 2:544117df8c65 215
jsa1969 9:5150afa50eb6 216 callbacks = new I2cCallbacks(&uBit);
jsa1969 9:5150afa50eb6 217
jsa1969 9:5150afa50eb6 218 initSensors();
jsa1969 9:5150afa50eb6 219
jsa1969 2:544117df8c65 220 if (bme680!=NULL || sgp30!=NULL) {
jsa1969 7:80429cbd095f 221 create_fiber(measureLoop);
jsa1969 2:544117df8c65 222 }
jsa1969 2:544117df8c65 223
jsa1969 0:cef60cc92da0 224 release_fiber();
jsa1969 0:cef60cc92da0 225 }
jsa1969 0:cef60cc92da0 226