Client

LD2410 is the asyncio serial client from aio_ld2410 to handle LD2410 devices.

Create and Connect

class aio_ld2410.LD2410[source]
__init__(device: str, *, baud_rate: int = 256000, command_timeout: float | None = 2.0, read_bufsize: int | None = None) None[source]

Create a new async client for the provided LD2410 device.

Important

The command timeout affects all command requests when enabled. If the device doesn’t reply within this period, an asyncio.TimoutError is raised (which is equivalent to a TimeoutError) on Python 3.10+.

Parameters:
  • device (str) – path to the device to use.

  • baud_rate (int, default: 256000) – serial baud rate to use.

  • command_timeout (float | None, default: 2.0) – how long to wait for a command reply (in seconds), None to disable.

  • read_bufsize (int | None, default: None) – max buffer size used by the underlying asyncio.StreamReader.

async __aenter__() Self[source]

Enter the device’s context, open the device.

This initializes the serial link and creates the reader asyncio.Task.

This method, along with __aexit__() are called with the following syntax:

async with LD2410('/dev/ttyUSB0'):
    pass
Raises:

RuntimeError – when the device is already in use (entered).

Returns:

Self – The exact same LD2410 object.

async __aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) bool[source]

Exit the device’s context and close the serial link.

The reader task is also canceled.

Returns:

boolFalse to let any exception flow through the call stack.

Properties

class aio_ld2410.LD2410[source]
property configuring: bool

Tell whether a configuration context is currently entered.

property connected: bool

Tell whether we are still connected and receiving data frames.

property entered: bool

Tell whether the device’s context manager is entered.

Configuration mode

class aio_ld2410.LD2410[source]
configure() AsyncIterator[ConfigModeStatus]

Enter the configuration mode.

As stated on the LD2410 documentation, no reports are generated when the configuration mode is active but the device now accepts configuration commands.

Notes

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as dev:
    async with dev.configure():
        # Some configuration commands
Yields:

Device’s protocol information.

This is the standard reply for command CONFIG_ENABLE.

You most likely don’t need this returned value.

Returns:

AsyncIterator[ConfigModeStatus] – An asynchronous iterator (seel YIELDS).

Getters

class aio_ld2410.LD2410[source]
async get_bluetooth_address() bytes[source]

Get the device’s bluetooth mac address.

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as dev:
    async with dev.configure():
        addr = await dev.get_bluetooth_address()

    print('MAC address:', addr.hex(':'))
Returns:

bytes – The MAC address in 6 bytes.

async get_distance_resolution() int[source]

Get the gate distance resolution (in centimeter).

Caution

This command may not be available on your variant or with your firmware.

See also

The internal ResolutionIndex for a list of available resolutions.

Raises:
Returns:

int – The distance resolution in centimeters.

async get_firmware_version() FirmwareVersion[source]

Get the device’s firmware version.

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as device:
    async with device.configure():
        ver = await device.get_firmware_version()
        print(f'[+] Running with firmware v{ver}')
Raises:
Returns:

FirmwareVersion – The firmware version structure.

async get_light_control() LightControlStatus[source]

Get the light controls parameters for OUT pin.

This gets the specific configuration used to control the OUT pin status with the integrated photo sensor.

Caution

This command may not be available on your variant or with your firmware.

Raises:
Returns:

LightControlStatus – The status of the light configuration.

async get_parameters() ParametersStatus[source]

Get the standard configuration parameters.

Raises:
Returns:

ParametersStatus – The currently applied standard parameters.

Setters

class aio_ld2410.LD2410[source]
async reset_to_factory() None[source]

Reset the device to its factory settings.

Tell the device to reset all its parameters to factory settings.

Return type:

None

Important

This command requires a module restart to be effective!

Hint

In factory settings, you get the following parameters:

Max moving distance gate

8

Max static distance gate

8

No-one duration

5 sec

Serial port baud rate

256000 Hz

Bluetooth mode

enabled

Bluetooth password

HiLink

Distance resolution

75 cm

Light control

disabled

Light threshold

128

Light default

LOW

Gate

Moving threshold

Static threshold

0

50%

N/A

1

50%

N/A

2

40%

40%

3

30%

40%

4

20%

30%

5

15%

30%

6

15%

20%

7

15%

20%

8

15%

20%

Raises:
async restart_module(*, close_config_context: bool = False) None[source]

Restart the module immediately.

On my tests it takes at least 1100ms for the module to be responsive again.

Important

If you chose close the current configuration context using close_config_context you will have to exit the context by yourself before you can issue new commands.

Keyword Arguments:

close_config_context – close the surrounding configuration context by raising a ModuleRestartedError (see configure()).

Raises:
Return type:

None

async set_baud_rate(baud_rate: int) None[source]

Set the serial port baud rate.

Important

This command requires a module restart to be effective!

Parameters:

baud_rate (int) – the baud rate you want to apply (see BaudRateIndex).

Return type:

None

See also

The internal BaudRateIndex for a list of available baud rates.

Raises:
async set_bluetooth_mode(enabled: bool) None[source]

Enable of disable bluetooth mode.

Important

This command requires a module restart to be effective!

Parameters:

enabled (bool) – whether bluetooth should be enabled on the device.

Raises:
Return type:

None

async set_bluetooth_password(password: str) None[source]

Set the device’s bluetooth password.

Caution

This command may not be available on your variant or with your firmware.

Parameters:

password (str) – must have less than 7 ASCII characters.

Raises:
Return type:

None

async set_distance_resolution(resolution: int) None[source]

Set the gate distance resolution (in centimeter).

Important

This command requires a module restart to be effective!

Caution

This command seems to be available for a few devices / firmwares.

Parameters:

resolution (int) – per-gate distance (the only valid values are 20 and 75 cm).

Return type:

None

See also

The internal ResolutionIndex for a list of available resolutions.

Raises:
async set_engineering_mode(enabled: bool) None[source]

Enable or disable engineering reports.

Caution

The engineering mode is lost after a device restart.

Parameters:

enabled (bool) – whether ReportStatus should contain an advanced report.

Return type:

None

See also

Raises:
async set_gate_sensitivity(**kwargs: Unpack[GateSensitivityConfig]) None[source]

Set gate sensitivities.

This command is used to configure gate sensitivity to one or to all gate.

Return type:

None

Hint

See GateSensitivityConfig for keyword arguments.

Tip

  • These parameters apply immediately and are persistent across module restart.

  • distance_gate keyword argument can be set to 0xFFFF to broadcast to all gates.

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as dev:
    async with dev.configure():
        # Set sensitivities for gate 4.
        await dev.set_gate_sensitivity(
            distance_gate=4,
            moving_threshold=25,
            static_threshold=20,
        )
Raises:
async set_light_control(**kwargs: Unpack[LightControlConfig]) None[source]

Set the light controls parameters for OUT pin.

This sets the specific configuration used to control the OUT pin status with the integrated photo sensor.

Return type:

None

Caution

This command may not be available on your variant or with your firmware.

Hint

See LightControlConfig for keyword arguments.

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as dev:
    async with dev.configure():
        await dev.set_light_control(
            control=LightControl.BELOW,
            default=OutPinLevel.LOW,
            threshold=120,
        )
Raises:
async set_parameters(**kwargs: Unpack[ParametersConfig]) None[source]

Set the standard configuration parameters.

This method only sets basic configuration parameters.

Return type:

None

Hint

See ParametersConfig for keyword arguments.

Tip

These parameters apply immediately and are persistent across module restart.

USE EXAMPLE:

async with LD2410('/dev/ttyUSB0') as dev:
    async with dev.configure():
        await dev.set_parameters(
            moving_max_distance_gate=7,
            static_max_distance_gate=7,
            presence_timeout=5,
        )

See also

set_gate_sensitivity() to set additional parameters.

Raises:

Reports

class aio_ld2410.LD2410[source]
get_last_report() ReportStatus | None[source]

Get the latest report received from the device, if any.

Note

This report can be very outdated if you spent too much time in configuration mode.

Tip

This method does not way for anything and it not asynchronous.

Returns:

ReportStatus | None – The last report we received from the device.

async get_next_report() ReportStatus[source]

Wait and get the next available report.

Caution

Must be called outside of the configuration context as no report is being generated in configuration mode.

Returns:

ReportStatus – The next report we just received from the device (if any).

async get_reports() AsyncIterator[ReportStatus][source]

Get reports as they arrive with an asynchronous iterator.

This is just a simple loop around get_next_report().

Caution

Must be called outside of the configuration context as no report is being generated in configuration mode.

Yields:

An asynchronous iterator for ReportStatus.

Returns:

AsyncIterator[ReportStatus] – An asynchronous iterator (seel YIELDS).