2020-01-21 14:56:02 +01:00
|
|
|
// routines for fetching data from the SDS011-sensor
|
|
|
|
|
2020-02-25 22:18:20 +01:00
|
|
|
#if (HAS_SDS011)
|
|
|
|
|
2020-01-21 14:56:02 +01:00
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
2020-02-05 22:13:30 +01:00
|
|
|
#include "sds011read.h"
|
2020-01-21 14:56:02 +01:00
|
|
|
|
2020-02-03 15:26:57 +01:00
|
|
|
#if (HAS_IF482)
|
|
|
|
#error cannot use IF482 together with SDS011 (both use UART#2)
|
|
|
|
#endif
|
2022-02-14 17:32:46 +01:00
|
|
|
|
2022-02-16 00:07:41 +01:00
|
|
|
SdsDustSensor sds(Serial2);
|
2020-01-21 14:56:02 +01:00
|
|
|
|
2022-02-14 17:32:46 +01:00
|
|
|
bool isSDS011Active = false;
|
2022-02-16 00:47:17 +01:00
|
|
|
static float pm10 = 0.0, pm25 = 0.0;
|
2020-01-21 14:56:02 +01:00
|
|
|
|
|
|
|
// init
|
2020-02-05 22:13:30 +01:00
|
|
|
bool sds011_init() {
|
2022-02-16 00:47:17 +01:00
|
|
|
sds.begin(9600, SERIAL_8N1, SDS_RX, SDS_TX);
|
2022-02-16 00:07:41 +01:00
|
|
|
String version = sds.queryFirmwareVersion().toString();
|
2022-02-14 17:32:46 +01:00
|
|
|
ESP_LOGI(TAG, "SDS011 firmware version %s", version);
|
2022-02-16 00:07:41 +01:00
|
|
|
sds.setQueryReportingMode();
|
2020-02-25 22:18:20 +01:00
|
|
|
sds011_sleep(); // we do sleep/wakup by ourselves
|
2022-02-14 17:32:46 +01:00
|
|
|
|
2020-02-05 22:13:30 +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-05 22:13:30 +01:00
|
|
|
void sds011_loop() {
|
|
|
|
if (isSDS011Active) {
|
2022-02-16 00:07:41 +01:00
|
|
|
PmResult pm = sds.queryPm();
|
2022-02-14 17:32:46 +01:00
|
|
|
if (!pm.isOk()) {
|
2022-02-16 00:47:17 +01:00
|
|
|
ESP_LOGE(TAG, "SDS011 query error %s", pm.statusToString());
|
|
|
|
pm10 = pm25 = 0.0;
|
2020-02-05 22:13:30 +01:00
|
|
|
} else {
|
2022-02-16 00:47:17 +01:00
|
|
|
ESP_LOGI(TAG, "SDS011: %s", pm.toString());
|
2022-02-14 17:32:46 +01:00
|
|
|
pm10 = pm.pm10;
|
2022-02-16 00:47:17 +01:00
|
|
|
pm25 = pm.pm25;
|
2020-02-03 15:26:57 +01:00
|
|
|
}
|
2020-02-05 22:13:30 +01:00
|
|
|
sds011_sleep();
|
|
|
|
}
|
2020-01-21 14:56:02 +01:00
|
|
|
}
|
2020-02-03 15:26:57 +01:00
|
|
|
|
2020-02-25 22:18:20 +01:00
|
|
|
// retrieving stored data:
|
|
|
|
void sds011_store(sdsStatus_t *sds_store) {
|
|
|
|
sds_store->pm10 = pm10;
|
|
|
|
sds_store->pm25 = pm25;
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:26:57 +01:00
|
|
|
// putting the SDS-sensor to sleep
|
2020-02-05 22:13:30 +01:00
|
|
|
void sds011_sleep(void) {
|
2022-02-16 00:07:41 +01:00
|
|
|
WorkingStateResult state = sds.sleep();
|
2022-02-14 17:32:46 +01:00
|
|
|
isSDS011Active = state.isWorking();
|
2020-02-03 15:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// start the SDS-sensor
|
|
|
|
// needs 30 seconds for warming up
|
2020-02-05 22:13:30 +01:00
|
|
|
void sds011_wakeup() {
|
2022-02-16 00:07:41 +01:00
|
|
|
WorkingStateResult state = sds.wakeup();
|
2022-02-14 17:32:46 +01:00
|
|
|
isSDS011Active = state.isWorking();
|
2020-02-03 15:26:57 +01:00
|
|
|
}
|
2020-02-25 22:18:20 +01:00
|
|
|
|
|
|
|
#endif // HAS_SDS011
|