Development

» SCP 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 Smart Cities PRO board
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 Smart Cities PRO technical guide.

Code

/*  
 *  ------------  [SCP_v30_04] - Particle Matter Sensor  -------------- 
 *  
 *  Explanation: This is the basic code to manage and read the particle
 *  sensor. 
 *  
 *  Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify  
 *  it under the terms of the GNU General Public License as published by  
 *  the Free Software Foundation, either version 3 of the License, or  
 *  (at your option) any later version.  
 *   
 *  This program is distributed in the hope that it will be useful,  
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 *  GNU General Public License for more details.  
 *   
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 * 
 *  Version:           3.0
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

#include <WaspOPC_N2.h>

/*
 * P&S! Possibilities for this sensor:
 * 	- SOCKET_D
 */

char info_string[61];
int status;
int measure;

void setup()
{
    USB.println(F("Particle Matter Sensor example"));
    status = OPC_N2.ON();
    if (status == 1)
    {
        status = OPC_N2.getInfoString(info_string);
        if (status == 1)
        {
            USB.println(F("Information string extracted:"));
            USB.println(info_string);

        }
        else
        {
            USB.println(F("Error reading the particle sensor"));
        }

        OPC_N2.OFF();
    }
    else
    {
        USB.println(F("Error starting the particle sensor"));
    }
}	


void loop()
{
    ///////////////////////////////////////////
    // 1. Turn on the sensor
    /////////////////////////////////////////// 

    // Power on the OPC_N2 sensor. 
    // If the Smart Cities PRO board is off, turn it on automatically.
    status = OPC_N2.ON();
    if (status == 1)
    {
        USB.println(F("Particle sensor started"));

    }
    else
    {
        USB.println(F("Error starting the particle sensor"));
    }

    ///////////////////////////////////////////
    // 2. Read sensor
    ///////////////////////////////////////////  
    if (status == 1)
    {
        // Power the fan and the laser and perform a measure of 5 seconds
        measure = OPC_N2.getPM(5000,5000);
        if (measure == 1)
        {
            USB.println(F("Measure performed"));
            USB.print(F("PM 1: "));
            USB.print(OPC_N2._PM1);
            USB.println(F(" ug/m3"));
            USB.print(F("PM 2.5: "));
            USB.print(OPC_N2._PM2_5);
            USB.println(F(" ug/m3"));
            USB.print(F("PM 10: "));
            USB.print(OPC_N2._PM10);
            USB.println(F(" ug/m3"));

        }
        else
        {
            USB.print(F("Error performing the measure. Error code:"));
            USB.println(measure, DEC);
        }
    }


    ///////////////////////////////////////////
    // 3. Turn off the sensor
    /////////////////////////////////////////// 

    // Power off the OPC_N2 sensor. If there aren't other sensors powered, 
    // turn off the board automatically
    OPC_N2.OFF();

    ///////////////////////////////////////////
    // 5. Sleep
    /////////////////////////////////////////// 

    // Go to deepsleep	
    // After 3 minutes, Waspmote wakes up thanks to the RTC Alarm
    PWR.deepSleep("00:00:03:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

}

Output

H#
Particle Matter Sensor example
Information string extracted:
OPC-N2 FirmwareVer=OPC-017b...............................

Particle sensor started
Measure performed
PM 1: 0.1799999952 ug/m3
PM 2.5: 0.1900000000 ug/m3
PM 10: 0.1900000000 ug/m3
Particle sensor started
Measure performed
PM 1: 0.3299999713 ug/m3
PM 2.5: 1.3700000047 ug/m3
PM 10: 2.1499998569 ug/m3
Particle sensor started
Measure performed
PM 1: 0.2399999856 ug/m3
PM 2.5: 0.4799999713 ug/m3
PM 10: 0.5099999904 ug/m3
Particle sensor started
Measure performed
PM 1: 0.2099999904 ug/m3
PM 2.5: 0.6100000381 ug/m3
PM 10: 0.7400000095 ug/m3

Quick Publish: