Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#define _DEBUG
unsigned int pm1 = 0;
unsigned int pm2_5 = 0;
unsigned int pm10 = 0;
long lastPmReading = 0;
byte buffer[24];
void setup() {
Serial.begin(9600);
Serial.println("Hello!");
Serial1.begin(9600);
}
unsigned int packetIndex = 0;
void offer(byte value) {
buffer[packetIndex] = value;
if (packetIndex == 0) {
if(value == 0x42) {
packetIndex = 1;
}
return;
}
if (packetIndex == 1) {
if (value == 0x4d) {
packetIndex = 2;
return;
} else {
packetIndex = 0;
return;
}
}
if (packetIndex == 23) {
measure();
packetIndex = 0;
} else {
packetIndex ++;
}
}
void measure() {
for (unsigned int i = 0; i < sizeof(buffer); i++) {
Serial.printf("%x ", buffer[i]);
}
Serial.println("");
pm1 = 256 * buffer[4] + buffer[5];
pm2_5 = 256 * buffer[6] + buffer[7];
pm10 = 256 * buffer[8] + buffer[9];
}
void loop() {
if (Serial1.available())
offer(Serial1.read());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.