OpenANT Python Library

repository·master·Indexed 42 days ago

https://github.com/tigge/openant

A Python library and CLI toolset for interacting with ANT and ANT+ devices. It provides interfaces for basic ANT communication, ANT-FS (File System), and high-level ANT+ device profiles. The toolset includes capabilities to scan for nearby devices and stream ANT+ data to InfluxDB (version 2.0+) and MQTT servers.

Tokens
2K
Snippets
12
Records
13
Agent score
47%

What's inside OpenANT

  1. Overview of the openant.base package

    master

    The openant.base package serves as the foundational layer for the OpenANT project. It provides the core abstractions and modules required to interface with ANT USB sticks and manage communication protocols. The package is organized into several specialized submodules:

    • openant.base.ant: Core ANT-specific functionality.
    • openant.base.commons: Shared utility functions and common logic.
    • openant.base.driver: Low-level driver interfaces for hardware communication.
    • openant.base.message: Logic for handling and constructing ANT messages.
  2. Install MQTT CLI Tool support

    master

    To use the openant mqtt command, you must install the mqtt extra:

    pip install openant[mqtt]

    Alternatively, you can install the paho-mqtt module manually. This tool requires an MQTT server.

    pip install openant[mqtt]
  3. Install InfluxDB CLI Tool support

    master

    To use the openant influx command, you must install the influx extra:

    pip install openant[influx]

    Alternatively, you can install the influxdb-client module manually. This tool requires an InfluxDB server version 2.0 or higher.

    pip install openant[influx]
  4. Install openant

    master

    Install the core library via pip:

    pip install openant

    To install the latest development version (HEAD):

    pip install git+https://github.com/Tigge/openant#egg=openant

    Platform Specifics

    • Linux: You must install a udev rule for the Dynastream ANTUSB stick to run without elevated privileges:
      sudo python -m openant.udev_rules
    - **Windows**: Follow the [libusb driver installation instructions](https://github.com/libusb/libusb/wiki/Windows#Driver_Installation).
    - **macOS**: Should work with just `libusb` installed.
    
    pip install openant
  5. Install udev rules for ANT USB sticks on Linux

    master

    To allow non-root users to access ANT USB sticks on Linux, you must install specific udev rules. This utility copies the rule file resources/42-ant-usb-sticks.rules to /etc/udev/rules.d, reloads the udev rules, and triggers the subsystem to apply changes immediately.

    Requirements:

    • The operating system must be Linux.
    • You must run the script with root privileges (e.g., using sudo).

    If you are running this as a standalone script, it will attempt to install the rules and raise an OSError if requirements are not met.

    sudo python openant/udev_rules.py
  6. Stream ANT+ data to MQTT

    master

    The openant mqtt command streams DeviceData from an ANT+ device to an MQTT server. This is useful for integrating ANT data into wider systems.

    Publishing Behavior:

    • Default: All received data is published as a single JSON-encoded string to openant/<DeviceType>/<DeviceId>.
    • --topic-per-field: Instructs the application to publish data to individual topics for each data field (e.g., one for heart_rate, another for battery_percentage).
    • --device-topic <topic>: Overrides the default topic used for posting a device's data.

    Usage Examples:

    # attach to first trainer found and push data to localhost MQTT
    openant mqtt --verbose FitnessDevice
    
    # attach to power meter with device id 12345 and push to localhost MQTT
    openant mqtt --id 12345 --verbose PowerMeter
    
    # attach to devices in 'devices.json' - allows connection to multiple devices
    openant mqtt --verbose --config devices.json config
    
    # attach to devices in 'devices.json' and post a specific device's data to the topic my/heartrate
    openant mqtt --config devices.json --device-topic 120:54368:my/heartrate config
    openant mqtt --verbose FitnessDevice
  7. Stream ANT+ data to InfluxDB

    master

    The openant influx command streams DeviceData from an ANT+ device to an InfluxDB instance. This is useful for real-time plotting and post-review.

    Usage Examples:

    # attach to first trainer found and push data to localhost InfluxDB
    openant influx --verbose FitnessDevice
    
    # attach to power meter with device id 12345 and push to localhost InfluxDB
    openant influx --id 12345 --verbose PowerMeter
    
    # attach to devices in 'devices.json' - allows connection to multiple devices
    openant influx --verbose --config devices.json config

    Refer to openant influx --help for specific server setup flags.

    openant influx --verbose FitnessDevice
  8. Scan for nearby ANT devices

    master

    Use the openant scan command to find nearby devices. You can filter by device type or save the results to a file.

    Flags:

    • --device_type: Search for specific device types.
    • --outfile <path>: Save found devices to a file.
    • --auto_create: Instantiate the device object when found so that device data is also printed.
    • --logging: Enable logging output.
    # print devices found to terminal
    openant scan
    
    # capture devices found to devices.json for use with antinflux
    openant scan --outfile devices.json
    
    # instantiate object when found so that device data is also printed
    openant scan --auto_create
  9. Use install_udev_rules() to configure Linux permissions

    master

    The install_udev_rules(raise_exception) function is the primary entry point for installing the necessary udev rules for ANT USB sticks. It checks if the platform is Linux and verifies root privileges.

    Parameters:

    • raise_exception (bool): If True, the function raises an OSError if the platform is not Linux or if the user lacks root privileges. If False, it prints an error message instead.
    from openant.udev_rules import install_udev_rules
    
    try:
        install_udev_rules(raise_exception=True)
    except OSError as e:
        print(f"Failed to install rules: {e}")
  10. Access OpenANT core modules

    master

    The openant package provides access to several core modules for interacting with ANT hardware and protocols. You can import these modules directly to build your own applications:

    • openant.base: Core functionality.
    • openant.easy: High-level, simplified API for common tasks.
    • openant.fs: ANT File System (ANT-FS) support.
    • openant.devices: Device-specific implementations and management.
    import openant
    
    # Accessing modules
    base = openant.base
    easy = openant.easy
    fs = openant.fs
    devices = openant.devices
  11. Use the OpenANT CLI

    master

    OpenANT includes a command-line interface for running various sub-commands. The CLI supports global options for logging and version information.

    Global Options

    • --logging: Set the logging level. Valid choices are DEBUG, INFO, WARNING, ERROR, or CRITICAL.
    • --version: Display the current version of OpenANT.

    Commands

    Commands are implemented as sub-commands. The available commands are dynamically loaded from the subparsers directory within the package. If a specific command's dependencies are missing, the CLI will report that the command is unavailable rather than crashing.

    # Example: Set logging level to DEBUG
    python -m openant --logging DEBUG <command> [args]
    
    # Example: Check version
    python -m openant --version
  12. Use the openant CLI

    master

    The openant package provides a command-line interface (CLI) for interacting with ANT USB sticks and managing device data. You can invoke the CLI directly using the openant command if the package is installed in your environment's path, or via python -m openant.

    python -m openant [COMMAND]