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

Revision:
2:544117df8c65
Parent:
1:f9245fb53737
Child:
3:6084ab9ff0c9
--- a/main.cpp	Mon Dec 03 15:07:25 2018 +0000
+++ b/main.cpp	Tue Dec 04 06:50:48 2018 +0000
@@ -26,49 +26,103 @@
 #include "MicroBit.h"
 
 #include "bme680.h"
+#include "sgp30.h"
+#include "dotmath.h"
+
 #include "physics.h"
 
 MicroBit uBit;
-Bme680* bme680;
-struct bme680_field_data* bme680Data;
+Bme680* bme680 = NULL;
+struct bme680_field_data* bme680Data = NULL;
+Sgp30* sgp30 = NULL;
 
 bool cancelMeasureLoop=false;
-float tmp=0.0;
+bool loopRunning=false;
 
 int measureBme680(){
     if (bme680!=NULL && bme680Data!=NULL) {
         return bme680->measure(
             bme680Data,
-            uBit.thermometer.getTemperature(),
+            bme680Data->temperature==0 ?
+                uBit.thermometer.getTemperature()
+                :bme680Data->temperature,
             750, 350);
     } else {
         return BME680_E_DEV_NOT_FOUND;
     }
 }
 
+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);
+    }
+}
+
+void measureLopp() {
+    if (loopRunning) return;
+    loopRunning = true;
+    
+    if (bme680!=NULL || sgp30!=NULL) {
+        while (!cancelMeasureLoop){
+                if (bme680!=NULL) {
+                    uBit.display.image.setPixelValue(4, 4, 255);
+                    if (measureBme680()==MICROBIT_OK) {
+                    uBit.display.image.setPixelValue(4, 4, 0);
+                    displayPixels(2, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), 5), 5);
+                    displayPixels(3, 5*bme680Data->humidity/100000, 5);
+                    uBit.sleep(100);
+                }
+            }
+            
+            if (sgp30!=NULL) {
+                if (bme680!=NULL) {
+                    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(3, 4, 0);
+                    int co2Dots = min (5, sgp30->eCO2 /10000);
+                    displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 10000, 5), 5);
+                    displayPixels(1, co2Dots, 5);
+                }
+            }
+            uBit.sleep(bme680!=NULL ? 250 : 900);
+        }
+        uBit.display.image.setPixelValue(4, 4, 0);
+    } else {
+        uBit.display.scroll("no dev");
+    }
+    cancelMeasureLoop=false;
+    loopRunning = false;
+}
+
 void onButtonA(MicroBitEvent evt)
 {
-    /*
-    if (bme680!=NULL) {
-        while (!cancelMeasureLoop){
-            uBit.display.scroll(measureBme680());
-            uBit.sleep(500);
-        }
-        cancelMeasureLoop=false;
-    }
-    */
-    //uBit.display.scroll((int)Physics::saettigungsdampfdruck(0.0));
-    tmp += 1.0;
-    uBit.display.scroll((int)tmp);
-    uBit.display.scroll(Physics::absHumidity(100,tmp));
+    measureLopp();
 }
 
 void onButtonB(MicroBitEvent evt)
 {
-    //cancelMeasureLoop=true;
-    tmp -= 1.0;
-    uBit.display.scroll((int)tmp);
-    uBit.display.scroll((int)Physics::absHumidity(100,tmp));
+    cancelMeasureLoop=true;
+    while (loopRunning) {
+        uBit.sleep(10);
+    }
+    uBit.display.scroll("h");
+    uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
+    uBit.display.scroll("v");
+    uBit.display.scroll((int)sgp30->TVOC);
+    uBit.display.scroll("c");
+    uBit.display.scroll((int)sgp30->eCO2);
 }
 
 int main()
@@ -79,9 +133,13 @@
     uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
     uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
     
-    bme680 = new Bme680(new I2cCallbacks(&uBit));
+    I2cCallbacks* callbacks = new I2cCallbacks(&uBit);
+    
+    //*
+    bme680 = new Bme680(callbacks);
     int code = bme680->init();
     if (code != MICROBIT_OK){
+        delete bme680;
         bme680 = NULL;
         uBit.display.scroll("no bme");
         uBit.display.scroll(code);
@@ -93,9 +151,26 @@
             uBit.thermometer.getTemperature(),
             100, 100));
     }
-    // If main exits, there may still be other fibers running or registered event handlers etc.
-    // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
-    // sit in the idle task forever, in a power efficient sleep.
+    //*/
+    //*
+    sgp30 = new Sgp30(callbacks);
+    sgp30->serialnumber[0]=sgp30->serialnumber[1]=sgp30->serialnumber[2]=0;
+    if (sgp30->begin()) {
+        uBit.display.scroll("sgp");
+    } else  {
+        uBit.display.scroll("no sgp");
+        if (sgp30->serialnumber[0]!=0 || sgp30->serialnumber[1]!=0 || sgp30->serialnumber[2]!=0) {
+            uBit.display.scroll("sn ok");
+        }
+        delete sgp30;
+        sgp30 = NULL;
+    }
+    //*/
+    if (bme680!=NULL || sgp30!=NULL) {
+        MicroBitEvent evt(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK);
+        uBit.messageBus.send(evt);
+    }
+    
     release_fiber();
 }