ESP32-PaxCounter/src/sds011read.cpp

60 lines
1.3 KiB
C++
Raw Normal View History

2020-01-21 14:56:02 +01:00
// routines for fetching data from the SDS011-sensor
// Local logging tag
static const char TAG[] = __FILE__;
2020-02-25 15:41:12 +01:00
#include "sds011read.h"
2020-01-21 14:56:02 +01:00
// UART(2) is unused in this project
2020-02-03 15:26:57 +01:00
#if (HAS_IF482)
#error cannot use IF482 together with SDS011 (both use UART#2)
#endif
2020-02-25 15:41:12 +01:00
static HardwareSerial sdsSerial(2); // so we use it here
static SDS011 sdsSensor; // fine dust sensor
2020-01-21 14:56:02 +01:00
// the results of the sensor:
2020-02-25 15:41:12 +01:00
float pm25;
2020-01-21 14:56:02 +01:00
float pm10;
2020-02-03 15:26:57 +01:00
boolean isSDS011Active;
2020-01-21 14:56:02 +01:00
// init
2020-02-25 15:41:12 +01:00
bool sds011_init() {
pm25 = pm10 = 0.0;
2020-02-25 15:43:54 +01:00
sdsSensor.begin (&sdsSerial, ESP_PIN_RX, ESP_PIN_TX);
delay(100);
// sdsSerial.begin(SDS011_SERIAL);
2020-02-25 15:41:12 +01:00
//sdsSensor.contmode(0); // for safety: no wakeup/sleep by the sensor
2020-02-25 15:43:54 +01:00
sds011_sleep(); // we do sleep/wakup by ourselves
2020-02-25 15:41:12 +01:00
return true;
2020-01-21 14:56:02 +01:00
}
2020-02-25 15:43:54 +01:00
2020-01-21 14:56:02 +01:00
// reading data:
2020-02-25 15:41:12 +01:00
void sds011_loop() {
if (isSDS011Active) {
int sdsErrorCode = sdsSensor.read(&pm25, &pm10);
if (sdsErrorCode) {
pm25 = pm10 = 0.0;
ESP_LOGI(TAG, "SDS011 error: %d", sdsErrorCode);
} else {
ESP_LOGI(TAG, "fine-dust-values: %5.1f,%4.1f", pm10, pm25);
2020-02-03 15:26:57 +01:00
}
2020-02-25 15:41:12 +01:00
sds011_sleep();
}
return;
2020-01-21 14:56:02 +01:00
}
2020-02-03 15:26:57 +01:00
// putting the SDS-sensor to sleep
2020-02-25 15:41:12 +01:00
void sds011_sleep(void) {
sdsSensor.sleep();
isSDS011Active = false;
2020-02-03 15:26:57 +01:00
}
// start the SDS-sensor
// needs 30 seconds for warming up
2020-02-25 15:41:12 +01:00
void sds011_wakeup() {
if (!isSDS011Active) {
sdsSensor.wakeup();
isSDS011Active = true;
}
2020-02-03 15:26:57 +01:00
}