gpio.sensor.distance

gpio.sensor.distance

class platypush.plugins.gpio.sensor.distance.GpioSensorDistancePlugin(trigger_pin: int, echo_pin: int, measurement_interval: float = 0.15, timeout: float = 2.0, warmup_time: float = 2.0, *args, **kwargs)[source]

You can use this plugin to interact with a distance sensor on your Raspberry Pi (tested with a HC-SR04 ultrasound sensor).

Requires:

  • RPi.GPIO (pip install RPi.GPIO)

Triggers:

__init__(trigger_pin: int, echo_pin: int, measurement_interval: float = 0.15, timeout: float = 2.0, warmup_time: float = 2.0, *args, **kwargs)[source]
Parameters:
  • trigger_pin – GPIO PIN where you connected your sensor trigger PIN (the one that triggers the sensor to perform a measurement).

  • echo_pin – GPIO PIN where you connected your sensor echo PIN (the one that will listen for the signal to bounce back and therefore trigger the distance calculation).

  • measurement_interval – When running in continuous mode (see platypush.plugins.gpio.sensor.distance.GpioSensorDistancePlugin.start_measurement()) this parameter indicates how long should be waited between two measurements (default: 0.15 seconds)

  • timeout – The echo-wait will terminate and the plugin will return null if no echo has been received after this time (default: 1 second).

  • warmup_time – Number of seconds that should be waited on plugin instantiation for the sensor to be ready (default: 2 seconds).

cleanup()

Cleanup the state of the GPIO and resets PIN values.

get_data(*args, **kwargs)

Alias for get_measurement

get_measurement()[source]

Extends GpioSensorPlugin.get_measurement()

Returns:

Distance measurement as a scalar (in mm):

read(pin: Union[int, str], name: Optional[str] = None) Dict[str, Any]

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()

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_measurement(duration: Optional[float] = None)[source]

Start the measurement thread. It will trigger platypush.message.event.distance.DistanceSensorEvent events when new measurements are available.

Parameters:

duration – If set, then the thread will run for the specified amount of seconds (default: None)

stop_measurement()[source]

Stop the running measurement thread.

write(pin: Union[int, str], value: Union[int, bool], name: Optional[str] = None) Dict[str, Any]

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"
}