GP v30 04: Particle Matter Sensor
This is the basic code to manage and read the particle sensor.

Required Materials

1 x Waspmote 1 x Battery 1 x Gases PRO board v30 1 x Particle matter sensor

Notes

- Remember to connect the battery to Waspmote for proper operation. - The connection of the sensor is described in the Gases PRO v30 technical guide.

Code

1
/*
2
* ------------ [GP_v30_04] - Particle Matter Sensor --------------
3
*
4
* Explanation: This is a complete code to manage and read the particle
5
* sensor.
6
*
7
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
*
23
* Version: 3.1
24
* Design: David Gascón
25
* Implementation: Isabel Burillo
26
*/
27
28
#include <WaspPM.h>
29
#include <WaspFrame.h>
30
31
/*
32
* P&S! Possibilities for this sensor:
33
* - SOCKET_D
34
*/
35
36
char info_string[61];
37
char serial_number[61];
38
int status;
39
int measure;
40
41
void setup()
42
{
43
USB.println(F("Particle Matter Sensor example"));
44
45
// switch on sensor
46
status = PM.ON();
47
48
// check answer
49
if (status == 1)
50
{
51
// get info from sensor
52
status = PM.getInfoString(info_string);
53
54
if (status == 1)
55
{
56
USB.println(F("Information string extracted:"));
57
USB.println(info_string);
58
}
59
else
60
{
61
USB.println(F("Error reading the particle sensor"));
62
}
63
64
// read serial number from sensor. That funtion isn't avalable for OPC-N2 sensor
65
status = PM.readSerialNumber(serial_number);
66
if (status == 1)
67
{
68
USB.print(F("Serial number: "));
69
USB.println(serial_number);
70
71
}
72
else
73
{
74
USB.println(F("Error reading the serial number"));
75
}
76
77
// switch off sensor
78
PM.OFF();
79
}
80
else
81
{
82
USB.println(F("Error starting the particle sensor"));
83
}
84
}
85
86
void loop()
87
{
88
///////////////////////////////////////////
89
// 1. Turn on the sensor
90
///////////////////////////////////////////
91
92
// Power on the PM sensor
93
status = PM.ON();
94
95
// check answer
96
if (status == 1)
97
{
98
USB.println(F("Particle sensor started"));
99
}
100
else
101
{
102
USB.println(F("Error starting the particle sensor"));
103
}
104
105
///////////////////////////////////////////
106
// 2. Read sensor
107
///////////////////////////////////////////
108
if (status == 1)
109
{
110
// Power the fan and the laser and perform a measure of 5 seconds
111
measure = PM.getPM(5000, 5000);
112
113
// check answer
114
if (measure == 1)
115
{
116
USB.println(F("Measure performed"));
117
USB.print(F("PM 1: "));
118
USB.printFloat(PM._PM1, 3);
119
USB.println(F(" ug/m3"));
120
USB.print(F("PM 2.5: "));
121
USB.printFloat(PM._PM2_5, 3);
122
USB.println(F(" ug/m3"));
123
USB.print(F("PM 10: "));
124
USB.printFloat(PM._PM10, 3);
125
USB.println(F(" ug/m3"));
126
USB.print(F("Temp: "));
127
USB.print(PM._temp);
128
USB.println(F(" ºC"));
129
USB.print(F("Hum: "));
130
USB.print(PM._hum);
131
USB.println(F(" %RH"));
132
USB.print("Bin: ");
133
for (int i = 0; i < 24; i++)
134
{
135
USB.print(PM._bin[i]);
136
USB.print(";");
137
}
138
USB.println();
139
}
140
else
141
{
142
USB.print(F("Error performing the measure. Error code:"));
143
USB.println(measure, DEC);
144
}
145
}
146
147
///////////////////////////////////////////
148
// 3. Turn off the sensor
149
///////////////////////////////////////////
150
151
// Power off the PM sensor. If there aren't other sensors powered,
152
// turn off the board automatically
153
PM.OFF();
154
155
///////////////////////////////////////////
156
// 4. Create ASCII frame
157
///////////////////////////////////////////
158
159
if (measure == 1)
160
{
161
// Create new frame (ASCII)
162
frame.createFrame(ASCII);
163
// Add sensor values
164
frame.addSensor(SENSOR_GASES_PRO_PM1, PM._PM1);
165
frame.addSensor(SENSOR_GASES_PRO_PM2_5, PM._PM2_5);
166
frame.addSensor(SENSOR_GASES_PRO_PM10, PM._PM10);
167
frame.addSensor(SENSOR_GASES_PRO_TC, PM._temp);
168
frame.addSensor(SENSOR_GASES_PRO_HUM, PM._hum);
169
// Show the frame
170
frame.showFrame();
171
172
// Create new frame (ASCII)
173
frame.createFrame(ASCII);
174
// Add first 16 bins
175
frame.addSensor(SENSOR_GASES_PRO_PM_BINL, PM._binL);
176
// Add last 8 bins
177
frame.addSensor(SENSOR_GASES_PRO_PM_BINH, PM._binH);
178
// Show the frame
179
frame.showFrame();
180
}
181
182
///////////////////////////////////////////
183
// 5. Sleep
184
///////////////////////////////////////////
185
186
// Go to deepsleep
187
// After 3 minutes, Waspmote wakes up thanks to the RTC Alarm
188
PWR.deepSleep("00:00:03:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
189
}
Copied!

Output

1
J#
2
Particle Matter Sensor example
3
Information string extracted:
4
OPC-N3 Iss1.1 FirmwareVer=1.17............................
5
6
Serial number: OPC-N3 177470907
7
Particle sensor started
8
Measure performed
9
PM 1: 1.360 ug/m3
10
PM 2.5: 3.300 ug/m3
11
PM 10: 10.230 ug/m3
12
Temp: 23.5199985504 ºC
13
Hum: 32.9699974060 %RH
14
Bin: 264;44;19;3;3;2;2;0;2;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
15
===============================
16
Current ASCII Frame:
17
Length: 87
18
Frame Type: 134
19
frame (HEX):
20
3C3D3E86052332313439364630353743313035344444234F50435F4E33233023504D313A312E3336303023504D325F353A332E3330303023504D31303A31302E323330302354433A32332E35322348554D3A33332E3023
21
frame (STR):
22
<=>�#21496F057C1054DD#OPC_N3#0#PM1:1.3600#PM2_5:3.3000#PM10:10.2300#TC:23.52#HUM:33.0#
23
===============================
24
===============================
25
Current ASCII Frame:
26
Length: 100
27
Frame Type: 134
28
frame (HEX): 3C3D3E86022332313439364630353743313035344444234F50435F4E33233123504D5F42494E4C3A3236343B34343B31393B333B333B323B323B303B323B303B303B303B303B303B303B3023504D5F42494E483A303B303B303B303B303B303B303B3023
29
frame (STR):
30
<=>�#21496F057C1054DD#OPC_N3#1#PM_BINL:264;44;19;3;3;2;2;0;2;0;0;0;0;0;0;0#PM_BINH:0;0;0;0;0;0;0;0#
31
===============================
Copied!
Last modified 1yr ago