-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code: Photon PMS1003 Dust Sensor
101 lines (81 loc) · 2.47 KB
/
Code: Photon PMS1003 Dust Sensor
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
// This #include statement was automatically added by the Particle IDE.
#include <ParticleSoftSerial.h>
#include <Arduino.h>
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
//unsigned char myChar = 255;
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
ParticleSoftSerial PMSerial(4, 5); // RX, TX
void setup()
{
PMSerial.begin(9600);
PMSerial.setTimeout(1500);
Serial.begin(9600);
}
void loop()
{
if(PMSerial.find("B")){
PMSerial.readBytes((char*)buf,LENG);
if(buf[0] == 0x4d){
//if(buf[0] == "A"){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf);//count PM10 value of the air detector module
Particle.publish("dust", String(PM2_5Value) + " ug/m3 ");
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}