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:31:50 2018 +0000
Revision:
9:5150afa50eb6
Parent:
8:1bdb50e03d39
Child:
10:8e6b71a46871
re-init sensors on btn AB

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