xbst

Untitled

Feb 8th, 2021 (edited)
2,433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. Required Parts
  2. - ESP8266 V3 Module (https://amzn.to/3jvniw0)(https://s.click.aliexpress.com/e/_Ak3Lem)
  3. - BME680 Module (https://amzn.to/36ShuYg)(https://s.click.aliexpress.com/e/_AEkwT4)
  4. - 0.96" OLED Module (https://amzn.to/36ShuYg)(https://s.click.aliexpress.com/e/_AK5W8m)
  5. - Dupont Wires (Included w/ OLED)(https://s.click.aliexpress.com/e/_AXslsa)
  6.  
  7. Print the case:
  8. https://www.thingiverse.com/thing:4755364
  9.  
  10. 1. Download & install the Arduino IDE
  11. https://www.arduino.cc/en/software
  12.  
  13. 2. Install the ESP8266 Board
  14. 2.1. In your Arduino IDE, go to File> Preferences
  15. 2.2. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the “Additional Boards Manager URLs”. Click OK.
  16. 2.3. Open the Boards Manager. Go to "Tools > Board > Boards Manager…".
  17. 2.4. Search for ESP8266 and press install button for the “ESP8266 by ESP8266 Community“.
  18. 2.5. Select "NodeMCU 1.0 (ESP-12E Module)" under "Tools > Board: "XXXX""
  19. 2.6. Select the correct COM port under "Tools > Port: "XXXX""
  20.  
  21. 3. Install Oled & BME Libraries
  22. 3.1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
  23. 3.2. Search for "SSD1306", and install "Adafruit SSD1306" library.
  24. 3.3. Search for "BSEC", and install "BSEC Software Library" library.
  25.  
  26. 4. Wire the modules. Both modules will connect to the same headers on the ESP board.
  27. SCL (or SCK or C)> D1
  28. SDA (or D) > D2
  29. VCC (or 3V or +) > 3V3
  30. GND (or -) > GND
  31.  
  32. 5. Paste the code, compile & test!
  33.  
  34. My code: (based on Adafruit example (which was based on Bosch example), modified for my OLED module)
  35.  
  36. #include <Wire.h>
  37. #include <Adafruit_SSD1306.h>
  38. #include "bsec.h"
  39.  
  40. #define SEALEVELPRESSURE_HPA (1013.25)
  41.  
  42. Bsec iaqSensor;
  43. String output;
  44. void checkIaqSensorStatus(void);
  45. void errLeds(void);
  46.  
  47. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
  48.  
  49. void setup() {
  50. // put your setup code here, to run once:
  51. Serial.begin(115200);
  52. Wire.begin();
  53.  
  54. Serial.println(F("Starting..."));
  55. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  56. Serial.println(F("SSD1306 allocation failed"));
  57. for(;;);
  58. }
  59.  
  60. Serial.println("OLED begun");
  61.  
  62. display.display();
  63. delay(100);
  64. display.clearDisplay();
  65. display.display();
  66. display.setTextSize(1);
  67. display.setTextColor(SSD1306_WHITE);
  68. display.setRotation(0);
  69.  
  70. iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire);
  71. output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
  72.  
  73. Serial.println(output);
  74. checkIaqSensorStatus();
  75. bsec_virtual_sensor_t sensorList[10] = {
  76. BSEC_OUTPUT_RAW_TEMPERATURE,
  77. BSEC_OUTPUT_RAW_PRESSURE,
  78. BSEC_OUTPUT_RAW_HUMIDITY,
  79. BSEC_OUTPUT_RAW_GAS,
  80. BSEC_OUTPUT_IAQ,
  81. BSEC_OUTPUT_STATIC_IAQ,
  82. BSEC_OUTPUT_CO2_EQUIVALENT,
  83. BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
  84. BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
  85. BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
  86. };
  87.  
  88. iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
  89. checkIaqSensorStatus();
  90. // Print the header
  91. 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";
  92. Serial.println(output);
  93. }
  94.  
  95. void loop() {
  96. // put your main code here, to run repeatedly:
  97. display.setCursor(0,0);
  98. display.clearDisplay();
  99.  
  100. unsigned long time_trigger = millis();
  101. if (iaqSensor.run()) { // If new data is available
  102. output = String(time_trigger);
  103. output += ", " + String(iaqSensor.rawTemperature);
  104. output += ", " + String(iaqSensor.pressure);
  105. output += ", " + String(iaqSensor.rawHumidity);
  106. output += ", " + String(iaqSensor.gasResistance);
  107. output += ", " + String(iaqSensor.iaq);
  108. output += ", " + String(iaqSensor.iaqAccuracy);
  109. output += ", " + String(iaqSensor.temperature);
  110. output += ", " + String(iaqSensor.humidity);
  111. output += ", " + String(iaqSensor.staticIaq);
  112. output += ", " + String(iaqSensor.co2Equivalent);
  113. output += ", " + String(iaqSensor.breathVocEquivalent);
  114. Serial.println(output);
  115. } else {
  116. checkIaqSensorStatus();
  117. }
  118.  
  119. Serial.print("Temperature = "); Serial.print(iaqSensor.temperature); Serial.println(" *C");
  120. display.print("Temperature: "); display.print(iaqSensor.temperature); display.println(" *C");
  121.  
  122. Serial.print("Pressure = "); Serial.print(iaqSensor.pressure / 100.0); Serial.println(" hPa");
  123. display.print("Pressure: "); display.print(iaqSensor.pressure / 100); display.println(" hPa");
  124.  
  125. Serial.print("Humidity = "); Serial.print(iaqSensor.humidity); Serial.println(" %");
  126. display.print("Humidity: "); display.print(iaqSensor.humidity); display.println(" %");
  127.  
  128. Serial.print("IAQ = "); Serial.print(iaqSensor.staticIaq); Serial.println("");
  129. display.print("IAQ: "); display.print(iaqSensor.staticIaq); display.println("");
  130.  
  131. Serial.print("CO2 equiv = "); Serial.print(iaqSensor.co2Equivalent); Serial.println("");
  132. display.print("CO2eq: "); display.print(iaqSensor.co2Equivalent); display.println("");
  133.  
  134. Serial.print("Breath VOC = "); Serial.print(iaqSensor.breathVocEquivalent); Serial.println("");
  135. display.print("VOC: "); display.print(iaqSensor.breathVocEquivalent); display.println("");
  136.  
  137. Serial.println();
  138. display.display();
  139. delay(2000);
  140. }
  141.  
  142. // Helper function definitions
  143. void checkIaqSensorStatus(void)
  144. {
  145. if (iaqSensor.status != BSEC_OK) {
  146. if (iaqSensor.status < BSEC_OK) {
  147. output = "BSEC error code : " + String(iaqSensor.status);
  148. Serial.println(output);
  149. for (;;)
  150. errLeds(); /* Halt in case of failure */
  151. } else {
  152. output = "BSEC warning code : " + String(iaqSensor.status);
  153. Serial.println(output);
  154. }
  155. }
  156.  
  157. if (iaqSensor.bme680Status != BME680_OK) {
  158. if (iaqSensor.bme680Status < BME680_OK) {
  159. output = "BME680 error code : " + String(iaqSensor.bme680Status);
  160. Serial.println(output);
  161. for (;;)
  162. errLeds(); /* Halt in case of failure */
  163. } else {
  164. output = "BME680 warning code : " + String(iaqSensor.bme680Status);
  165. Serial.println(output);
  166. }
  167. }
  168. }
  169.  
  170. void errLeds(void)
  171. {
  172. pinMode(LED_BUILTIN, OUTPUT);
  173. digitalWrite(LED_BUILTIN, HIGH);
  174. delay(100);
  175. digitalWrite(LED_BUILTIN, LOW);
  176. delay(100);
  177. }
  178.  
  179.  
  180. References used:
  181. https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
  182. https://learn.adafruit.com/adafruit-bme680-humidity-temperature-barometic-pressure-voc-gas/bsec-air-quality-library
Advertisement
Add Comment
Please, Sign In to add comment