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:
Thu Jan 20 17:22:19 2022 +0000
Revision:
54:7a73502d5308
Parent:
53:d560c097f1b4
Child:
56:6a7142c84db5
some more tests

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 2:544117df8c65 29 #include "sgp30.h"
jsa1969 46:2fed2865a0f3 30 #include "IaqNonVolatileStore.h"
jsa1969 49:bbb506b58e6e 31 #include "JavaScriptRadio.h"
jsa1969 2:544117df8c65 32
jsa1969 1:f9245fb53737 33 #include "physics.h"
jsa1969 0:cef60cc92da0 34
jsa1969 46:2fed2865a0f3 35 #include "MovingAverage.h"
jsa1969 47:881bfe77a00a 36 #include "RangeTransform.h"
jsa1969 46:2fed2865a0f3 37
jsa1969 33:af2dfab24ca9 38 #include "AppSpecificTestrunner.h"
jsa1969 33:af2dfab24ca9 39
jsa1969 49:bbb506b58e6e 40 #define RADIO_GROUP 14
jsa1969 49:bbb506b58e6e 41 #define RADIO_DELAY 800
jsa1969 49:bbb506b58e6e 42
jsa1969 49:bbb506b58e6e 43 #define CMD_SET_BASELINE "baseline "
jsa1969 49:bbb506b58e6e 44 #define CMD_MEASURE_AND_PUBLISH "IAQ"
jsa1969 49:bbb506b58e6e 45 #define CMD_RESET_TEMP_HUM_CO2 "resetTmpHum"
jsa1969 49:bbb506b58e6e 46 #define CMD_STORE_CURRENT_BASELINE "updateBaseline"
jsa1969 49:bbb506b58e6e 47
jsa1969 0:cef60cc92da0 48 MicroBit uBit;
jsa1969 2:544117df8c65 49 Bme680* bme680 = NULL;
jsa1969 2:544117df8c65 50 struct bme680_field_data* bme680Data = NULL;
jsa1969 2:544117df8c65 51 Sgp30* sgp30 = NULL;
jsa1969 12:96eb06e837f4 52 I2cCallbacks* callbacks = NULL;
jsa1969 46:2fed2865a0f3 53 IaqNonVolatileStore* nvStore = NULL;
jsa1969 49:bbb506b58e6e 54 JavaScriptRadio* radio = NULL;
jsa1969 12:96eb06e837f4 55
jsa1969 12:96eb06e837f4 56 bool cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 57 int runningLoops = 0;
jsa1969 49:bbb506b58e6e 58 int displayBrightness = 255;
jsa1969 0:cef60cc92da0 59
jsa1969 12:96eb06e837f4 60 void waitForLooppsToFinish() {
jsa1969 12:96eb06e837f4 61 cancelMeasureLoops=true;
jsa1969 12:96eb06e837f4 62 while (runningLoops>0) {
jsa1969 12:96eb06e837f4 63 uBit.sleep(10);
jsa1969 12:96eb06e837f4 64 }
jsa1969 12:96eb06e837f4 65 cancelMeasureLoops = false;
jsa1969 12:96eb06e837f4 66 }
jsa1969 0:cef60cc92da0 67
jsa1969 49:bbb506b58e6e 68
jsa1969 11:6844a5578b4f 69 void displayPixels(int ystart, int pixels, int maxpixels) {
jsa1969 11:6844a5578b4f 70 int y = ystart;
jsa1969 11:6844a5578b4f 71 for (int i=0; i+((y-ystart)*5)<maxpixels; ++i) {
jsa1969 11:6844a5578b4f 72 if (i==5) {
jsa1969 11:6844a5578b4f 73 ++y;
jsa1969 11:6844a5578b4f 74 i=0;
jsa1969 11:6844a5578b4f 75 }
jsa1969 11:6844a5578b4f 76 int pixelsused = i+((y-ystart)*5);
jsa1969 49:bbb506b58e6e 77 uBit.display.image.setPixelValue(i, y, pixelsused<pixels? displayBrightness : 0);
jsa1969 10:8e6b71a46871 78 }
jsa1969 10:8e6b71a46871 79 }
jsa1969 48:52cad865a84f 80
jsa1969 1:f9245fb53737 81 int measureBme680(){
jsa1969 38:0f29a0ea64ca 82 int result = BME680_E_DEV_NOT_FOUND;
jsa1969 1:f9245fb53737 83 if (bme680!=NULL && bme680Data!=NULL) {
jsa1969 38:0f29a0ea64ca 84 result = bme680->measure(
jsa1969 1:f9245fb53737 85 bme680Data,
jsa1969 2:544117df8c65 86 bme680Data->temperature==0 ?
jsa1969 2:544117df8c65 87 uBit.thermometer.getTemperature()
jsa1969 45:d6a9fd9c8200 88 : bme680Data->temperature,
jsa1969 38:0f29a0ea64ca 89 2000, 350);
jsa1969 1:f9245fb53737 90 }
jsa1969 38:0f29a0ea64ca 91 return result;
jsa1969 0:cef60cc92da0 92 }
jsa1969 0:cef60cc92da0 93
jsa1969 11:6844a5578b4f 94 void measureAndDisplayBme680() {
jsa1969 11:6844a5578b4f 95 if (bme680!=NULL) {
jsa1969 46:2fed2865a0f3 96 // measuring
jsa1969 49:bbb506b58e6e 97 uBit.display.image.setPixelValue(4, 4, displayBrightness);
jsa1969 11:6844a5578b4f 98 if (measureBme680()==MICROBIT_OK) {
jsa1969 49:bbb506b58e6e 99 nvStore->updateGas(bme680Data->gas_resistance);
jsa1969 17:a2c4dd192146 100 nvStore->updateTemp(bme680Data->temperature);
jsa1969 17:a2c4dd192146 101 nvStore->updatePress(bme680Data->pressure);
jsa1969 17:a2c4dd192146 102 nvStore->updateHumidity(bme680Data->humidity);
jsa1969 17:a2c4dd192146 103
jsa1969 49:bbb506b58e6e 104 const uint32_t gasMax = nvStore->getGasMax();
jsa1969 46:2fed2865a0f3 105
jsa1969 46:2fed2865a0f3 106 // status (last line: 0 sensor found, 2, stray signals, 4, measuring)
jsa1969 49:bbb506b58e6e 107 uBit.display.image.setPixelValue(0, 4, displayBrightness);
jsa1969 49:bbb506b58e6e 108 uBit.display.image.setPixelValue(2, 4, nvStore->strayData() || gasMax < bme680Data->gas_resistance ? displayBrightness : 0);
jsa1969 46:2fed2865a0f3 109 // will be set below uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 44:67a19da5f269 110
jsa1969 46:2fed2865a0f3 111 // if sgp 30 exists, we have less room fpr bme680 resukts
jsa1969 47:881bfe77a00a 112 const int bmeY = sgp30 != NULL ? 2 : 0;
jsa1969 47:881bfe77a00a 113 const int bmeMaxPixels = sgp30 != NULL ? 5 : 15;
jsa1969 46:2fed2865a0f3 114
jsa1969 46:2fed2865a0f3 115 // bme 680 gas state
jsa1969 47:881bfe77a00a 116 const int bmeGasPixels = (gasMax - bme680Data->gas_resistance) * bmeMaxPixels / gasMax;
jsa1969 47:881bfe77a00a 117 displayPixels(bmeY, bmeGasPixels, bmeMaxPixels);
jsa1969 46:2fed2865a0f3 118
jsa1969 46:2fed2865a0f3 119 // humidity index
jsa1969 11:6844a5578b4f 120 displayPixels(3, 5*bme680Data->humidity/100000, 5);
jsa1969 14:71060505061e 121 } else {
jsa1969 46:2fed2865a0f3 122 // indicate sensor not working
jsa1969 14:71060505061e 123 uBit.display.image.setPixelValue(0, 4, 0);
jsa1969 2:544117df8c65 124 }
jsa1969 46:2fed2865a0f3 125 // not measuring
jsa1969 12:96eb06e837f4 126 uBit.display.image.setPixelValue(4, 4, 0);
jsa1969 11:6844a5578b4f 127 }
jsa1969 11:6844a5578b4f 128 }
jsa1969 11:6844a5578b4f 129
jsa1969 49:bbb506b58e6e 130 bool checkAndStoreSgp30Baseline() {
jsa1969 53:d560c097f1b4 131 uint16_t eco2_base = 0;
jsa1969 53:d560c097f1b4 132 uint16_t tvoc_base = 0;
jsa1969 49:bbb506b58e6e 133 bool baseLineOK = sgp30->getIAQBaseline(&eco2_base, &tvoc_base);
jsa1969 49:bbb506b58e6e 134 if (baseLineOK) {
jsa1969 49:bbb506b58e6e 135 nvStore->storeIAQBaseline(eco2_base, tvoc_base);
jsa1969 52:112eaa5282c6 136 radio -> sendToMakeCodeDevices("sgp30 baseln upd");
jsa1969 52:112eaa5282c6 137 radio -> sendToMakeCodeDevices(ManagedString(eco2_base)
jsa1969 52:112eaa5282c6 138 + " / "
jsa1969 52:112eaa5282c6 139 + ManagedString(tvoc_base));
jsa1969 53:d560c097f1b4 140 if (nvStore->getIAQBaseline(&eco2_base, &tvoc_base)) {
jsa1969 53:d560c097f1b4 141 radio -> sendToMakeCodeDevices(ManagedString(eco2_base)
jsa1969 53:d560c097f1b4 142 + " / "
jsa1969 53:d560c097f1b4 143 + ManagedString(tvoc_base));
jsa1969 53:d560c097f1b4 144 }
jsa1969 49:bbb506b58e6e 145 }
jsa1969 52:112eaa5282c6 146
jsa1969 49:bbb506b58e6e 147 return baseLineOK;
jsa1969 49:bbb506b58e6e 148 }
jsa1969 49:bbb506b58e6e 149
jsa1969 11:6844a5578b4f 150 void measureAndDisplaySgp30() {
jsa1969 11:6844a5578b4f 151 if (sgp30!=NULL) {
jsa1969 11:6844a5578b4f 152 int sgpMaxPixels = 10;
jsa1969 11:6844a5578b4f 153 int secondLineStart = 2;
jsa1969 11:6844a5578b4f 154 if (bme680!=NULL) {
jsa1969 11:6844a5578b4f 155 sgpMaxPixels = 5;
jsa1969 11:6844a5578b4f 156 secondLineStart = 1;
jsa1969 49:bbb506b58e6e 157 uBit.display.image.setPixelValue(3, 4, displayBrightness);
jsa1969 11:6844a5578b4f 158 if (sgp30->setHumidity(bme680Data->humidity, bme680Data->temperature)) {
jsa1969 44:67a19da5f269 159 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 11:6844a5578b4f 160 }
jsa1969 11:6844a5578b4f 161 uBit.sleep(10);
jsa1969 11:6844a5578b4f 162 }
jsa1969 49:bbb506b58e6e 163 uBit.display.image.setPixelValue(3, 4, displayBrightness);
jsa1969 38:0f29a0ea64ca 164 bool measureOK = sgp30->IAQmeasure();
jsa1969 38:0f29a0ea64ca 165 if (measureOK) {
jsa1969 52:112eaa5282c6 166 sgp30->IAQmeasureRaw();
jsa1969 49:bbb506b58e6e 167 uBit.display.image.setPixelValue(1, 4, displayBrightness);
jsa1969 11:6844a5578b4f 168 uBit.display.image.setPixelValue(3, 4, 0);
jsa1969 18:c4ac93e01027 169
jsa1969 18:c4ac93e01027 170 nvStore->updateVoc(sgp30->TVOC);
jsa1969 18:c4ac93e01027 171 nvStore->updateCo(sgp30->eCO2);
jsa1969 18:c4ac93e01027 172
jsa1969 12:96eb06e837f4 173 int co2Dots = min (5, sgp30->eCO2 /1500);
jsa1969 47:881bfe77a00a 174 displayPixels(0, 5 - RangeTransform::exponentialTransform(sgp30->TVOC, 20000, sgpMaxPixels), sgpMaxPixels);
jsa1969 11:6844a5578b4f 175 displayPixels(secondLineStart, co2Dots, sgpMaxPixels);
jsa1969 13:996026828be7 176 } else {
jsa1969 13:996026828be7 177 uBit.display.image.setPixelValue(1, 4, 0);
jsa1969 11:6844a5578b4f 178 }
jsa1969 2:544117df8c65 179 }
jsa1969 2:544117df8c65 180 }
jsa1969 2:544117df8c65 181
jsa1969 49:bbb506b58e6e 182
jsa1969 12:96eb06e837f4 183 void bmeMeasureLoop() {
jsa1969 12:96eb06e837f4 184 if (bme680!=NULL) {
jsa1969 12:96eb06e837f4 185 ++runningLoops;
jsa1969 12:96eb06e837f4 186 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 187 measureAndDisplayBme680();
jsa1969 45:d6a9fd9c8200 188 uBit.sleep(500);
jsa1969 12:96eb06e837f4 189 }
jsa1969 12:96eb06e837f4 190 --runningLoops;
jsa1969 12:96eb06e837f4 191 }
jsa1969 24:07a1d2e6914e 192 release_fiber();
jsa1969 12:96eb06e837f4 193 }
jsa1969 12:96eb06e837f4 194
jsa1969 12:96eb06e837f4 195 void sgpMeasureLoop() {
jsa1969 12:96eb06e837f4 196 if (sgp30!=NULL) {
jsa1969 12:96eb06e837f4 197 ++runningLoops;
jsa1969 12:96eb06e837f4 198 while (!cancelMeasureLoops){
jsa1969 11:6844a5578b4f 199 measureAndDisplaySgp30();
jsa1969 12:96eb06e837f4 200 uBit.sleep(950);
jsa1969 2:544117df8c65 201 }
jsa1969 12:96eb06e837f4 202 --runningLoops;
jsa1969 2:544117df8c65 203 }
jsa1969 24:07a1d2e6914e 204 release_fiber();
jsa1969 12:96eb06e837f4 205 }
jsa1969 12:96eb06e837f4 206
jsa1969 34:069c4f6d5b2c 207 void startMeasureLoops() {
jsa1969 12:96eb06e837f4 208 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 209 uBit.display.scroll("already running");
jsa1969 12:96eb06e837f4 210 return;
jsa1969 12:96eb06e837f4 211 }
jsa1969 14:71060505061e 212 create_fiber(sgpMeasureLoop);
jsa1969 12:96eb06e837f4 213 create_fiber(bmeMeasureLoop);
jsa1969 2:544117df8c65 214 }
jsa1969 2:544117df8c65 215
jsa1969 49:bbb506b58e6e 216
jsa1969 11:6844a5578b4f 217 void init680(){
jsa1969 9:5150afa50eb6 218 if (bme680!=NULL){
jsa1969 9:5150afa50eb6 219 delete bme680;
jsa1969 9:5150afa50eb6 220 }
jsa1969 10:8e6b71a46871 221
jsa1969 49:bbb506b58e6e 222 uint32_t gasMax = nvStore->getGasMax();
jsa1969 10:8e6b71a46871 223
jsa1969 14:71060505061e 224 bme680 = new Bme680(callbacks);
jsa1969 9:5150afa50eb6 225 int code = bme680->init();
jsa1969 9:5150afa50eb6 226 if (code == MICROBIT_OK){
jsa1969 9:5150afa50eb6 227 if (bme680Data==NULL) {
jsa1969 9:5150afa50eb6 228 bme680Data = new struct bme680_field_data;
jsa1969 9:5150afa50eb6 229 }
jsa1969 9:5150afa50eb6 230 code = bme680->measure(
jsa1969 9:5150afa50eb6 231 bme680Data,
jsa1969 9:5150afa50eb6 232 uBit.thermometer.getTemperature(),
jsa1969 9:5150afa50eb6 233 100, 100);
jsa1969 9:5150afa50eb6 234 }
jsa1969 9:5150afa50eb6 235 if (code != MICROBIT_OK){
jsa1969 9:5150afa50eb6 236 delete bme680;
jsa1969 9:5150afa50eb6 237 bme680 = NULL;
jsa1969 9:5150afa50eb6 238 delete bme680Data;
jsa1969 9:5150afa50eb6 239 bme680Data = NULL;
jsa1969 9:5150afa50eb6 240 uBit.display.scroll(code);
jsa1969 49:bbb506b58e6e 241 ManagedString send("no BME680 " + code);
jsa1969 49:bbb506b58e6e 242 radio -> sendToMakeCodeDevices(send);
jsa1969 9:5150afa50eb6 243 } else {
jsa1969 49:bbb506b58e6e 244 uBit.display.image.setPixelValue(0, 4, displayBrightness);
jsa1969 49:bbb506b58e6e 245 radio -> sendToMakeCodeDevices("BME680 up");
jsa1969 9:5150afa50eb6 246 }
jsa1969 11:6844a5578b4f 247 }
jsa1969 9:5150afa50eb6 248
jsa1969 11:6844a5578b4f 249 void initSgp30(){
jsa1969 9:5150afa50eb6 250 if (sgp30!=NULL){
jsa1969 9:5150afa50eb6 251 delete sgp30;
jsa1969 9:5150afa50eb6 252 }
jsa1969 49:bbb506b58e6e 253
jsa1969 43:f968ca84d4ed 254 sgp30 = new Sgp30(callbacks);
jsa1969 9:5150afa50eb6 255 if (sgp30->test() && sgp30->begin()) {
jsa1969 49:bbb506b58e6e 256 uBit.display.image.setPixelValue(1, 4, displayBrightness);
jsa1969 49:bbb506b58e6e 257 radio -> sendToMakeCodeDevices("sgp30 up");
jsa1969 49:bbb506b58e6e 258
jsa1969 49:bbb506b58e6e 259 uint16_t eco2_base;
jsa1969 49:bbb506b58e6e 260 uint16_t tvoc_base;
jsa1969 49:bbb506b58e6e 261 if (nvStore->getIAQBaseline(&eco2_base, &tvoc_base)) {
jsa1969 49:bbb506b58e6e 262 sgp30->setIAQBaseline(eco2_base, tvoc_base);
jsa1969 49:bbb506b58e6e 263 uBit.display.image.setPixelValue(2, 4, displayBrightness);
jsa1969 49:bbb506b58e6e 264
jsa1969 49:bbb506b58e6e 265 uBit.sleep(RADIO_DELAY);
jsa1969 51:133f2fb8a7c5 266 radio -> sendToMakeCodeDevices("sgp30 basline");
jsa1969 50:63442fd5e709 267 radio -> sendToMakeCodeDevices("eco:");
jsa1969 49:bbb506b58e6e 268 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 269 radio -> sendToMakeCodeDevices(eco2_base);
jsa1969 49:bbb506b58e6e 270 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 271 radio -> sendToMakeCodeDevices(" co2:");
jsa1969 49:bbb506b58e6e 272 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 273 radio -> sendToMakeCodeDevices(tvoc_base);
jsa1969 49:bbb506b58e6e 274 } else {
jsa1969 49:bbb506b58e6e 275 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 276 radio -> sendToMakeCodeDevices("sgp30 no baseline");
jsa1969 49:bbb506b58e6e 277 }
jsa1969 9:5150afa50eb6 278 } else {
jsa1969 49:bbb506b58e6e 279 radio -> sendToMakeCodeDevices("no sgp30");
jsa1969 9:5150afa50eb6 280 delete sgp30;
jsa1969 9:5150afa50eb6 281 sgp30 = NULL;
jsa1969 40:a67a880cb538 282 }
jsa1969 40:a67a880cb538 283 }
jsa1969 11:6844a5578b4f 284
jsa1969 49:bbb506b58e6e 285 void sgp30SetBaseline(ManagedString message){
jsa1969 49:bbb506b58e6e 286 // sgp30->setIAQBaseline(eco2_base, tvoc_base);
jsa1969 49:bbb506b58e6e 287 if (! (message.substring(0, 9) == CMD_SET_BASELINE)) {
jsa1969 50:63442fd5e709 288 radio -> sendToMakeCodeDevices("sgp30 not setting baseline");
jsa1969 49:bbb506b58e6e 289 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 290 radio -> sendToMakeCodeDevices(message);
jsa1969 49:bbb506b58e6e 291 return;
jsa1969 49:bbb506b58e6e 292 }
jsa1969 49:bbb506b58e6e 293
jsa1969 49:bbb506b58e6e 294 uint16_t eco2_base = 0;
jsa1969 49:bbb506b58e6e 295 uint16_t tvoc_base = 0;
jsa1969 49:bbb506b58e6e 296
jsa1969 49:bbb506b58e6e 297 int messageLength = message.length();
jsa1969 49:bbb506b58e6e 298
jsa1969 49:bbb506b58e6e 299 int i = 13;
jsa1969 49:bbb506b58e6e 300 char c = ' ';
jsa1969 49:bbb506b58e6e 301
jsa1969 49:bbb506b58e6e 302 do{
jsa1969 49:bbb506b58e6e 303 c = message.charAt(i);
jsa1969 49:bbb506b58e6e 304
jsa1969 49:bbb506b58e6e 305 if (c<'0' || c>'9') {
jsa1969 49:bbb506b58e6e 306 eco2_base*=10;
jsa1969 49:bbb506b58e6e 307 eco2_base+= c-'0';
jsa1969 49:bbb506b58e6e 308 }
jsa1969 49:bbb506b58e6e 309 } while (++i < messageLength && c != ' ');
jsa1969 49:bbb506b58e6e 310
jsa1969 49:bbb506b58e6e 311 if (eco2_base!=0 && c==' ') {
jsa1969 49:bbb506b58e6e 312 do{
jsa1969 49:bbb506b58e6e 313 c = message.charAt(i);
jsa1969 49:bbb506b58e6e 314
jsa1969 49:bbb506b58e6e 315 if (c<'0' || c>'9') {
jsa1969 49:bbb506b58e6e 316 tvoc_base*=10;
jsa1969 49:bbb506b58e6e 317 tvoc_base+= c-'0';
jsa1969 49:bbb506b58e6e 318 }
jsa1969 49:bbb506b58e6e 319 } while (++i < messageLength);
jsa1969 49:bbb506b58e6e 320 }
jsa1969 49:bbb506b58e6e 321
jsa1969 49:bbb506b58e6e 322 if (tvoc_base != 0){
jsa1969 50:63442fd5e709 323 sgp30->setIAQBaseline(eco2_base, tvoc_base);
jsa1969 50:63442fd5e709 324 nvStore->storeIAQBaseline(eco2_base, tvoc_base);
jsa1969 49:bbb506b58e6e 325
jsa1969 50:63442fd5e709 326 radio -> sendToMakeCodeDevices("sgp30 baseline set and stored");
jsa1969 49:bbb506b58e6e 327 uBit.sleep(RADIO_DELAY);
jsa1969 50:63442fd5e709 328 radio -> sendToMakeCodeDevices("eco:");
jsa1969 49:bbb506b58e6e 329 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 330 radio -> sendToMakeCodeDevices(eco2_base);
jsa1969 49:bbb506b58e6e 331 uBit.sleep(RADIO_DELAY);
jsa1969 50:63442fd5e709 332 radio -> sendToMakeCodeDevices("voc:");
jsa1969 51:133f2fb8a7c5 333 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 334 radio -> sendToMakeCodeDevices(tvoc_base);
jsa1969 49:bbb506b58e6e 335 } else {
jsa1969 50:63442fd5e709 336 radio -> sendToMakeCodeDevices("sgp30 not setting baseline");
jsa1969 49:bbb506b58e6e 337 uBit.sleep(RADIO_DELAY);
jsa1969 50:63442fd5e709 338 radio -> sendToMakeCodeDevices("eco:");
jsa1969 49:bbb506b58e6e 339 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 340 radio -> sendToMakeCodeDevices(eco2_base);
jsa1969 51:133f2fb8a7c5 341 uBit.sleep(RADIO_DELAY);
jsa1969 50:63442fd5e709 342 radio -> sendToMakeCodeDevices("voc:");
jsa1969 49:bbb506b58e6e 343 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 344 radio -> sendToMakeCodeDevices(tvoc_base);
jsa1969 49:bbb506b58e6e 345 }
jsa1969 49:bbb506b58e6e 346
jsa1969 49:bbb506b58e6e 347 }
jsa1969 49:bbb506b58e6e 348
jsa1969 11:6844a5578b4f 349 void initSensors() {
jsa1969 12:96eb06e837f4 350 waitForLooppsToFinish();
jsa1969 11:6844a5578b4f 351 init680();
jsa1969 11:6844a5578b4f 352 initSgp30();
jsa1969 9:5150afa50eb6 353 }
jsa1969 9:5150afa50eb6 354
jsa1969 49:bbb506b58e6e 355 void publishResults() {
jsa1969 12:96eb06e837f4 356 waitForLooppsToFinish();
jsa1969 49:bbb506b58e6e 357 if (sgp30 != NULL) {
jsa1969 52:112eaa5282c6 358 ManagedString send = ManagedString("Sgp30Tvoc ")
jsa1969 52:112eaa5282c6 359 + ManagedString((int)sgp30->TVOC)
jsa1969 52:112eaa5282c6 360 + " / "
jsa1969 52:112eaa5282c6 361 + ManagedString((int)nvStore->getVocMax());
jsa1969 49:bbb506b58e6e 362 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 363 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 364
jsa1969 52:112eaa5282c6 365 //send = ManagedString("Sgp30MaxVoc ") + ManagedString((int)nvStore->getVocMax());
jsa1969 52:112eaa5282c6 366 //radio -> sendToMakeCodeDevices(send);
jsa1969 52:112eaa5282c6 367 //uBit.sleep(RADIO_DELAY);
jsa1969 52:112eaa5282c6 368
jsa1969 52:112eaa5282c6 369 send = ManagedString("Sgp30o2 ")
jsa1969 52:112eaa5282c6 370 + ManagedString((int)sgp30->eCO2)
jsa1969 52:112eaa5282c6 371 + " / "
jsa1969 52:112eaa5282c6 372 + ManagedString((int)nvStore->getCoMax());
jsa1969 49:bbb506b58e6e 373 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 374 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 375
jsa1969 52:112eaa5282c6 376 //send = ManagedString("Sgp30MaxCo2 ") + ManagedString((int)nvStore->getCoMax());
jsa1969 52:112eaa5282c6 377 //radio -> sendToMakeCodeDevices(send);
jsa1969 52:112eaa5282c6 378 //uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 379
jsa1969 52:112eaa5282c6 380 send = ManagedString("Sgp30Raw ")
jsa1969 52:112eaa5282c6 381 + ManagedString((int)sgp30->rawH2)
jsa1969 52:112eaa5282c6 382 + " / "
jsa1969 52:112eaa5282c6 383 + ManagedString((int)sgp30->rawEthanol);
jsa1969 49:bbb506b58e6e 384 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 385 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 386 }else {
jsa1969 49:bbb506b58e6e 387 radio -> sendToMakeCodeDevices("IAQ no sgp30");
jsa1969 49:bbb506b58e6e 388 uBit.sleep(RADIO_DELAY);
jsa1969 8:1bdb50e03d39 389 }
jsa1969 17:a2c4dd192146 390
jsa1969 49:bbb506b58e6e 391 if (bme680Data!=NULL) {
jsa1969 52:112eaa5282c6 392 ManagedString send = ManagedString("Bme680Gas ") + ManagedString((int)bme680Data->gas_resistance);
jsa1969 49:bbb506b58e6e 393 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 394 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 395
jsa1969 53:d560c097f1b4 396 send = ManagedString("Bme680+Gas ") + ManagedString((int)nvStore->getGasMax());
jsa1969 49:bbb506b58e6e 397 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 398 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 399
jsa1969 53:d560c097f1b4 400 send = ManagedString("Bme680-Gas ") + ManagedString((int)nvStore->getGasMin());
jsa1969 49:bbb506b58e6e 401 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 402 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 403
jsa1969 52:112eaa5282c6 404 send = ManagedString("Bme680T ") + ManagedString((int)bme680Data->temperature);
jsa1969 49:bbb506b58e6e 405 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 406 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 407
jsa1969 53:d560c097f1b4 408 send = ManagedString("Bme680+T ") + ManagedString((int)nvStore->getTempMax());
jsa1969 49:bbb506b58e6e 409 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 410 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 411
jsa1969 53:d560c097f1b4 412 send = ManagedString("Bme680-T ") + ManagedString((int)nvStore->getTempMin());
jsa1969 52:112eaa5282c6 413 radio -> sendToMakeCodeDevices(send);
jsa1969 52:112eaa5282c6 414 uBit.sleep(RADIO_DELAY);
jsa1969 52:112eaa5282c6 415
jsa1969 52:112eaa5282c6 416 send = ManagedString("Bme680Press ") + ManagedString((int)bme680Data->pressure);
jsa1969 49:bbb506b58e6e 417 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 418 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 419
jsa1969 53:d560c097f1b4 420 send = ManagedString("Bme680+P ") + ManagedString((int)nvStore->getPressMax());
jsa1969 49:bbb506b58e6e 421 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 422 uBit.sleep(RADIO_DELAY);
jsa1969 53:d560c097f1b4 423 send = ManagedString("Bme680-P ") + ManagedString((int)nvStore->getPressMin());
jsa1969 49:bbb506b58e6e 424 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 425 uBit.sleep(RADIO_DELAY);
jsa1969 49:bbb506b58e6e 426
jsa1969 52:112eaa5282c6 427 send = ManagedString("Bme680Hum ") + ManagedString((int)bme680Data->humidity);
jsa1969 49:bbb506b58e6e 428 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 429 uBit.sleep(RADIO_DELAY);
jsa1969 52:112eaa5282c6 430
jsa1969 53:d560c097f1b4 431 send = ManagedString("Bme680+H ") + ManagedString((int)nvStore->getHumMax());
jsa1969 52:112eaa5282c6 432 radio -> sendToMakeCodeDevices(send);
jsa1969 52:112eaa5282c6 433 uBit.sleep(RADIO_DELAY);
jsa1969 53:d560c097f1b4 434 send = ManagedString("Bme680-H ") + ManagedString((int)nvStore->getHumMin());
jsa1969 49:bbb506b58e6e 435 radio -> sendToMakeCodeDevices(send);
jsa1969 49:bbb506b58e6e 436 } else {
jsa1969 49:bbb506b58e6e 437 radio -> sendToMakeCodeDevices("IAQ no BME680");
jsa1969 8:1bdb50e03d39 438 }
jsa1969 49:bbb506b58e6e 439 startMeasureLoops();
jsa1969 49:bbb506b58e6e 440 release_fiber();
jsa1969 0:cef60cc92da0 441 }
jsa1969 0:cef60cc92da0 442
jsa1969 49:bbb506b58e6e 443 /*
jsa1969 12:96eb06e837f4 444 void onButtonA(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 445 {
jsa1969 12:96eb06e837f4 446 if (runningLoops>0) {
jsa1969 12:96eb06e837f4 447 displayValuesTxt();
jsa1969 12:96eb06e837f4 448 } else {
jsa1969 34:069c4f6d5b2c 449 startMeasureLoops();
jsa1969 12:96eb06e837f4 450 }
jsa1969 12:96eb06e837f4 451 }
jsa1969 49:bbb506b58e6e 452 */
jsa1969 49:bbb506b58e6e 453 void switchLightLevel(MicroBitEvent evt)
jsa1969 12:96eb06e837f4 454 {
jsa1969 49:bbb506b58e6e 455 switch (displayBrightness) {
jsa1969 49:bbb506b58e6e 456 case 255:
jsa1969 50:63442fd5e709 457 displayBrightness = 1;
jsa1969 20:2f11b93f4cbf 458 break;
jsa1969 50:63442fd5e709 459 case 1:
jsa1969 49:bbb506b58e6e 460 displayBrightness =0;
jsa1969 20:2f11b93f4cbf 461 break;
jsa1969 20:2f11b93f4cbf 462 default:
jsa1969 49:bbb506b58e6e 463 displayBrightness = 255;
jsa1969 20:2f11b93f4cbf 464 }
jsa1969 12:96eb06e837f4 465 }
jsa1969 12:96eb06e837f4 466
jsa1969 49:bbb506b58e6e 467
jsa1969 49:bbb506b58e6e 468 void clearIQQBaseline(MicroBitEvent evt)
jsa1969 9:5150afa50eb6 469 {
jsa1969 49:bbb506b58e6e 470 nvStore->clearIQQBaseline();
jsa1969 49:bbb506b58e6e 471 uBit.display.scroll("base line clear");
jsa1969 49:bbb506b58e6e 472 uBit.reset();
jsa1969 9:5150afa50eb6 473 }
jsa1969 9:5150afa50eb6 474
jsa1969 49:bbb506b58e6e 475
jsa1969 34:069c4f6d5b2c 476 const char* runSofwareModuleTests() {
jsa1969 48:52cad865a84f 477 // heap we've got plenty, stack is rare
jsa1969 54:7a73502d5308 478 AppSpecificTestrunner* runner = new AppSpecificTestrunner(&uBit);
jsa1969 33:af2dfab24ca9 479 const char* result = runner->runAll();
jsa1969 33:af2dfab24ca9 480 delete runner;
jsa1969 33:af2dfab24ca9 481 return result;
jsa1969 33:af2dfab24ca9 482 }
jsa1969 33:af2dfab24ca9 483
jsa1969 49:bbb506b58e6e 484 void onData(MicroBitEvent e) {
jsa1969 49:bbb506b58e6e 485 ManagedString rcv= radio->received();
jsa1969 49:bbb506b58e6e 486 if (rcv == CMD_MEASURE_AND_PUBLISH) {
jsa1969 49:bbb506b58e6e 487 create_fiber(publishResults);
jsa1969 49:bbb506b58e6e 488 } else if (rcv == CMD_RESET_TEMP_HUM_CO2) {
jsa1969 49:bbb506b58e6e 489 nvStore->resetTmpHumCo2();
jsa1969 49:bbb506b58e6e 490 create_fiber(publishResults);
jsa1969 49:bbb506b58e6e 491 } else if (rcv == CMD_STORE_CURRENT_BASELINE) {
jsa1969 52:112eaa5282c6 492 if (!checkAndStoreSgp30Baseline()) {
jsa1969 52:112eaa5282c6 493 radio -> sendToMakeCodeDevices("sgp30 baseln not upd");
jsa1969 49:bbb506b58e6e 494 }
jsa1969 49:bbb506b58e6e 495 } else if (rcv == CMD_SET_BASELINE) {
jsa1969 49:bbb506b58e6e 496 sgp30SetBaseline(rcv);
jsa1969 49:bbb506b58e6e 497 }
jsa1969 49:bbb506b58e6e 498 }
jsa1969 49:bbb506b58e6e 499
jsa1969 0:cef60cc92da0 500 int main()
jsa1969 0:cef60cc92da0 501 {
jsa1969 0:cef60cc92da0 502 uBit.init();
jsa1969 49:bbb506b58e6e 503 uBit.display.scroll("i");
jsa1969 0:cef60cc92da0 504
jsa1969 49:bbb506b58e6e 505 //uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 49:bbb506b58e6e 506 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, switchLightLevel);
jsa1969 49:bbb506b58e6e 507 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, clearIQQBaseline);
jsa1969 49:bbb506b58e6e 508 uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);
jsa1969 2:544117df8c65 509
jsa1969 46:2fed2865a0f3 510 callbacks = new I2cCallbacks(&uBit);
jsa1969 46:2fed2865a0f3 511 nvStore = new IaqNonVolatileStore(&uBit);
jsa1969 49:bbb506b58e6e 512
jsa1969 49:bbb506b58e6e 513 radio = new JavaScriptRadio(&uBit, RADIO_GROUP);
jsa1969 33:af2dfab24ca9 514
jsa1969 46:2fed2865a0f3 515 uBit.display.scroll("t");
jsa1969 34:069c4f6d5b2c 516 const char* testResults = runSofwareModuleTests();
jsa1969 33:af2dfab24ca9 517 if (! Testrunner::messageOK(testResults)) {
jsa1969 54:7a73502d5308 518 uBit.display.scroll(testResults);
jsa1969 54:7a73502d5308 519 //radio -> sendToMakeCodeDevices("IAQ tests failed " +ManagedString(testResults));
jsa1969 33:af2dfab24ca9 520 return -1;
jsa1969 54:7a73502d5308 521 }
jsa1969 46:2fed2865a0f3 522
jsa1969 54:7a73502d5308 523 uBit.display.scroll("s");
jsa1969 34:069c4f6d5b2c 524 // includes hardware tests
jsa1969 9:5150afa50eb6 525 initSensors();
jsa1969 9:5150afa50eb6 526
jsa1969 34:069c4f6d5b2c 527 startMeasureLoops();
jsa1969 2:544117df8c65 528
jsa1969 0:cef60cc92da0 529 release_fiber();
jsa1969 0:cef60cc92da0 530 }
jsa1969 0:cef60cc92da0 531