Add Air Quality sensor readings

This commit is contained in:
Cedric Girard 2019-05-02 16:27:48 +02:00
parent 77cbdf47b9
commit 452db24b08
Signed by: X-dark
GPG key ID: E7D0E125DB9519E4
3 changed files with 73 additions and 2 deletions

3
.gitmodules vendored
View file

@ -4,3 +4,6 @@
[submodule "libraries/BME280"] [submodule "libraries/BME280"]
path = libraries/BME280 path = libraries/BME280
url = https://github.com/finitespace/BME280.git url = https://github.com/finitespace/BME280.git
[submodule "libraries/Adafruit_SGP30"]
path = libraries/Adafruit_SGP30
url = https://github.com/adafruit/Adafruit_SGP30

View file

@ -1,4 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#include <WiFi.h> #include <WiFi.h>
#include "wifi_login.h" #include "wifi_login.h"
@ -10,13 +11,16 @@
#endif #endif
#include <BME280I2C.h> #include <BME280I2C.h>
#include <Wire.h>
#include <Adafruit_SGP30.h>
U8X8_SSD1327_EA_W128128_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); U8X8_SSD1327_EA_W128128_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
BME280I2C bme; // Default : forced mode, standby time = 1000 ms BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
Adafruit_SGP30 sgp;
// setup the terminal (U8X8LOG) and connect to u8g2 for automatic refresh of the display // setup the terminal (U8X8LOG) and connect to u8g2 for automatic refresh of the display
// The size (width * height) depends on the display // The size (width * height) depends on the display
#define U8LOG_WIDTH 16 #define U8LOG_WIDTH 16
@ -24,6 +28,17 @@ BME280I2C bme; // Default : forced mode, standby time = 1000 ms
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT]; uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8X8LOG u8x8log; U8X8LOG u8x8log;
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void setup() { void setup() {
//Init Display //Init Display
@ -75,19 +90,47 @@ void setup() {
u8x8log.print("Found UNKNOWN sensor! Error!\n"); u8x8log.print("Found UNKNOWN sensor! Error!\n");
} }
//Init SGP30 sensor
while(!sgp.begin())
{
u8x8log.print("Could not find SGP30 sensor!\n");
delay(1000);
}
u8x8log.print("Found SGP30 serial #");
u8x8log.print(sgp.serialnumber[0], HEX);
u8x8log.print(sgp.serialnumber[1], HEX);
u8x8log.print(sgp.serialnumber[2], HEX);
u8x8log.print("\n");
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
//sgp.setIAQBaseline(0x8E68, 0x8F41); // Will vary for each sensor!
} }
int counter = 0;
void loop() { void loop() {
//Clear screen //Clear screen
u8x8log.print("\f"); u8x8log.print("\f");
//Read Temperature, humidity and pressure
float temp(NAN), hum(NAN), pres(NAN); float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa); BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit); bme.read(pres, temp, hum, tempUnit, presUnit);
//Read Air Quality
sgp.setHumidity(getAbsoluteHumidity(temp, hum));
if (! sgp.IAQmeasure()) {
u8x8log.print("Air Quality Measurement failed\n");
return;
}
u8x8log.print("Temp: "); u8x8log.print("Temp: ");
u8x8log.print(temp); u8x8log.print(temp);
u8x8log.print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); u8x8log.print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
@ -98,6 +141,30 @@ void loop() {
u8x8log.print(pres); u8x8log.print(pres);
u8x8log.print("Pa\n"); u8x8log.print("Pa\n");
delay(10000); u8x8log.print("TVOC ");
u8x8log.print(sgp.TVOC);
u8x8log.print(" ppb\n");
u8x8log.print("eCO2 ");
u8x8log.print(sgp.eCO2);
u8x8log.print(" ppm\n");
//Get Baseline readings every 30s
counter++;
if (counter == 30) {
counter = 0;
uint16_t TVOC_base, eCO2_base;
if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
u8x8log.print("Failed to get baseline readings\n");
return;
}
u8x8log.print("Baseline values: eCO2: 0x");
u8x8log.print(eCO2_base, HEX);
u8x8log.print(" & TVOC: 0x");
u8x8log.print(TVOC_base, HEX);
u8x8log.print("\n");
}
delay(1000);
} }

@ -0,0 +1 @@
Subproject commit f0f80ad69a62fd6577fb4a9c0cd0b86bccea4279