The SDS011 Class:

class src.utils.sds011.SDS011(serial_port: str, baudrate: int = 9600, timeout: int = 2, use_query_mode: bool = True)[source]

Provides functionality to read from a SDS011 air particlate density sensor using UART serial communication.

__init__(serial_port: str, baudrate: int = 9600, timeout: int = 2, use_query_mode: bool = True)[source]

Initialise and open serial port.

Parameters:
  • serial_port (str) – The serial port (device path) on which the sensor is mounted. Ex: “/dev/ttyUSB0”

  • baudrate (int) – The rate at which information is transferred in a communication channel. See baudrate.

  • timeout (int) – The number of seconds to wait for a response from the sensor.

  • use_query_mode (bool) – Whether to use the sensor’s query mode. This should always be set to true for out applications.

__weakref__

list of weak references to the object

_execute(cmd_bytes: bytes)[source]

Writes a byte sequence to the serial port (device).

Parameters:

cmd_bytes (bytes) – The bytes that should be sent.

_finish_cmd(cmd: bytes, id1: bytes = b'\xff', id2: bytes = b'\xff') bytes[source]

Add device ID, checksum and tail bytes.

Parameters:
  • cmd (bytes) – The partial command bytes to which we should append the device id, checksum, and tail bytes.

  • id1 (bytes) – The first id byte of the destination device (default all)

  • id2 (bytes) – The second id byte of the destination device (default all)

Returns:

The command cmd after the device ID, checksum, and tail bytes have been appended.

_get_reply() bytes | None[source]

Read reply from device.

Returns:

The 10 bytes consisting of the device’s response.

_process_frame(data: bytes) Tuple[float, float][source]

Processes a SDS011 data frame.

Byte positions:

0 - Header

1 - Command No.

2,3 - PM2.5 low/high byte

4,5 - PM10 low/high

6,7 - ID bytes

8 - Checksum - sum of bytes 2-7

9 - Tail

Parameters:

data (bytes) – The data bytes that should be processed.

Returns:

A tuple containing the pm25 and pm10 values queried from the device in the form (pm25, pm10).

cmd_begin() bytes[source]

Get command header and command ID bytes.

Returns:

The sequence of bytes corresponding to the HEAD and CMD_ID bytes as identified in the device’s data sheet.

query() Tuple[float, float][source]

Query the device and read the data.

Returns:

Air particulate density in micrograms per cubic meter in the form of a tuple of floats (pm25, pm10)

read() Tuple[float, float][source]

Read sensor data.

Returns:

PM25 and PM10 concentration in micrograms per cubic meter in the form (pm25, pm10).

set_report_mode(read: bool = False, active: bool = False)[source]

Get sleep command. Does not contain checksum and tail.

Parameters:
  • read (bool) – Boolean flag indicating whether we should query or set the report mode of the device.

  • active (bool) – Boolean flag indicating whether we should set the report mode of the device to active (True) or query (False) modes.

set_work_period(read: bool = False, work_time: int = 0)[source]

Get work period command. Does not contain checksum and tail.

Parameters:
  • read (bool) – Boolean flag indicating whether this should query the device or modify its behavior.

  • work_time (int) – The working period of the device in seconds

sleep(read: bool = False, sleep: bool = True)[source]

Sleep/Wake up the sensor.

Parameters:
  • read (bool) – Boolean flag indicating whether this should query the device or modify its behavior.

  • sleep (bool) – Boolean flag indicating whether the device should sleep or work.