Device::SDS011 - Module to work with SDS011 particulate matter laser sensor
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; }
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.
Device::SerialPort
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.
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.
Device::SDS011
The $usb_device option is passed on to Device::SerialPort (please see documentation for this module).
$usb_device
my $sensor = Device::SDS011->new('/dev/ttyUSB0');
Returns current PM readings as an arrayref [PM 2.5, PM 10].
[PM 2.5, PM 10]
Ex.: [8.77,17.73]
[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.
Requests sensor data. Returns current PM readings as an arrayref [PM 2.5, PM 10].
Also see the reporting_mode() method.
reporting_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).
0
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.
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."
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.
1-30
Returns sensor's firmware version: a string of format YY-MM-DD (year, month, date).
YY-MM-DD
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.
Destroys the Device::SerialPort object. Re-connect is not possible.
Irakliy Sunguryan
Repository: https://github.com/OpossumPetya/pi-air-monitor.
Please report any bugs at GitHub, or RT.
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.
To install Device::SDS011, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Device::SDS011
CPAN shell
perl -MCPAN -e shell install Device::SDS011
For more information on module installation, please visit the detailed CPAN module installation guide.