Created
April 11, 2017 22:56
-
-
Save piotrkpaul/f85efa61968cd5115dddb78bc4be15fc to your computer and use it in GitHub Desktop.
Lightweight data collector for SDS011 particulate matter sensor on NodeMCU board (ESP8266)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "Sds011.h" | |
#define SDS_RX D1 | |
#define SDS_TX D2 | |
#define SAMPLES 5 | |
SoftwareSerial serialSDS(SDS_RX, SDS_TX); | |
sds011::Sds011 sds(serialSDS); | |
struct { | |
bool valid; | |
float pm10; | |
float pm25; | |
} sds_data = {}; | |
void initSDS(){ | |
sds.set_sleep(false); | |
sds.set_mode(sds011::QUERY); | |
sds.set_sleep(true); | |
} | |
void readSDS() { | |
int pm25, pm10; | |
sds.set_sleep(false); | |
sds_data.valid = sds.query_data_auto(&pm25, &pm10, SAMPLES); | |
sds_data.pm10 = float(pm10)/10; | |
sds_data.pm25 = float(pm25)/10; | |
sds.set_sleep(true); | |
} | |
void readAndPrint(){ | |
readSDS(); | |
if (sds_data.valid) { | |
String result = "PM2.5: " + String(sds_data.pm25) + " | PM10: " + String(sds_data.pm10) + "\n"; | |
Serial.print(result); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
serialSDS.begin(9600); | |
} | |
void loop() { | |
readAndPrint(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment