|
| 1 | +/*************************************************************************** |
| 2 | + This is a library for the BME680 gas, humidity, temperature & pressure sensor |
| 3 | +
|
| 4 | + Designed specifically to work with the Adafruit BME680 Breakout |
| 5 | + ----> http://www.adafruit.com/products/3660 |
| 6 | +
|
| 7 | + These sensors use I2C or SPI to communicate, 2 or 4 pins are required |
| 8 | + to interface. |
| 9 | +
|
| 10 | + Adafruit invests time and resources providing this open source code, |
| 11 | + please support Adafruit and open-source hardware by purchasing products |
| 12 | + from Adafruit! |
| 13 | +
|
| 14 | + Written by Limor Fried & Kevin Townsend for Adafruit Industries. |
| 15 | + BSD license, all text above must be included in any redistribution |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include <Adafruit_SSD1306.h> |
| 19 | +#include "bsec.h" |
| 20 | + |
| 21 | +Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire); |
| 22 | + |
| 23 | +Bsec iaqSensor; |
| 24 | +String output; |
| 25 | + |
| 26 | + |
| 27 | +void setup() { |
| 28 | + Serial.begin(9600); |
| 29 | + //while (!Serial); |
| 30 | + |
| 31 | + Serial.println(F("BME680 test")); |
| 32 | + |
| 33 | + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally |
| 34 | + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64 |
| 35 | + Serial.println(F("SSD1306 allocation failed")); |
| 36 | + for(;;); // Don't proceed, loop forever |
| 37 | + } |
| 38 | + Serial.println("OLED begun"); |
| 39 | + |
| 40 | + display.display(); |
| 41 | + delay(100); |
| 42 | + display.clearDisplay(); |
| 43 | + display.display(); |
| 44 | + display.setTextSize(1); |
| 45 | + display.setTextColor(SSD1306_WHITE); |
| 46 | + display.setRotation(0); |
| 47 | + |
| 48 | + iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire); |
| 49 | + output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix); |
| 50 | + Serial.println(output); |
| 51 | + checkIaqSensorStatus(); |
| 52 | + bsec_virtual_sensor_t sensorList[10] = { |
| 53 | + BSEC_OUTPUT_RAW_TEMPERATURE, |
| 54 | + BSEC_OUTPUT_RAW_PRESSURE, |
| 55 | + BSEC_OUTPUT_RAW_HUMIDITY, |
| 56 | + BSEC_OUTPUT_RAW_GAS, |
| 57 | + BSEC_OUTPUT_IAQ, |
| 58 | + BSEC_OUTPUT_STATIC_IAQ, |
| 59 | + BSEC_OUTPUT_CO2_EQUIVALENT, |
| 60 | + BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, |
| 61 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, |
| 62 | + BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, |
| 63 | + }; |
| 64 | + |
| 65 | + iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP); |
| 66 | + checkIaqSensorStatus(); |
| 67 | + // Print the header |
| 68 | + output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%], Static IAQ, CO2 equivalent, breath VOC equivalent"; |
| 69 | + Serial.println(output); |
| 70 | +} |
| 71 | + |
| 72 | +void loop() { |
| 73 | + display.setCursor(0,0); |
| 74 | + display.clearDisplay(); |
| 75 | + |
| 76 | + unsigned long time_trigger = millis(); |
| 77 | + if (! iaqSensor.run()) { // If no data is available |
| 78 | + checkIaqSensorStatus(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + output = String(time_trigger); |
| 83 | + output += ", " + String(iaqSensor.rawTemperature); |
| 84 | + output += ", " + String(iaqSensor.pressure); |
| 85 | + output += ", " + String(iaqSensor.rawHumidity); |
| 86 | + output += ", " + String(iaqSensor.gasResistance); |
| 87 | + output += ", " + String(iaqSensor.iaq); |
| 88 | + output += ", " + String(iaqSensor.iaqAccuracy); |
| 89 | + output += ", " + String(iaqSensor.temperature); |
| 90 | + output += ", " + String(iaqSensor.humidity); |
| 91 | + output += ", " + String(iaqSensor.staticIaq); |
| 92 | + output += ", " + String(iaqSensor.co2Equivalent); |
| 93 | + output += ", " + String(iaqSensor.breathVocEquivalent); |
| 94 | + Serial.println(output); |
| 95 | + |
| 96 | + |
| 97 | + Serial.print("Temperature = "); Serial.print(iaqSensor.temperature); Serial.println(" *C"); |
| 98 | + display.print("Temperature: "); display.print(iaqSensor.temperature); display.println(" *C"); |
| 99 | + |
| 100 | + Serial.print("Pressure = "); Serial.print(iaqSensor.pressure / 100.0); Serial.println(" hPa"); |
| 101 | + display.print("Pressure: "); display.print(iaqSensor.pressure / 100); display.println(" hPa"); |
| 102 | + |
| 103 | + Serial.print("Humidity = "); Serial.print(iaqSensor.humidity); Serial.println(" %"); |
| 104 | + display.print("Humidity: "); display.print(iaqSensor.humidity); display.println(" %"); |
| 105 | + |
| 106 | + Serial.print("IAQ = "); Serial.print(iaqSensor.staticIaq); Serial.println(""); |
| 107 | + display.print("IAQ: "); display.print(iaqSensor.staticIaq); display.println(""); |
| 108 | + |
| 109 | + Serial.print("CO2 equiv = "); Serial.print(iaqSensor.co2Equivalent); Serial.println(""); |
| 110 | + display.print("CO2eq: "); display.print(iaqSensor.co2Equivalent); display.println(""); |
| 111 | + |
| 112 | + Serial.print("Breath VOC = "); Serial.print(iaqSensor.breathVocEquivalent); Serial.println(""); |
| 113 | + display.print("VOC: "); display.print(iaqSensor.breathVocEquivalent); display.println(""); |
| 114 | + |
| 115 | + Serial.println(); |
| 116 | + display.display(); |
| 117 | + delay(2000); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +// Helper function definitions |
| 122 | +void checkIaqSensorStatus(void) |
| 123 | +{ |
| 124 | + if (iaqSensor.status != BSEC_OK) { |
| 125 | + if (iaqSensor.status < BSEC_OK) { |
| 126 | + output = "BSEC error code : " + String(iaqSensor.status); |
| 127 | + Serial.println(output); |
| 128 | + display.setCursor(0,0); |
| 129 | + display.println(output); |
| 130 | + display.display(); |
| 131 | + for (;;) delay(10); |
| 132 | + } else { |
| 133 | + output = "BSEC warning code : " + String(iaqSensor.status); |
| 134 | + Serial.println(output); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (iaqSensor.bme680Status != BME680_OK) { |
| 139 | + if (iaqSensor.bme680Status < BME680_OK) { |
| 140 | + output = "BME680 error code : " + String(iaqSensor.bme680Status); |
| 141 | + Serial.println(output); |
| 142 | + display.setCursor(0,0); |
| 143 | + display.println(output); |
| 144 | + display.display(); |
| 145 | + for (;;) delay(10); |
| 146 | + } else { |
| 147 | + output = "BME680 warning code : " + String(iaqSensor.bme680Status); |
| 148 | + Serial.println(output); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments