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 06:50:48 2018 +0000
Revision:
2:544117df8c65
Parent:
1:f9245fb53737
Child:
3:6084ab9ff0c9
sgp30

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 1:f9245fb53737 49 750, 350);
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 2:544117df8c65 67 void measureLopp() {
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 2:544117df8c65 77 displayPixels(2, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), 5), 5);
jsa1969 2:544117df8c65 78 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 2:544117df8c65 79 uBit.sleep(100);
jsa1969 2:544117df8c65 80 }
jsa1969 2:544117df8c65 81 }
jsa1969 2:544117df8c65 82
jsa1969 2:544117df8c65 83 if (sgp30!=NULL) {
jsa1969 2:544117df8c65 84 if (bme680!=NULL) {
jsa1969 2:544117df8c65 85 uBit.display.image.setPixelValue(2, 4, 255);
jsa1969 2:544117df8c65 86 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 2:544117df8c65 87 uBit.display.image.setPixelValue(2, 4, 0);
jsa1969 2:544117df8c65 88 }
jsa1969 2:544117df8c65 89 uBit.sleep(10);
jsa1969 2:544117df8c65 90 }
jsa1969 2:544117df8c65 91 uBit.display.image.setPixelValue(3, 4, 255);
jsa1969 2:544117df8c65 92 if (sgp30->IAQmeasure()) {
jsa1969 2:544117df8c65 93 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 2:544117df8c65 94 int co2Dots = min (5, sgp30->eCO2 /10000);
jsa1969 2:544117df8c65 95 displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 10000, 5), 5);
jsa1969 2:544117df8c65 96 displayPixels(1, co2Dots, 5);
jsa1969 2:544117df8c65 97 }
jsa1969 2:544117df8c65 98 }
jsa1969 2:544117df8c65 99 uBit.sleep(bme680!=NULL ? 250 : 900);
jsa1969 2:544117df8c65 100 }
jsa1969 2:544117df8c65 101 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 2:544117df8c65 102 } else {
jsa1969 2:544117df8c65 103 uBit.display.scroll("no dev");
jsa1969 2:544117df8c65 104 }
jsa1969 2:544117df8c65 105 cancelMeasureLoop=false;
jsa1969 2:544117df8c65 106 loopRunning = false;
jsa1969 2:544117df8c65 107 }
jsa1969 2:544117df8c65 108
jsa1969 0:cef60cc92da0 109 void onButtonA(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 110 {
jsa1969 2:544117df8c65 111 measureLopp();
jsa1969 0:cef60cc92da0 112 }
jsa1969 0:cef60cc92da0 113
jsa1969 0:cef60cc92da0 114 void onButtonB(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 115 {
jsa1969 2:544117df8c65 116 cancelMeasureLoop=true;
jsa1969 2:544117df8c65 117 while (loopRunning) {
jsa1969 2:544117df8c65 118 uBit.sleep(10);
jsa1969 2:544117df8c65 119 }
jsa1969 2:544117df8c65 120 uBit.display.scroll("h");
jsa1969 2:544117df8c65 121 uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
jsa1969 2:544117df8c65 122 uBit.display.scroll("v");
jsa1969 2:544117df8c65 123 uBit.display.scroll((int)sgp30->TVOC);
jsa1969 2:544117df8c65 124 uBit.display.scroll("c");
jsa1969 2:544117df8c65 125 uBit.display.scroll((int)sgp30->eCO2);
jsa1969 0:cef60cc92da0 126 }
jsa1969 0:cef60cc92da0 127
jsa1969 0:cef60cc92da0 128 int main()
jsa1969 0:cef60cc92da0 129 {
jsa1969 0:cef60cc92da0 130 // Initialise the micro:bit runtime.
jsa1969 0:cef60cc92da0 131 uBit.init();
jsa1969 0:cef60cc92da0 132
jsa1969 0:cef60cc92da0 133 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 134 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 0:cef60cc92da0 135
jsa1969 2:544117df8c65 136 I2cCallbacks* callbacks = new I2cCallbacks(&uBit);
jsa1969 2:544117df8c65 137
jsa1969 2:544117df8c65 138 //*
jsa1969 2:544117df8c65 139 bme680 = new Bme680(callbacks);
jsa1969 0:cef60cc92da0 140 int code = bme680->init();
jsa1969 0:cef60cc92da0 141 if (code != MICROBIT_OK){
jsa1969 2:544117df8c65 142 delete bme680;
jsa1969 0:cef60cc92da0 143 bme680 = NULL;
jsa1969 0:cef60cc92da0 144 uBit.display.scroll("no bme");
jsa1969 0:cef60cc92da0 145 uBit.display.scroll(code);
jsa1969 0:cef60cc92da0 146 } else {
jsa1969 0:cef60cc92da0 147 uBit.display.scroll("bme:");
jsa1969 1:f9245fb53737 148 bme680Data = new struct bme680_field_data;
jsa1969 1:f9245fb53737 149 uBit.display.scroll(bme680->measure(
jsa1969 1:f9245fb53737 150 bme680Data,
jsa1969 1:f9245fb53737 151 uBit.thermometer.getTemperature(),
jsa1969 1:f9245fb53737 152 100, 100));
jsa1969 0:cef60cc92da0 153 }
jsa1969 2:544117df8c65 154 //*/
jsa1969 2:544117df8c65 155 //*
jsa1969 2:544117df8c65 156 sgp30 = new Sgp30(callbacks);
jsa1969 2:544117df8c65 157 sgp30->serialnumber[0]=sgp30->serialnumber[1]=sgp30->serialnumber[2]=0;
jsa1969 2:544117df8c65 158 if (sgp30->begin()) {
jsa1969 2:544117df8c65 159 uBit.display.scroll("sgp");
jsa1969 2:544117df8c65 160 } else {
jsa1969 2:544117df8c65 161 uBit.display.scroll("no sgp");
jsa1969 2:544117df8c65 162 if (sgp30->serialnumber[0]!=0 || sgp30->serialnumber[1]!=0 || sgp30->serialnumber[2]!=0) {
jsa1969 2:544117df8c65 163 uBit.display.scroll("sn ok");
jsa1969 2:544117df8c65 164 }
jsa1969 2:544117df8c65 165 delete sgp30;
jsa1969 2:544117df8c65 166 sgp30 = NULL;
jsa1969 2:544117df8c65 167 }
jsa1969 2:544117df8c65 168 //*/
jsa1969 2:544117df8c65 169 if (bme680!=NULL || sgp30!=NULL) {
jsa1969 2:544117df8c65 170 MicroBitEvent evt(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK);
jsa1969 2:544117df8c65 171 uBit.messageBus.send(evt);
jsa1969 2:544117df8c65 172 }
jsa1969 2:544117df8c65 173
jsa1969 0:cef60cc92da0 174 release_fiber();
jsa1969 0:cef60cc92da0 175 }
jsa1969 0:cef60cc92da0 176