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:
12:96eb06e837f4
Parent:
11:6844a5578b4f
Child:
13:996026828be7
diff -r 6844a5578b4f -r 96eb06e837f4 main.cpp
--- a/main.cpp	Tue Dec 04 17:10:00 2018 +0000
+++ b/main.cpp	Wed Dec 05 09:40:45 2018 +0000
@@ -36,10 +36,19 @@
 Bme680* bme680 = NULL;
 struct bme680_field_data* bme680Data = NULL;
 Sgp30* sgp30 = NULL;
-I2cCallbacks* callbacks;
+I2cCallbacks* callbacks = NULL;
+NvStore* nvStore = NULL;
+
+bool cancelMeasureLoops = false;
+int runningLoops = 0;
 
-bool cancelMeasureLoop=false;
-bool loopRunning=false;
+void waitForLooppsToFinish() {
+    cancelMeasureLoops=true;
+    while (runningLoops>0) {
+        uBit.sleep(10);
+    }
+    cancelMeasureLoops = false;
+}
 
 void displayPixels(int ystart, int pixels, int maxpixels) {
     int y = ystart;
@@ -60,7 +69,7 @@
             bme680Data->temperature==0 ?
                 uBit.thermometer.getTemperature()
                 :bme680Data->temperature,
-            150, 250);
+            2000, 250);
     } else {
         return BME680_E_DEV_NOT_FOUND;
     }
@@ -76,8 +85,9 @@
             displayPixels(bmeY, DotMath::pixels(bme680Data->gas_resistance, bme680->gasResistanceMax(), bmeMaxPixels),
                 bmeMaxPixels);
             displayPixels(3, 5*bme680Data->humidity/100000, 5);
-            NvStore::storeGasMax(bme680->gasResistanceMax(), &uBit);
+            nvStore->storeGasMax(bme680->gasResistanceMax());
         }
+        uBit.display.image.setPixelValue(4, 4, 0);
     }
 }
 
@@ -97,29 +107,42 @@
         uBit.display.image.setPixelValue(3, 4, 255);
         if (sgp30->IAQmeasure()) {
             uBit.display.image.setPixelValue(3, 4, 0);
-            int co2Dots = min (5, sgp30->eCO2 /10000);
+            int co2Dots = min (5, sgp30->eCO2 /1500);
             displayPixels(0, 5 - DotMath::pixels(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
             displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
         }
     }
 }
 
-void measureLoop() {
-    if (loopRunning) return;
-    loopRunning = true;
-    
-    if (bme680!=NULL || sgp30!=NULL) {
-        while (!cancelMeasureLoop){
+void bmeMeasureLoop() { 
+    if (bme680!=NULL) {
+        ++runningLoops;
+        while (!cancelMeasureLoops){
             measureAndDisplayBme680();
+            uBit.sleep(10);
+        }
+        --runningLoops;
+    }
+}
+
+void sgpMeasureLoop() {
+    if (sgp30!=NULL) {
+         ++runningLoops;
+        while (!cancelMeasureLoops){
             measureAndDisplaySgp30();
-            uBit.sleep(bme680!=NULL ? 850 : 950);
+            uBit.sleep(950);
         }
-        uBit.display.image.setPixelValue(4, 4, 0);
-    } else {
-        uBit.display.scroll("no dev");
+        --runningLoops;
     }
-    cancelMeasureLoop=false;
-    loopRunning = false;
+}
+
+void startLoops() {
+    if (runningLoops>0) {
+        uBit.display.scroll("already running");
+        return;
+    }
+    create_fiber(bmeMeasureLoop);
+    create_fiber(sgpMeasureLoop);
 }
 
 void init680(){
@@ -127,7 +150,7 @@
         delete bme680;
     }
     
-    uint32_t gasMax = NvStore::getStoredGasMax(&uBit);
+    uint32_t gasMax = nvStore->getStoredGasMax();
     if (gasMax>0) {
         uBit.display.scroll((int)gasMax);
     }
@@ -150,7 +173,6 @@
         bme680Data = NULL;
         uBit.display.scroll(code);
     } else {
-        bme680->resetGasResistenceMax();
         uBit.display.image.setPixelValue(0, 4, 255);
     }
 }
@@ -168,21 +190,13 @@
     }}
 
 void initSensors() {
+    waitForLooppsToFinish();
     init680();
     initSgp30();    
 }
 
-void onButtonA(MicroBitEvent evt)
-{
-    measureLoop();
-}
-
-void onButtonB(MicroBitEvent evt)
-{
-    cancelMeasureLoop=true;
-    while (loopRunning) {
-        uBit.sleep(10);
-    }
+void displayValuesTxt() {
+    waitForLooppsToFinish();
     if (bme680Data!=NULL) {
         uBit.display.scroll("h");
         uBit.display.scroll((int)Physics::absHumidity(bme680Data->humidity, bme680Data->temperature));
@@ -201,9 +215,23 @@
     }
 }
 
+void onButtonA(MicroBitEvent evt)
+{
+    if (runningLoops>0) {
+        displayValuesTxt();
+    } else {
+        startLoops();
+    }
+}
+
+void onButtonB(MicroBitEvent evt)
+{
+    initSensors();
+}
+
 void onButtonAB(MicroBitEvent evt)
 {
-    initSensors();
+    nvStore->resetNvStore();
 }
 
 int main()
@@ -215,12 +243,11 @@
     uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
     
     callbacks = new I2cCallbacks(&uBit);
+    nvStore = new NvStore(&uBit);
     
     initSensors();
     
-    if (bme680!=NULL || sgp30!=NULL) {
-        create_fiber(measureLoop);
-    }
+    startLoops();
     
     release_fiber();
 }