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:
Mon Dec 03 13:19:07 2018 +0000
Revision:
0:cef60cc92da0
Child:
1:f9245fb53737
initial bme680

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 0:cef60cc92da0 29
jsa1969 0:cef60cc92da0 30 MicroBit uBit;
jsa1969 0:cef60cc92da0 31 Bme680* bme680;
jsa1969 0:cef60cc92da0 32
jsa1969 0:cef60cc92da0 33 bool cancelMeasureLoop=false;
jsa1969 0:cef60cc92da0 34
jsa1969 0:cef60cc92da0 35 int measure(){
jsa1969 0:cef60cc92da0 36 struct bme680_field_data* data = new struct bme680_field_data;
jsa1969 0:cef60cc92da0 37 return bme680->measure(
jsa1969 0:cef60cc92da0 38 data,
jsa1969 0:cef60cc92da0 39 uBit.thermometer.getTemperature(),
jsa1969 0:cef60cc92da0 40 750, 150);
jsa1969 0:cef60cc92da0 41 }
jsa1969 0:cef60cc92da0 42
jsa1969 0:cef60cc92da0 43 void onButtonA(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 44 {
jsa1969 0:cef60cc92da0 45 if (bme680!=NULL) {
jsa1969 0:cef60cc92da0 46 while (!cancelMeasureLoop){
jsa1969 0:cef60cc92da0 47 uBit.display.scroll(measure());
jsa1969 0:cef60cc92da0 48 uBit.sleep(500);
jsa1969 0:cef60cc92da0 49 }
jsa1969 0:cef60cc92da0 50 cancelMeasureLoop=false;
jsa1969 0:cef60cc92da0 51 }
jsa1969 0:cef60cc92da0 52 }
jsa1969 0:cef60cc92da0 53
jsa1969 0:cef60cc92da0 54 void onButtonB(MicroBitEvent evt)
jsa1969 0:cef60cc92da0 55 {
jsa1969 0:cef60cc92da0 56 cancelMeasureLoop=true;
jsa1969 0:cef60cc92da0 57 }
jsa1969 0:cef60cc92da0 58
jsa1969 0:cef60cc92da0 59 int main()
jsa1969 0:cef60cc92da0 60 {
jsa1969 0:cef60cc92da0 61 // Initialise the micro:bit runtime.
jsa1969 0:cef60cc92da0 62 uBit.init();
jsa1969 0:cef60cc92da0 63
jsa1969 0:cef60cc92da0 64 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:cef60cc92da0 65 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 0:cef60cc92da0 66
jsa1969 0:cef60cc92da0 67 bme680 = new Bme680(new I2cCallbacks(&uBit));
jsa1969 0:cef60cc92da0 68 int code = bme680->init();
jsa1969 0:cef60cc92da0 69 if (code != MICROBIT_OK){
jsa1969 0:cef60cc92da0 70 bme680 = NULL;
jsa1969 0:cef60cc92da0 71 uBit.display.scroll("no bme");
jsa1969 0:cef60cc92da0 72 uBit.display.scroll(code);
jsa1969 0:cef60cc92da0 73 } else {
jsa1969 0:cef60cc92da0 74 uBit.display.scroll("bme:");
jsa1969 0:cef60cc92da0 75 uBit.display.scroll(measure());
jsa1969 0:cef60cc92da0 76 }
jsa1969 0:cef60cc92da0 77 // If main exits, there may still be other fibers running or registered event handlers etc.
jsa1969 0:cef60cc92da0 78 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
jsa1969 0:cef60cc92da0 79 // sit in the idle task forever, in a power efficient sleep.
jsa1969 0:cef60cc92da0 80 release_fiber();
jsa1969 0:cef60cc92da0 81 }
jsa1969 0:cef60cc92da0 82