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:15:14 2018 +0000
Revision:
7:80429cbd095f
Parent:
6:fc6f2f08724b
Child:
8:1bdb50e03d39
properly schedule measure loop at start

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 0:cef60cc92da0 38
jsa1969 0:cef60cc92da0 39 bool cancelMeasureLoop=false;
jsa1969 2:544117df8c65 40 bool loopRunning=false;
jsa1969 0:cef60cc92da0 41
jsa1969 1:f9245fb53737 42 int measureBme680(){
jsa1969 1:f9245fb53737 43 if (bme680!=NULL && bme680Data!=NULL) {
jsa1969 1:f9245fb53737 44 return bme680->measure(
jsa1969 1:f9245fb53737 45 bme680Data,
jsa1969 2:544117df8c65 46 bme680Data->temperature==0 ?
jsa1969 2:544117df8c65 47 uBit.thermometer.getTemperature()
jsa1969 2:544117df8c65 48 :bme680Data->temperature,
jsa1969 3:6084ab9ff0c9 49 150, 250);
jsa1969 1:f9245fb53737 50 } else {
jsa1969 1:f9245fb53737 51 return BME680_E_DEV_NOT_FOUND;
jsa1969 1:f9245fb53737 52 }
jsa1969 0:cef60cc92da0 53 }
jsa1969 0:cef60cc92da0 54
jsa1969 2:544117df8c65 55 void displayPixels(int ystart, int pixels, int maxpixels) {
jsa1969 2:544117df8c65 56 int y = ystart;
jsa1969 2:544117df8c65 57 for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
jsa1969 2:544117df8c65 58 if (i==5) {
jsa1969 2:544117df8c65 59 ++y;
jsa1969 2:544117df8c65 60 i=0;
jsa1969 2:544117df8c65 61 }
jsa1969 2:544117df8c65 62 int pixelsused = i+((y-ystart)*5);
jsa1969 2:544117df8c65 63 uBit.display.image.setPixelValue(i, y, pixelsused<pixels? 255 : 0);
jsa1969 2:544117df8c65 64 }
jsa1969 2:544117df8c65 65 }
jsa1969 2:544117df8c65 66
jsa1969 7:80429cbd095f 67 void measureLoop() {
jsa1969 2:544117df8c65 68 if (loopRunning) return;
jsa1969 2:544117df8c65 69 loopRunning = true;
jsa1969 2:544117df8c65 70
jsa1969 2:544117df8c65 71 if (bme680!=NULL || sgp30!=NULL) {
jsa1969 2:544117df8c65 72 while (!cancelMeasureLoop){
jsa1969 2:544117df8c65 73 if (bme680!=NULL) {
jsa1969 2:544117df8c65 74 uBit.display.image.setPixelValue(4, 4, 255);
jsa1969 2:544117df8c65 75 if (measureBme680()==MICROBIT_OK) {
jsa1969 2:544117df8c65 76 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 3:6084ab9ff0c9 77 int bmeY = sgp30!=NULL ? 2 : 0;
jsa1969 3:6084ab9ff0c9 78 int bmeMaxPixels = sgp30!=NULL ? 5 : 15;
jsa1969 3:6084ab9ff0c9 79 displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), bmeMaxPixels), bmeMaxPixels);
jsa1969 2:544117df8c65 80 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 2:544117df8c65 81 uBit.sleep(100);
jsa1969 2:544117df8c65 82 }
jsa1969 2:544117df8c65 83 }
jsa1969 2:544117df8c65 84
jsa1969 2:544117df8c65 85 if (sgp30!=NULL) {
jsa1969 3:6084ab9ff0c9 86 int sgpMaxPixels = 10;
jsa1969 3:6084ab9ff0c9 87 int secondLineStart = 2;
jsa1969 2:544117df8c65 88 if (bme680!=NULL) {
jsa1969 3:6084ab9ff0c9 89 sgpMaxPixels = 5;
jsa1969 3:6084ab9ff0c9 90 secondLineStart = 1;
jsa1969 2:544117df8c65 91 uBit.display.image.setPixelValue(2, 4, 255);
jsa1969 2:544117df8c65 92 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 2:544117df8c65 93 uBit.display.image.setPixelValue(2, 4, 0);
jsa1969 2:544117df8c65 94 }
jsa1969 2:544117df8c65 95 uBit.sleep(10);
jsa1969 2:544117df8c65 96 }
jsa1969 2:544117df8c65 97 uBit.display.image.setPixelValue(3, 4, 255);
jsa1969 2:544117df8c65 98 if (sgp30->IAQmeasure()) {
jsa1969 2:544117df8c65 99 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 2:544117df8c65 100 int co2Dots = min (5, sgp30->eCO2 /10000);
jsa1969 5:9a04d6d0c2ff 101 displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
jsa1969 3:6084ab9ff0c9 102 displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
jsa1969 2:544117df8c65 103 }
jsa1969 2:544117df8c65 104 }
jsa1969 3:6084ab9ff0c9 105 uBit.sleep(bme680!=NULL ? 850 : 950);
jsa1969 2:544117df8c65 106 }
jsa1969 2:544117df8c65 107 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 2:544117df8c65 108 } else {
jsa1969 2:544117df8c65 109 uBit.display.scroll("no dev");
jsa1969 2:544117df8c65 110 }
jsa1969 2:544117df8c65 111 cancelMeasureLoop=false;
jsa1969 2:544117df8c65 112 loopRunning = false;
jsa1969 2:544117df8c65 113 }
jsa1969 2:544117df8c65 114
jsa1969 0:cef60cc92da0 115 void onButtonA(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 116 {
jsa1969 7:80429cbd095f 117 measureLoop();
jsa1969 0:cef60cc92da0 118 }
jsa1969 0:cef60cc92da0 119
jsa1969 0:cef60cc92da0 120 void onButtonB(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 121 {
jsa1969 2:544117df8c65 122 cancelMeasureLoop=true;
jsa1969 2:544117df8c65 123 while (loopRunning) {
jsa1969 2:544117df8c65 124 uBit.sleep(10);
jsa1969 2:544117df8c65 125 }
jsa1969 2:544117df8c65 126 uBit.display.scroll("h");
jsa1969 2:544117df8c65 127 uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
jsa1969 2:544117df8c65 128 uBit.display.scroll("v");
jsa1969 2:544117df8c65 129 uBit.display.scroll((int)sgp30->TVOC);
jsa1969 2:544117df8c65 130 uBit.display.scroll("c");
jsa1969 2:544117df8c65 131 uBit.display.scroll((int)sgp30->eCO2);
jsa1969 0:cef60cc92da0 132 }
jsa1969 0:cef60cc92da0 133
jsa1969 0:cef60cc92da0 134 int main()
jsa1969 0:cef60cc92da0 135 {
jsa1969 0:cef60cc92da0 136 // Initialise the micro:bit runtime.
jsa1969 0:cef60cc92da0 137 uBit.init();
jsa1969 0:cef60cc92da0 138
jsa1969 0:cef60cc92da0 139 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 140 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 0:cef60cc92da0 141
jsa1969 2:544117df8c65 142 I2cCallbacks* callbacks = new I2cCallbacks(&uBit);
jsa1969 2:544117df8c65 143
jsa1969 2:544117df8c65 144 bme680 = new Bme680(callbacks);
jsa1969 0:cef60cc92da0 145 int code = bme680->init();
jsa1969 3:6084ab9ff0c9 146 if (code == MICROBIT_OK){
jsa1969 3:6084ab9ff0c9 147 bme680Data = new struct bme680_field_data;
jsa1969 3:6084ab9ff0c9 148 code = bme680->measure(
jsa1969 3:6084ab9ff0c9 149 bme680Data,
jsa1969 3:6084ab9ff0c9 150 uBit.thermometer.getTemperature(),
jsa1969 3:6084ab9ff0c9 151 100, 100);
jsa1969 3:6084ab9ff0c9 152 }
jsa1969 0:cef60cc92da0 153 if (code != MICROBIT_OK){
jsa1969 2:544117df8c65 154 delete bme680;
jsa1969 0:cef60cc92da0 155 bme680 = NULL;
jsa1969 0:cef60cc92da0 156 uBit.display.scroll(code);
jsa1969 0:cef60cc92da0 157 } else {
jsa1969 3:6084ab9ff0c9 158 bme680->resetGasResistenceMax();
jsa1969 4:aef0bddcdf50 159 uBit.display.image.setPixelValue(0, 4, 255);
jsa1969 0:cef60cc92da0 160 }
jsa1969 4:aef0bddcdf50 161
jsa1969 2:544117df8c65 162 sgp30 = new Sgp30(callbacks);
jsa1969 6:fc6f2f08724b 163 if (sgp30->test() && sgp30->begin()) {
jsa1969 4:aef0bddcdf50 164 uBit.display.image.setPixelValue(1, 4, 255);
jsa1969 2:544117df8c65 165 } else {
jsa1969 2:544117df8c65 166 delete sgp30;
jsa1969 2:544117df8c65 167 sgp30 = NULL;
jsa1969 2:544117df8c65 168 }
jsa1969 4:aef0bddcdf50 169
jsa1969 2:544117df8c65 170 if (bme680!=NULL || sgp30!=NULL) {
jsa1969 7:80429cbd095f 171 create_fiber(measureLoop);
jsa1969 2:544117df8c65 172 }
jsa1969 2:544117df8c65 173
jsa1969 0:cef60cc92da0 174 release_fiber();
jsa1969 0:cef60cc92da0 175 }
jsa1969 0:cef60cc92da0 176