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 17:10:00 2018 +0000
Revision:
11:6844a5578b4f
Parent:
10:8e6b71a46871
Child:
12:96eb06e837f4
code cleanup / smaller functions

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