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) 2017 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.1
    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"));

  // switch on sensor
  status = OPC_N2.ON();

  // check answer
  if (status == 1)
  {
    // get info from sensor
    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"));
    }

    // switch off 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
  status = OPC_N2.ON();

  // check answer
  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);
    
    // check answer
    if (measure == 1)
    {
      USB.println(F("Measure performed"));
      USB.print(F("PM 1: "));
      USB.printFloat(OPC_N2._PM1, 3);
      USB.println(F(" ug/m3"));
      USB.print(F("PM 2.5: "));
      USB.printFloat(OPC_N2._PM2_5, 3);
      USB.println(F(" ug/m3"));
      USB.print(F("PM 10: "));
      USB.printFloat(OPC_N2._PM10, 3);
      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();

  ///////////////////////////////////////////
  // 4. 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-018.2..............................

Particle sensor started
Measure performed
PM 1: 2.590 ug/m3
PM 2.5: 3.160 ug/m3
PM 10: 3.180 ug/m3

Quick Publish: