SGP30: Corrected measure interval, added separate Reporting Interval, Corrected TVOC device class (#1203)
SGP30 Differentiate in sensor measuring interval and reporting interval; Corrected TVOC device class.
This commit is contained in:
parent
d888c45806
commit
a3d80f3a1d
|
@ -14,8 +14,12 @@ SGP30* sgp;
|
|||
long SGP30_status;
|
||||
String SGP30_I2c;
|
||||
int SGP30_I2c_Bus;
|
||||
unsigned long SGP30PreviousMillis = 0;
|
||||
int sensorInterval = 60000;
|
||||
|
||||
unsigned long SGP30PreviousSensorMillis = 0;
|
||||
unsigned long SGP30PreviousReportMillis = 0;
|
||||
|
||||
int sensorInterval = 1000; // SGP30/40/41 are designed to operate at 1Hz: so pull every second
|
||||
int reportInterval = 60000; // Report every minute to MQTT (to avoid flooding)
|
||||
bool initialized = false;
|
||||
|
||||
void Setup() {
|
||||
|
@ -52,25 +56,30 @@ void Loop() {
|
|||
if (!I2C_Bus_1_Started && !I2C_Bus_2_Started) return;
|
||||
if (!initialized) return;
|
||||
|
||||
if (SGP30PreviousMillis == 0 || millis() - SGP30PreviousMillis >= sensorInterval) {
|
||||
SGP30PreviousMillis = millis();
|
||||
if (SGP30PreviousSensorMillis == 0 || millis() - SGP30PreviousSensorMillis >= sensorInterval) {
|
||||
SGP30PreviousSensorMillis = millis();
|
||||
|
||||
sgp->measureAirQuality();
|
||||
float co2 = sgp->CO2;
|
||||
float tvoc = sgp->TVOC;
|
||||
|
||||
if (SGP30PreviousMillis > 30000) { // First 30 seconds after boot, don't report
|
||||
pub((roomsTopic + "/co2").c_str(), 0, 1, String(co2).c_str());
|
||||
pub((roomsTopic + "/tvoc").c_str(), 0, 1, String(tvoc).c_str());
|
||||
|
||||
if (SGP30PreviousSensorMillis > 30000) { // First 30 seconds after boot, don't report
|
||||
if (SGP30PreviousReportMillis == 0 || millis() - SGP30PreviousReportMillis >= reportInterval) {
|
||||
SGP30PreviousReportMillis = millis();
|
||||
|
||||
pub((roomsTopic + "/co2").c_str(), 0, 1, String(co2).c_str());
|
||||
pub((roomsTopic + "/tvoc").c_str(), 0, 1, String(tvoc).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SendDiscovery() {
|
||||
if (SGP30_I2c.isEmpty()) return true;
|
||||
|
||||
return sendSensorDiscovery("Co2", EC_NONE, "carbon_dioxide", "ppm") && sendSensorDiscovery("TVOC", EC_NONE, "volatile_organic_compounds", "ppb");
|
||||
return sendSensorDiscovery("Co2", EC_NONE, "carbon_dioxide", "ppm") && sendSensorDiscovery("TVOC", EC_NONE, "volatile_organic_compounds_parts", "ppb");
|
||||
}
|
||||
|
||||
} // namespace SensirionSGP30
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue