Created
January 1, 2018 16:56
-
-
Save Meroje/c4156c418c7af7df011665f66355dad7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// SDS011 dust sensor example | |
// for use with SoftSerial | |
// ----------------------------- | |
#include <SoftwareSerial.h> | |
uint8_t wakequery[] = { | |
0x01 | |
}; | |
uint8_t readquery[] = { | |
0xAA, // head | |
0xB4, // command id | |
0x06, // data byte 1 | |
0x01, // data byte 2 (set mode) | |
0x00, // data byte 3 (query) | |
0x00, // data byte 4 | |
0x00, // data byte 5 | |
0x00, // data byte 6 | |
0x00, // data byte 7 | |
0x00, // data byte 8 | |
0x00, // data byte 9 | |
0x00, // data byte 10 | |
0x00, // data byte 11 | |
0x00, // data byte 12 | |
0x00, // data byte 13 | |
0xFF, // data byte 14 (device id byte 1) | |
0xFF, // data byte 15 (device id byte 2) | |
0x05, // checksum | |
0xAB // tail | |
}; | |
uint8_t setpassivequery[] = { | |
0xAA, // head | |
0xB4, // command id | |
0x02, // data byte 1 | |
0x01, // data byte 2 (set mode) | |
0x01, // data byte 3 (query) | |
0x00, // data byte 4 | |
0x00, // data byte 5 | |
0x00, // data byte 6 | |
0x00, // data byte 7 | |
0x00, // data byte 8 | |
0x00, // data byte 9 | |
0x00, // data byte 10 | |
0x00, // data byte 11 | |
0x00, // data byte 12 | |
0x00, // data byte 13 | |
0xFF, // data byte 14 (device id byte 1) | |
0xFF, // data byte 15 (device id byte 2) | |
0x02, // checksum | |
0xAB // tail | |
}; | |
SoftwareSerial mySerial(10, 11); // RX, TX | |
void setup() { | |
// initialize normal Serial port | |
Serial.begin(9600); | |
// initalize SDS Serial Port | |
mySerial.begin(9600); | |
// Wakeup | |
sendquery(wakequery); | |
delay(100); | |
sendquery(setpassivequery); | |
int data[10]; | |
readresponse(data); | |
Serial.println("Mode: " + String(data[3])); | |
} | |
void loop() { | |
int data[10]; | |
sendquery(wakequery); | |
delay(3000); | |
sendquery(readquery); | |
readresponse(data); | |
Serial.println("P2.5: " + String(((data[3] << 8) + data[2]) / 10.0f)); | |
Serial.println("P10: " + String(((data[5] << 8) + data[4]) / 10.0f)); | |
delay(8000); | |
} | |
void readresponse(int* data) { | |
int value; | |
int len = 0; | |
int receiveSum; | |
while (len < 10) { | |
if (mySerial.available()) { | |
value = int(mySerial.read()); | |
data[len] = value; | |
if (len > 1 && len < 9) { | |
receiveSum += value; | |
} | |
len++; | |
} | |
} | |
while (mySerial.available() > 0) { | |
mySerial.read(); | |
} | |
if (data[0] == 170 && data[1] == 197 && data[9] == 171) { | |
Serial.println("checksum: " + String((receiveSum % 256) == data[8])); | |
} else { | |
Serial.println("Wrong envelope"); | |
} | |
return data; | |
} | |
void sendquery(uint8_t query[]) { | |
while (mySerial.available() > 0) { | |
mySerial.read(); | |
} | |
for (uint8_t i = 0; i < 19; i++) { | |
mySerial.write(query[i]); | |
} | |
mySerial.flush(); | |
while (mySerial.available() > 0) { | |
mySerial.read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment