gpio#

Description#

This plugin can be used to interact with custom electronic devices connected to a Raspberry Pi (or compatible device) over GPIO pins.

Example:

pins:
    LED_1: 14,
    LED_2: 15,
    MOTOR: 16,
    SENSOR_1: 17
    SENSOR_2: 18

Configuration#

gpio:
  # [Optional]
  # Custom `GPIO name` -> `PIN number` mapping. This can be
  # useful if you want to reference your GPIO ports by name instead
  # of PIN number.
  # pins:   # type=Optional[Dict[str, int]]

  # [Optional]
  # List of PINs to monitor. If a new value is detected
  # on these pins then a `GPIOEvent <https://docs.platypush.tech/platypush/events/gpio.html#platypush.message.event.gpio.GPIOEvent>`_
  # event will be triggered. GPIO PINS can be referenced either by number
  # or name, if a name is specified on the `pins` argument.
  # monitored_pins:   # type=Optional[Collection[Union[str, int]]]

  # [Optional]
  # Specify ``board`` if you want to use the board PIN numbers,
  # ``bcm`` for Broadcom PIN numbers (default: ``board``)
  # mode: board  # type=str

  # [Optional]
  # How often the `RunnablePlugin.loop <https://docs.platypush.tech/platypush/plugins/.html#platypush.plugins.RunnablePlugin.loop>`_ function should be
  # executed (default: 15 seconds). *NOTE*: For back-compatibility
  # reasons, the `poll_seconds` argument is also supported, but it's
  # deprecated.
  # poll_interval: 15  # type=Optional[float]

  # [Optional]
  # How long we should wait for any running
  # threads/processes to stop before exiting (default: 5 seconds).
  # stop_timeout: 5  # type=Optional[float]

  # [Optional]
  # If set to True then the plugin will not monitor
  # for new events. This is useful if you want to run a plugin in
  # stateless mode and only leverage its actions, without triggering any
  # events. Defaults to False.
  # disable_monitor: False  # type=bool

Dependencies#

pip

pip install RPi.GPIO

Triggered events#

Actions#

Module reference#

class platypush.plugins.gpio.GpioPlugin(pins: Dict[str, int] | None = None, monitored_pins: Collection[str | int] | None = None, mode: str = 'board', **kwargs)[source]#

Bases: RunnablePlugin

This plugin can be used to interact with custom electronic devices connected to a Raspberry Pi (or compatible device) over GPIO pins.

__init__(pins: Dict[str, int] | None = None, monitored_pins: Collection[str | int] | None = None, mode: str = 'board', **kwargs)[source]#
Parameters:
  • mode – Specify board if you want to use the board PIN numbers, bcm for Broadcom PIN numbers (default: board)

  • pins – Custom GPIO name -> PIN number mapping. This can be useful if you want to reference your GPIO ports by name instead of PIN number.

Example:

pins:
    LED_1: 14,
    LED_2: 15,
    MOTOR: 16,
    SENSOR_1: 17
    SENSOR_2: 18
Parameters:

monitored_pins – List of PINs to monitor. If a new value is detected on these pins then a platypush.message.event.gpio.GPIOEvent event will be triggered. GPIO PINS can be referenced either by number or name, if a name is specified on the pins argument.

cleanup()[source]#

Cleanup the state of the GPIO and resets PIN values.

main()[source]#

Implementation of the main loop of the plugin.

read(pin: int | str, name: str | None = None) Dict[str, Any][source]#

Reads a value from a PIN.

Parameters:
  • pin – PIN number or configured name.

  • name – Optional name for the read value (e.g. “temperature” or “humidity”)

Response:

output = {
    "name": <pin number or pin/metric name>,
    "pin": <pin>,
    "value": <value>,
    "method": "read"
}
read_all()[source]#

Reads the values from all the configured PINs and returns them as a list. It will raise a RuntimeError if no PIN mappings were configured.

start()#

Start the plugin.

stop()#

Stop the plugin.

wait_stop(timeout=None)#

Wait until a stop event is received.

write(pin: int | str, value: int | bool, name: str | None = None) Dict[str, Any][source]#

Write a byte value to a pin.

Parameters:
  • pin – PIN number or configured name

  • name – Optional name for the written value (e.g. “temperature” or “humidity”)

  • value – Value to write

Response:

output = {
    "name": <pin or metric name>,
    "pin": <pin>,
    "value": <value>,
    "method": "write"
}