测量空气TVOC浓度、CO2浓度、空气温度和湿度
利用DHT11数据实时给SGP30传感器温湿度补偿
//ESP8266不定义i2c情况下,D1连接SCL,D2连接SDA
//总挥发性有机物tvoc
//eCO2浓度co2
#include <Wire.h>
#include "Adafruit_SGP30.h"
#include "DHT.h"
#define DHTPIN 2 // 引脚2连接黄色数据线 D4
#define DHTTYPE DHT11 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SGP30 sgp;
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
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() {
Serial.begin(115200);
dht.begin();
Serial.println("SGP30 test");
if (! sgp.begin()) {
Serial.println("Sensor not found :(");
while (1);
}
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float temperature = t; // [°C]
float humidity = h; // [%RH]
sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %");
Serial.print("\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
if (! sgp.IAQmeasure()) {
Serial.println("Measurement failed");
return;
}
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.println(" ppb\t");
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
Serial.println();
delay(1000);
}