NAME
Device::SDS011 - Module to work with SDS011 particulate matter laser sensor
SYNOPSIS
use Device::SDS011;
my $sensor = Device::SDS011->new('/dev/ttyUSB0');
$sensor->sensor_mode(1); # wake up (if in sleeping mode)
sleep 5;
$sensor->reporting_mode(1); # 1 = Report query mode
$sensor->working_period(0); # 0 = Continuous mode
while (1) {
my ($pm25, $pm10);
for (1..3) {
my ($pm25_tmp,$pm10_tmp) = @{$sensor->query_data};
$pm25 += $pm25_tmp;
$pm10 += $pm10_tmp;
sleep 3;
}
printf "PM25:%.2f, PM10:%.2f\n", $pm25/3, $pm10/3;
$sensor->sensor_mode(0); # enter sleep mode
sleep 60 * 15;
}
DESCRIPTION
Module to receive data from, and control SDS011 particulate matter sensor.
This module uses Device::SerialPort
for communicating with sensor. Laser Dust Sensor Control Protocol v1.3 is implemented.
Data retrieved
SDS011 uses the principle of laser scattering in the air, can be obtained from 0.3 to 10 microns suspended particulate matter concentration.
This module allows retrieving PM 2.5 mass in mg/m3 and PM 10 mass in mg/m3 sensor readings.
CONSTRUCTOR
Device::SDS011->new( $usb_device )
Creates and returns a new Device::SDS011
object, open specified port, and configure it according to the protocol: 9600 bps with 8 data bit, no parity, one stop bit.
The $usb_device
option is passed on to Device::SerialPort
(please see documentation for this module).
my $sensor = Device::SDS011->new('/dev/ttyUSB0');
METHODS
$sensor->live_data
Returns current PM readings as an arrayref [PM 2.5, PM 10]
.
Ex.: [8.77,17.73]
By default the sensor device works in Active reporting mode with Continuous working period, which means it reports PM readings every 1 second continuously. This method can be used to read the values.
$sensor->query_data
Requests sensor data. Returns current PM readings as an arrayref [PM 2.5, PM 10]
.
Also see the reporting_mode()
method.
$sensor->reporting_mode
$sensor->reporting_mode( $mode )
Sets report mode. Valid values: 0
(active) and 1
(query). When parameter value is not specified it returns current reporting mode (0
or 1
).
* Report active mode Sensor automatically reports a measurement data in a work period.
* Report query mode Sensor received query data command to report a measurement data.
$sensor->sensor_mode
$sensor->sensor_mode( $mode )
Sets sensor mode. Valid values: 0
(sleep) and 1
(work). When parameter value is not specified it returns current sensor mode (0
or 1
).
"Service life is the key parameter of laser dust sensor. The laser diode in this sensor has high quality and its service life is up to 8000 hours. If you don't need real-time data (such as filter, air quality monitoring, etc.), you can use the discontinuous working method to prolong the service life."
$sensor->working_period
$sensor->working_period( $mode )
Sets working period. Valid values: 0
(continuous), and 1-30
minute(s) -- work 30 seconds, and sleep n*60-30 seconds. When parameter value is not specified it returns current working period.
$sensor->firmware
Returns sensor's firmware version: a string of format YY-MM-DD
(year, month, date).
$sensor->device_id
$sensor->device_id( $id_byte_1, $id_byte_2 )
say join ' ', map { sprintf '%02x', $_ } @{$sensor->device_id};
my $newID = $sensor->device_id( 0xD0, 0xEA ); # returns [0xD0,0xEA]
Sets Device ID. Returns new device ID -- arrayref to two ID bytes. When no ID specified returns current device ID.
NOTE: All methods of this module will save/update (internally) the Device ID, since all commands return it. If this command is called first, it will (ab)use reporting_mode() method to get the ID.
$sensor->done
Destroys the Device::SerialPort
object. Re-connect is not possible.
AUTHOR
Irakliy Sunguryan
DEVELOPMENT & ISSUES
Repository: https://github.com/OpossumPetya/pi-air-monitor.
Please report any bugs at GitHub, or RT.
LICENSE AND COPYRIGHT
Copyright 2019 Irakliy Sunguryan
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.