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 15:07:25 2018 +0000
Revision:
1:f9245fb53737
Parent:
0:cef60cc92da0
Child:
2:544117df8c65
add humidity physics

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