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

main.cpp

Committer:
jsa1969
Date:
2019-02-12
Revision:
33:af2dfab24ca9
Parent:
28:56273b8282ca
Child:
34:069c4f6d5b2c

File content as of revision 33:af2dfab24ca9:

/*
The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "MicroBit.h"

#include "bme680.h"
#include "sgp30.h"
#include "dotmath.h"
#include "nvstore.h"

#include "physics.h"

#include "AppSpecificTestrunner.h"

MicroBit uBit;
Bme680* bme680 = NULL;
struct bme680_field_data* bme680Data = NULL;
Sgp30* sgp30 = NULL;
I2cCallbacks* callbacks = NULL;
NvStore* nvStore = NULL;

bool cancelMeasureLoops = false;
int runningLoops = 0;
int displayState = 0;

void waitForLooppsToFinish() {
    cancelMeasureLoops=true;
    while (runningLoops>0) {
        uBit.sleep(10);
    }
    cancelMeasureLoops = false;
}

void displayPixels(int ystart, int pixels, int maxpixels) {
    int y = ystart;
    for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
        if (i==5) {
            ++y;
            i=0;
        }
        int pixelsused = i+((y-ystart)*5);
        uBit.display.image.setPixelValue(i, y, pixelsused<pixels? 255 : 0);
    }
}

int measureBme680(){
    if (bme680!=NULL && bme680Data!=NULL) {
        return bme680->measure(
            bme680Data,
            bme680Data->temperature==0 ?
                uBit.thermometer.getTemperature()
                :bme680Data->temperature,
            2000, 250);
    } else {
        return BME680_E_DEV_NOT_FOUND;
    }
}

void measureAndDisplayBme680() {
    if (bme680!=NULL) {
        uBit.display.image.setPixelValue(4, 4, 255);
        if (measureBme680()==MICROBIT_OK) {
            nvStore->updateGas(bme680Data->gas_resistance, bme680Data->humidity);
            nvStore->updateTemp(bme680Data->temperature);
            nvStore->updatePress(bme680Data->pressure);
            nvStore->updateHumidity(bme680Data->humidity);
            
            uBit.display.image.setPixelValue(0, 4, 255);
            uBit.display.image.setPixelValue(4, 4, 0);
            int bmeY = sgp30!=NULL ? 2 : 0;
            int bmeMaxPixels = sgp30!=NULL ? 5 : 15;
            displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, nvStore->getGasMax(bme680Data->humidity), bmeMaxPixels),
                bmeMaxPixels);
            displayPixels(3, 5*bme680Data->humidity/100000, 5);
        } else {
            uBit.display.image.setPixelValue(0, 4, 0);
        }
        uBit.display.image.setPixelValue(4, 4, 0);
    }
}

void measureAndDisplaySgp30() {
    if (sgp30!=NULL) {
        int sgpMaxPixels = 10;
        int secondLineStart = 2;
        if (bme680!=NULL) {
            sgpMaxPixels = 5;
            secondLineStart = 1;
            uBit.display.image.setPixelValue(2, 4, 255);
            if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
                uBit.display.image.setPixelValue(2, 4, 0);
            }
            uBit.sleep(10);
        }
        uBit.display.image.setPixelValue(3, 4, 255);
        if (sgp30->IAQmeasure()) {
            uBit.display.image.setPixelValue(1, 4, 255);
            uBit.display.image.setPixelValue(3, 4, 0);
            
            nvStore->updateVoc(sgp30->TVOC);
            nvStore->updateCo(sgp30->eCO2);
            
            int co2Dots = min (5, sgp30->eCO2 /1500);
            displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
            displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
        } else {
            uBit.display.image.setPixelValue(1, 4, 0);
        }
    }
}

void bmeMeasureLoop() { 
    if (bme680!=NULL) {
        ++runningLoops;
        while (!cancelMeasureLoops){
            measureAndDisplayBme680();
            uBit.sleep(500);
        }
        --runningLoops;
    }
    release_fiber();
}

void sgpMeasureLoop() {
    if (sgp30!=NULL) {
         ++runningLoops;
        while (!cancelMeasureLoops){
            measureAndDisplaySgp30();
            uBit.sleep(950);
        }
        --runningLoops;
    }
    release_fiber();
}

void startLoops() {
    if (runningLoops>0) {
        uBit.display.scroll("already running");
        return;
    }
    create_fiber(sgpMeasureLoop);
    create_fiber(bmeMeasureLoop);
}

void init680(){
    if (bme680!=NULL){
        delete bme680;
    }
    
    uint32_t gasMax = nvStore->getGasMax(0);
    if (gasMax>0) {
        uBit.display.scroll((int)gasMax);
    }
    
    bme680 = new Bme680(callbacks);
    int code = bme680->init();
    if (code == MICROBIT_OK){
        if (bme680Data==NULL) {
            bme680Data = new struct bme680_field_data;
        }
        code = bme680->measure(
            bme680Data,
            uBit.thermometer.getTemperature(),
            100, 100);
    }
    if (code != MICROBIT_OK){
        delete bme680;
        bme680 = NULL;
        delete bme680Data;
        bme680Data = NULL;
        uBit.display.scroll(code);
    } else {
        uBit.display.image.setPixelValue(0, 4, 255);
    }
}

void initSgp30(){
    if (sgp30!=NULL){
        delete sgp30;
    }
    sgp30 = new Sgp30(callbacks);
    if (sgp30->test() && sgp30->begin()) {
        uBit.display.image.setPixelValue(1, 4, 255);
    } else  {
        delete sgp30;
        sgp30 = NULL;
    }}

void initSensors() {
    waitForLooppsToFinish();
    init680();
    initSgp30();    
}

void displayValuesTxt() {
    waitForLooppsToFinish();
    if (bme680Data!=NULL) {
        uBit.display.scroll("g");
        uBit.display.scroll((int)bme680Data->gas_resistance);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getGasMax(bme680Data->humidity));
        uBit.display.scroll("-");
        uBit.display.scroll((int)nvStore->getGasMin());
        uBit.display.scroll("t");
        uBit.display.scroll((int)bme680Data->temperature);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getTempMax());
        uBit.display.scroll("-");
        uBit.display.scroll((int)nvStore->getTempMin());
        uBit.display.scroll("p");
        uBit.display.scroll((int)bme680Data->pressure);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getPressMax());
        uBit.display.scroll("-");
        uBit.display.scroll((int)nvStore->getPressMin());
        uBit.display.scroll("h");
        uBit.display.scroll((int)bme680Data->humidity);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getHumMax());
        uBit.display.scroll("-");
        uBit.display.scroll((int)nvStore->getHumMin());
    }
    
    if (sgp30!=NULL) {
        uBit.display.scroll("v");
        uBit.display.scroll((int)sgp30->TVOC);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getVocMax());
        uBit.display.scroll("c");
        uBit.display.scroll((int)sgp30->eCO2);
        uBit.display.scroll("+");
        uBit.display.scroll((int)nvStore->getCoMax());
    }
}

void onButtonA(MicroBitEvent evt)
{
    if (runningLoops>0) {
        displayValuesTxt();
    } else {
        startLoops();
    }
}

void onButtonB(MicroBitEvent evt)
{
    //initSensors();
    switch (++displayState) {
        case 1:
            uBit.display.setBrightness(5);
            break;
        case 2:
            uBit.display.disable();
            break;
        default:
            uBit.display.setBrightness(255);
            uBit.display.enable();
            displayState = 0;
    }
}

void onButtonAB(MicroBitEvent evt)
{
    waitForLooppsToFinish();
    nvStore->resetNvStore();
    uBit.display.scroll("clear");
}

const char* runTests() {
    AppSpecificTestrunner* runner = new AppSpecificTestrunner();
    const char* result = runner->runAll();
    delete runner;
    return result;
    }

int main()
{
    uBit.init();
    
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
    
    callbacks = new I2cCallbacks(&uBit);
    nvStore = new NvStore(&uBit);

    const char* testResults = runTests();
    if (! Testrunner::messageOK(testResults)) {
        uBit.display.scroll(testResults);
        return -1;
        }

    initSensors();
    
    startLoops();
    
    release_fiber();
}