Update sds011read.cpp
This commit is contained in:
parent
f70cfbcc89
commit
8fe69a9130
@ -6,12 +6,16 @@ static const char TAG[] = __FILE__;
|
|||||||
#include <sds011read.h>
|
#include <sds011read.h>
|
||||||
|
|
||||||
// UART(2) is unused in this project
|
// UART(2) is unused in this project
|
||||||
|
#if (HAS_IF482)
|
||||||
|
#error cannot use IF482 together with SDS011 (both use UART#2)
|
||||||
|
#endif
|
||||||
static HardwareSerial sdsSerial(2); // so we use it here
|
static HardwareSerial sdsSerial(2); // so we use it here
|
||||||
static SDS011 sdsSensor; // fine dust sensor
|
static SDS011 sdsSensor; // fine dust sensor
|
||||||
|
|
||||||
// the results of the sensor:
|
// the results of the sensor:
|
||||||
float pm25;
|
float pm25;
|
||||||
float pm10;
|
float pm10;
|
||||||
|
boolean isSDS011Active;
|
||||||
|
|
||||||
// init
|
// init
|
||||||
bool sds011_init()
|
bool sds011_init()
|
||||||
@ -19,18 +23,40 @@ bool sds011_init()
|
|||||||
pm25 = pm10 = 0.0;
|
pm25 = pm10 = 0.0;
|
||||||
sdsSerial.begin(9600, SERIAL_8N1, ESP_PIN_RX, ESP_PIN_TX);
|
sdsSerial.begin(9600, SERIAL_8N1, ESP_PIN_RX, ESP_PIN_TX);
|
||||||
sdsSensor.begin (&sdsSerial);
|
sdsSensor.begin (&sdsSerial);
|
||||||
sdsSensor.contmode(0); // for safety: wakeup/sleep - if we want it we do it by ourselves
|
sdsSensor.contmode(0); // for safety: no wakeup/sleep by the sensor
|
||||||
sdsSensor.wakeup(); // always wake up
|
sds011_sleep(); // we do it by ourselves
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// reading data:
|
// reading data:
|
||||||
void sds011_loop()
|
void sds011_loop()
|
||||||
{
|
{
|
||||||
pm25 = pm10 = 0.0;
|
if ( isSDS011Active ) {
|
||||||
int sdsErrorCode = sdsSensor.read(&pm25, &pm10);
|
int sdsErrorCode = sdsSensor.read(&pm25, &pm10);
|
||||||
if (!sdsErrorCode)
|
if (sdsErrorCode) {
|
||||||
{
|
pm25 = pm10 = 0.0;
|
||||||
ESP_LOGD(TAG, "SDS011 error: %d", sdsErrorCode);
|
ESP_LOGI(TAG, "SDS011 error: %d", sdsErrorCode);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ESP_LOGI(TAG, "fine-dust-values: %5.1f,%4.1f", pm10, pm25);
|
||||||
|
}
|
||||||
|
sds011_sleep();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// putting the SDS-sensor to sleep
|
||||||
|
void sds011_sleep(void)
|
||||||
|
{
|
||||||
|
sdsSensor.sleep();
|
||||||
|
isSDS011Active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the SDS-sensor
|
||||||
|
// needs 30 seconds for warming up
|
||||||
|
void sds011_wakeup()
|
||||||
|
{
|
||||||
|
if ( !isSDS011Active ) {
|
||||||
|
sdsSensor.wakeup();
|
||||||
|
isSDS011Active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user