arduino#

class platypush.plugins.arduino.ArduinoPlugin(*_, **__)[source]#

Bases: SensorPlugin

Interact with an Arduino connected to the host machine over USB using the Firmata protocol.

You have two options to communicate with an Arduino-compatible board over USB:

  • Use this plugin if you want to use the general-purpose Firmata protocol - in this case most of your processing logic will be on the host side and you can read/write data to the Arduino transparently.

  • Use the platypush.plugins.serial.SerialPlugin if instead you want to run more custom logic on the Arduino and communicate back with the host computer through JSON formatted messages.

Download and flash the Standard Firmata firmware to the Arduino in order to use this plugin.

Requires:

  • pyfirmata2 (pip install pyfirmata2)

Triggers:

__init__(board: str | None = None, board_type: str | None = None, baud_rate: int = 9600, analog_pins: Dict[str, int] | None = None, digital_pins: Dict[str, int] | None = None, timeout: float = 20.0, conv_functions: Dict[str | int, str | Callable] | None = None, poll_interval: float = 1.0, **kwargs)[source]#
Parameters:
  • board – Default board name or path (e.g. COM3 on Windows or /dev/ttyUSB0 on Unix). If not set then the plugin will attempt an auto-discovery.

  • board_type – Default board type. It can be ‘mega’, ‘due’ or ‘nano’. Leave empty for auto-detection.

  • baud_rate – Default serial baud rate (default: 9600)

  • analog_pins – Optional analog PINs map name->pin_number.

  • digital_pins – Optional digital PINs map name->pin_number.

  • timeout – Board communication timeout in seconds.

  • conv_functions

    Optional mapping of conversion functions to apply to the analog values read from a certain PIN. The key can either be the PIN number or the name as specified in analog_pins, the value can be a function that takes an argument and transforms it or its lambda string representation. Note that analog_read returns by default float values in the range [0.0, 1.0]. Example:

    arduino:
        board: /dev/ttyUSB0
        analog_pins:
            temperature: 1  # Analog PIN 1
    
        conv_functions:
            temperature: 'lambda t: t * 500.0'
    

analog_read(pin: int | str, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, conv_function: str | Callable | None = None, timeout: int | None = None) float | None[source]#

Read an analog value from a PIN.

Parameters:
  • pin – PIN number or configured name.

  • board – Board path or name (default: default configured board).

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate).

  • conv_function – Optional conversion function override to apply to the output. It can be either a function object or its lambda string representation (e.g. lambda x: x*x). Keep in mind that analog_read returns by default float values in the range [0.0, 1.0].

  • timeout – Communication timeout in seconds (default: default configured timeout).

analog_write(pin: int | str, value: float, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, timeout: int | None = None)[source]#

Write a value to an analog PIN.

Parameters:
  • pin – PIN number or configured name.

  • value – Voltage to be sent, a real number normalized between 0 and 1.

  • board – Board path or name (default: default configured board).

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate).

  • timeout – Communication timeout in seconds (default: default configured timeout).

digital_read(pin: int | str, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, timeout: int | None = None) bool[source]#

Read a digital value from a PIN.

Parameters:
  • pin – PIN number or configured name.

  • board – Board path or name (default: default configured board).

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate).

  • timeout – Communication timeout in seconds (default: default configured timeout).

digital_write(pin: int | str, value: bool, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, timeout: int | None = None)[source]#

Write a value to a digital PIN.

Parameters:
  • pin – PIN number or configured name.

  • value – True (HIGH) or False (LOW).

  • board – Board path or name (default: default configured board).

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate).

  • timeout – Communication timeout in seconds (default: default configured timeout).

get_measurement(*_, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, timeout: int | None = None, **__) Dict[str, bool | float | None][source]#

Get a measurement from all the configured PINs.

Parameters:
  • board – Board path or name (default: default configured board)

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate)

  • timeout – Communication timeout in seconds (default: default configured timeout).

Returns:

dict, where the keys are either the configured names of the PINs (see analog_pins configuration) or all the analog PINs (names will be in the format ‘A0..A7’ in that case), and the values will be the real values measured, either normalized between 0 and 1 if no conversion functions were provided, or transformed through the configured conv_functions.

pwm_write(pin: int | str, value: float, board: str | None = None, board_type: str | None = None, baud_rate: int | None = None, timeout: int | None = None)[source]#

Write a PWM value to a digital PIN.

Parameters:
  • pin – PIN number or configured name.

  • value – PWM real value normalized between 0 and 1.

  • board – Board path or name (default: default configured board).

  • board_type – Board type. It can be ‘mega’, ‘due’ or ‘nano’ (default: configured board_type).

  • baud_rate – Baud rate (default: default configured baud_rate).

  • timeout – Communication timeout in seconds (default: default configured timeout).

stop()[source]#

Stop the plugin.

transform_entities(entities: Dict[str, int | float]) List[Device][source]#

This method takes a list of entities in any (plugin-specific) format and converts them into a standardized collection of Entity objects. Since this method is called by publish_entities() before entity updates are published, you may usually want to extend it to pre-process the entities managed by your extension into the standard format before they are stored and published to all the consumers.

class platypush.plugins.arduino.BoardType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Board types.

class platypush.plugins.arduino.PinType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: IntEnum

PIN type enumeration (analog or digital).