platypush.plugins.arduino

class platypush.plugins.arduino.ArduinoPlugin(board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: int = 57600, analog_pins: Optional[Dict[str, int]] = None, digital_pins: Optional[Dict[str, int]] = None, timeout: float = 20.0, conv_functions: Optional[Dict[Union[str, int], Union[str, Callable]]] = None, **kwargs)[source]

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)
__init__(board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: int = 57600, analog_pins: Optional[Dict[str, int]] = None, digital_pins: Optional[Dict[str, int]] = None, timeout: float = 20.0, conv_functions: Optional[Dict[Union[str, int], Union[str, Callable]]] = None, **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: 57600)
  • 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: Union[int, str], board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, conv_function: Union[str, Callable, None] = None, timeout: Optional[int] = None) → float[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: Union[int, str], value: float, board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, timeout: Optional[int] = 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: Union[int, str], board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, timeout: Optional[int] = 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: Union[int, str], value: bool, board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, timeout: Optional[int] = 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: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, timeout: Optional[int] = None) → Dict[str, float][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: Union[int, str], value: float, board: Optional[str] = None, board_type: Optional[str] = None, baud_rate: Optional[int] = None, timeout: Optional[int] = 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).
class platypush.plugins.arduino.BoardType[source]

An enumeration.

class platypush.plugins.arduino.PinType[source]

An enumeration.