-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPM2_5.ino
127 lines (90 loc) · 2.81 KB
/
PM2_5.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// based on https://github.com/Hypfer/esp8266-vindriktning-particle-sensor
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#define PIN_UART_RX 4 //D2 on Wemos D1 Mini
#define PIN_UART_TX 13 // UNUSED
struct particleSensorState_t {
unsigned long avgPM25;
unsigned long measurements[5] = {0, 0, 0, 0, 0};
int measurementIdx = 0;
};
particleSensorState_t state;
byte serialRxBuf[255];
SoftwareSerial particleSensorSerial(PIN_UART_RX, PIN_UART_TX);
unsigned long statusPublishPreviousMillis = millis();
const long statusPublishInterval = 5000; //5sec
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
particleSensorSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
handleUart();
if (statusPublishInterval <= (millis() - statusPublishPreviousMillis)) {
statusPublishPreviousMillis = millis();
if (state.avgPM25 > 0) {
//publishState();
}
}
}
boolean parseState() {
unsigned long pm25 = serialRxBuf[3] * 256 ^ 3 + serialRxBuf[4] * 256 ^ 2 + serialRxBuf[5] * 256 ^ 1 + serialRxBuf[6];
Serial.print("Received PM 2.5 reading: ");
Serial.println(pm25);
if (pm25 > 0) {
state.measurements[state.measurementIdx] = pm25;
state.measurementIdx = (state.measurementIdx + 1) % 5;
if (state.measurementIdx == 0) {
unsigned long avgPM25 = 0;
boolean invalid = false;
for (int i = 0; i < 5; i++) {
if (state.measurements[i] == 0) {
invalid = true;
} else {
avgPM25 += state.measurements[i];
}
}
if (invalid == false) {
state.avgPM25 = avgPM25 / 5;
Serial.print("New Avg PM25: "); Serial.println(state.avgPM25);
}
}
}
clearRxBuf();
}
int rxBufIdx = 0;
void clearRxBuf() {
//Clear everything for the next message
memset(serialRxBuf, 0, sizeof(serialRxBuf));
rxBufIdx = 0;
}
void handleUart() {
if (particleSensorSerial.available()) {
Serial.print("Receiving:");
}
int prevIdx = rxBufIdx;
while (particleSensorSerial.available()) {
serialRxBuf[rxBufIdx++] = particleSensorSerial.read();
Serial.print(".");
//Without this delay, receiving data breaks for reasons that are beyond me
delay(15);
if (rxBufIdx >= 64) {
clearRxBuf();
}
}
if (prevIdx != rxBufIdx) {
Serial.println("Done.");
}
if (serialRxBuf[0] == 0x16 && serialRxBuf[1] == 0x11 && serialRxBuf[2] == 0x0b) {
parseState();
Serial.print("Current measurements: ");
Serial.print(state.measurements[0]); Serial.print(", ");
Serial.print(state.measurements[1]); Serial.print(", ");
Serial.print(state.measurements[2]); Serial.print(", ");
Serial.print(state.measurements[3]); Serial.print(", ");
Serial.print(state.measurements[4]); Serial.print("\n");
} else {
clearRxBuf();
}
}