Created
December 29, 2018 18:49
-
-
Save timcharper/30094a2d9dd5b6cddf6f2c1bd6104a1f 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
#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