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:
Wed Dec 05 09:40:45 2018 +0000
Revision:
12:96eb06e837f4
Parent:
11:6844a5578b4f
Child:
13:996026828be7
independent fibres/loops for bme and agp measuring

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 11:6844a5578b4f 31 #include "nvstore.h"
jsa1969 2:544117df8c65 32
jsa1969 1:f9245fb53737 33 #include "physics.h"
jsa1969 0:cef60cc92da0 34
jsa1969 0:cef60cc92da0 35 MicroBit uBit;
jsa1969 2:544117df8c65 36 Bme680* bme680 = NULL;
jsa1969 2:544117df8c65 37 struct bme680_field_data* bme680Data = NULL;
jsa1969 2:544117df8c65 38 Sgp30* sgp30 = NULL;
jsa1969 12:96eb06e837f4 39 I2cCallbacks* callbacks = NULL;
jsa1969 12:96eb06e837f4 40 NvStore* nvStore = NULL;
jsa1969 12:96eb06e837f4 41
jsa1969 12:96eb06e837f4 42 bool cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 43 int runningLoops = 0;
jsa1969 0:cef60cc92da0 44
jsa1969 12:96eb06e837f4 45 void waitForLooppsToFinish() {
jsa1969 12:96eb06e837f4 46 cancelMeasureLoops=true;
jsa1969 12:96eb06e837f4 47 while (runningLoops>0) {
jsa1969 12:96eb06e837f4 48 uBit.sleep(10);
jsa1969 12:96eb06e837f4 49 }
jsa1969 12:96eb06e837f4 50 cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 51 }
jsa1969 0:cef60cc92da0 52
jsa1969 11:6844a5578b4f 53 void displayPixels(int ystart, int pixels, int maxpixels) {
jsa1969 11:6844a5578b4f 54 int y = ystart;
jsa1969 11:6844a5578b4f 55 for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
jsa1969 11:6844a5578b4f 56 if (i==5) {
jsa1969 11:6844a5578b4f 57 ++y;
jsa1969 11:6844a5578b4f 58 i=0;
jsa1969 11:6844a5578b4f 59 }
jsa1969 11:6844a5578b4f 60 int pixelsused = i+((y-ystart)*5);
jsa1969 11:6844a5578b4f 61 uBit.display.image.setPixelValue(i, y, pixelsused<pixels? 255 : 0);
jsa1969 10:8e6b71a46871 62 }
jsa1969 10:8e6b71a46871 63 }
jsa1969 10:8e6b71a46871 64
jsa1969 1:f9245fb53737 65 int measureBme680(){
jsa1969 1:f9245fb53737 66 if (bme680!=NULL && bme680Data!=NULL) {
jsa1969 1:f9245fb53737 67 return bme680->measure(
jsa1969 1:f9245fb53737 68 bme680Data,
jsa1969 2:544117df8c65 69 bme680Data->temperature==0 ?
jsa1969 2:544117df8c65 70 uBit.thermometer.getTemperature()
jsa1969 2:544117df8c65 71 :bme680Data->temperature,
jsa1969 12:96eb06e837f4 72 2000, 250);
jsa1969 1:f9245fb53737 73 } else {
jsa1969 1:f9245fb53737 74 return BME680_E_DEV_NOT_FOUND;
jsa1969 1:f9245fb53737 75 }
jsa1969 0:cef60cc92da0 76 }
jsa1969 0:cef60cc92da0 77
jsa1969 11:6844a5578b4f 78 void measureAndDisplayBme680() {
jsa1969 11:6844a5578b4f 79 if (bme680!=NULL) {
jsa1969 11:6844a5578b4f 80 uBit.display.image.setPixelValue(4, 4, 255);
jsa1969 11:6844a5578b4f 81 if (measureBme680()==MICROBIT_OK) {
jsa1969 11:6844a5578b4f 82 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 11:6844a5578b4f 83 int bmeY = sgp30!=NULL ? 2 : 0;
jsa1969 11:6844a5578b4f 84 int bmeMaxPixels = sgp30!=NULL ? 5 : 15;
jsa1969 11:6844a5578b4f 85 displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), bmeMaxPixels),
jsa1969 11:6844a5578b4f 86 bmeMaxPixels);
jsa1969 11:6844a5578b4f 87 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 12:96eb06e837f4 88 nvStore->storeGasMax(bme680->gasResistanceMax());
jsa1969 2:544117df8c65 89 }
jsa1969 12:96eb06e837f4 90 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 11:6844a5578b4f 91 }
jsa1969 11:6844a5578b4f 92 }
jsa1969 11:6844a5578b4f 93
jsa1969 11:6844a5578b4f 94 void measureAndDisplaySgp30() {
jsa1969 11:6844a5578b4f 95 if (sgp30!=NULL) {
jsa1969 11:6844a5578b4f 96 int sgpMaxPixels = 10;
jsa1969 11:6844a5578b4f 97 int secondLineStart = 2;
jsa1969 11:6844a5578b4f 98 if (bme680!=NULL) {
jsa1969 11:6844a5578b4f 99 sgpMaxPixels = 5;
jsa1969 11:6844a5578b4f 100 secondLineStart = 1;
jsa1969 11:6844a5578b4f 101 uBit.display.image.setPixelValue(2, 4, 255);
jsa1969 11:6844a5578b4f 102 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 11:6844a5578b4f 103 uBit.display.image.setPixelValue(2, 4, 0);
jsa1969 11:6844a5578b4f 104 }
jsa1969 11:6844a5578b4f 105 uBit.sleep(10);
jsa1969 11:6844a5578b4f 106 }
jsa1969 11:6844a5578b4f 107 uBit.display.image.setPixelValue(3, 4, 255);
jsa1969 11:6844a5578b4f 108 if (sgp30->IAQmeasure()) {
jsa1969 11:6844a5578b4f 109 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 12:96eb06e837f4 110 int co2Dots = min (5, sgp30->eCO2 /1500);
jsa1969 11:6844a5578b4f 111 displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
jsa1969 11:6844a5578b4f 112 displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
jsa1969 11:6844a5578b4f 113 }
jsa1969 2:544117df8c65 114 }
jsa1969 2:544117df8c65 115 }
jsa1969 2:544117df8c65 116
jsa1969 12:96eb06e837f4 117 void bmeMeasureLoop() {
jsa1969 12:96eb06e837f4 118 if (bme680!=NULL) {
jsa1969 12:96eb06e837f4 119 ++runningLoops;
jsa1969 12:96eb06e837f4 120 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 121 measureAndDisplayBme680();
jsa1969 12:96eb06e837f4 122 uBit.sleep(10);
jsa1969 12:96eb06e837f4 123 }
jsa1969 12:96eb06e837f4 124 --runningLoops;
jsa1969 12:96eb06e837f4 125 }
jsa1969 12:96eb06e837f4 126 }
jsa1969 12:96eb06e837f4 127
jsa1969 12:96eb06e837f4 128 void sgpMeasureLoop() {
jsa1969 12:96eb06e837f4 129 if (sgp30!=NULL) {
jsa1969 12:96eb06e837f4 130 ++runningLoops;
jsa1969 12:96eb06e837f4 131 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 132 measureAndDisplaySgp30();
jsa1969 12:96eb06e837f4 133 uBit.sleep(950);
jsa1969 2:544117df8c65 134 }
jsa1969 12:96eb06e837f4 135 --runningLoops;
jsa1969 2:544117df8c65 136 }
jsa1969 12:96eb06e837f4 137 }
jsa1969 12:96eb06e837f4 138
jsa1969 12:96eb06e837f4 139 void startLoops() {
jsa1969 12:96eb06e837f4 140 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 141 uBit.display.scroll("already running");
jsa1969 12:96eb06e837f4 142 return;
jsa1969 12:96eb06e837f4 143 }
jsa1969 12:96eb06e837f4 144 create_fiber(bmeMeasureLoop);
jsa1969 12:96eb06e837f4 145 create_fiber(sgpMeasureLoop);
jsa1969 2:544117df8c65 146 }
jsa1969 2:544117df8c65 147
jsa1969 11:6844a5578b4f 148 void init680(){
jsa1969 9:5150afa50eb6 149 if (bme680!=NULL){
jsa1969 9:5150afa50eb6 150 delete bme680;
jsa1969 9:5150afa50eb6 151 }
jsa1969 10:8e6b71a46871 152
jsa1969 12:96eb06e837f4 153 uint32_t gasMax = nvStore->getStoredGasMax();
jsa1969 11:6844a5578b4f 154 if (gasMax>0) {
jsa1969 11:6844a5578b4f 155 uBit.display.scroll((int)gasMax);
jsa1969 11:6844a5578b4f 156 }
jsa1969 10:8e6b71a46871 157
jsa1969 10:8e6b71a46871 158 bme680 = new Bme680(callbacks, gasMax);
jsa1969 9:5150afa50eb6 159 int code = bme680->init();
jsa1969 9:5150afa50eb6 160 if (code == MICROBIT_OK){
jsa1969 9:5150afa50eb6 161 if (bme680Data==NULL) {
jsa1969 9:5150afa50eb6 162 bme680Data = new struct bme680_field_data;
jsa1969 9:5150afa50eb6 163 }
jsa1969 9:5150afa50eb6 164 code = bme680->measure(
jsa1969 9:5150afa50eb6 165 bme680Data,
jsa1969 9:5150afa50eb6 166 uBit.thermometer.getTemperature(),
jsa1969 9:5150afa50eb6 167 100, 100);
jsa1969 9:5150afa50eb6 168 }
jsa1969 9:5150afa50eb6 169 if (code != MICROBIT_OK){
jsa1969 9:5150afa50eb6 170 delete bme680;
jsa1969 9:5150afa50eb6 171 bme680 = NULL;
jsa1969 9:5150afa50eb6 172 delete bme680Data;
jsa1969 9:5150afa50eb6 173 bme680Data = NULL;
jsa1969 9:5150afa50eb6 174 uBit.display.scroll(code);
jsa1969 9:5150afa50eb6 175 } else {
jsa1969 9:5150afa50eb6 176 uBit.display.image.setPixelValue(0, 4, 255);
jsa1969 9:5150afa50eb6 177 }
jsa1969 11:6844a5578b4f 178 }
jsa1969 9:5150afa50eb6 179
jsa1969 11:6844a5578b4f 180 void initSgp30(){
jsa1969 9:5150afa50eb6 181 if (sgp30!=NULL){
jsa1969 9:5150afa50eb6 182 delete sgp30;
jsa1969 9:5150afa50eb6 183 }
jsa1969 9:5150afa50eb6 184 sgp30 = new Sgp30(callbacks);
jsa1969 9:5150afa50eb6 185 if (sgp30->test() && sgp30->begin()) {
jsa1969 9:5150afa50eb6 186 uBit.display.image.setPixelValue(1, 4, 255);
jsa1969 9:5150afa50eb6 187 } else {
jsa1969 9:5150afa50eb6 188 delete sgp30;
jsa1969 9:5150afa50eb6 189 sgp30 = NULL;
jsa1969 11:6844a5578b4f 190 }}
jsa1969 11:6844a5578b4f 191
jsa1969 11:6844a5578b4f 192 void initSensors() {
jsa1969 12:96eb06e837f4 193 waitForLooppsToFinish();
jsa1969 11:6844a5578b4f 194 init680();
jsa1969 11:6844a5578b4f 195 initSgp30();
jsa1969 9:5150afa50eb6 196 }
jsa1969 9:5150afa50eb6 197
jsa1969 12:96eb06e837f4 198 void displayValuesTxt() {
jsa1969 12:96eb06e837f4 199 waitForLooppsToFinish();
jsa1969 8:1bdb50e03d39 200 if (bme680Data!=NULL) {
jsa1969 8:1bdb50e03d39 201 uBit.display.scroll("h");
jsa1969 8:1bdb50e03d39 202 uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
jsa1969 8:1bdb50e03d39 203 uBit.display.scroll("g");
jsa1969 8:1bdb50e03d39 204 uBit.display.scroll((int)bme680Data->gas_resistance);
jsa1969 8:1bdb50e03d39 205 }
jsa1969 8:1bdb50e03d39 206 if (bme680!=NULL) {
jsa1969 8:1bdb50e03d39 207 uBit.display.scroll("+");
jsa1969 8:1bdb50e03d39 208 uBit.display.scroll((int)bme680->gasResistanceMax());
jsa1969 8:1bdb50e03d39 209 }
jsa1969 8:1bdb50e03d39 210 if (sgp30!=NULL) {
jsa1969 8:1bdb50e03d39 211 uBit.display.scroll("v");
jsa1969 8:1bdb50e03d39 212 uBit.display.scroll((int)sgp30->TVOC);
jsa1969 8:1bdb50e03d39 213 uBit.display.scroll("c");
jsa1969 8:1bdb50e03d39 214 uBit.display.scroll((int)sgp30->eCO2);
jsa1969 8:1bdb50e03d39 215 }
jsa1969 0:cef60cc92da0 216 }
jsa1969 0:cef60cc92da0 217
jsa1969 12:96eb06e837f4 218 void onButtonA(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 219 {
jsa1969 12:96eb06e837f4 220 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 221 displayValuesTxt();
jsa1969 12:96eb06e837f4 222 } else {
jsa1969 12:96eb06e837f4 223 startLoops();
jsa1969 12:96eb06e837f4 224 }
jsa1969 12:96eb06e837f4 225 }
jsa1969 12:96eb06e837f4 226
jsa1969 12:96eb06e837f4 227 void onButtonB(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 228 {
jsa1969 12:96eb06e837f4 229 initSensors();
jsa1969 12:96eb06e837f4 230 }
jsa1969 12:96eb06e837f4 231
jsa1969 9:5150afa50eb6 232 void onButtonAB(MicroBitEvent evt)
jsa1969 9:5150afa50eb6 233 {
jsa1969 12:96eb06e837f4 234 nvStore->resetNvStore();
jsa1969 9:5150afa50eb6 235 }
jsa1969 9:5150afa50eb6 236
jsa1969 0:cef60cc92da0 237 int main()
jsa1969 0:cef60cc92da0 238 {
jsa1969 0:cef60cc92da0 239 uBit.init();
jsa1969 0:cef60cc92da0 240
jsa1969 0:cef60cc92da0 241 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 242 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 9:5150afa50eb6 243 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
jsa1969 2:544117df8c65 244
jsa1969 9:5150afa50eb6 245 callbacks = new I2cCallbacks(&uBit);
jsa1969 12:96eb06e837f4 246 nvStore = new NvStore(&uBit);
jsa1969 9:5150afa50eb6 247
jsa1969 9:5150afa50eb6 248 initSensors();
jsa1969 9:5150afa50eb6 249
jsa1969 12:96eb06e837f4 250 startLoops();
jsa1969 2:544117df8c65 251
jsa1969 0:cef60cc92da0 252 release_fiber();
jsa1969 0:cef60cc92da0 253 }
jsa1969 0:cef60cc92da0 254