# RaspberryPi task

A RaspberryPi task reads and sets the GPIO pins of a Raspberry Pi: the row of contacts on the board to which switches, sensors, relays and lamps are wired. With it a job can act on the physical world, and react to it: switch a relay when a file arrives, start a transfer when a button is pressed, light a lamp while a job runs.

The task works only where CrushMFT, or one of its [job engines](Jobs-Engines-Other-Machines.md), runs on a Raspberry Pi.

> [!WARNING]
> The task starts command line tools on the machine. Only full administrators can put it into a job; no role can grant it.

## Fields

| Field | What it does | On a new task |
|---|---|---|
| **Pins (one per line)** | what to do with which pin: see below | |
| **Pin numbers** | how the pins are numbered: **GPIO (BCM)**, the numbers printed in every pin chart, or **WiringPi**, the older scheme | GPIO (BCM) |
| **Wait for this many changes** | for pins that are read with `block`: the task goes on after so many changes | |
| **Stop waiting after** | milliseconds. 0 waits for ever. | 0 |
| **Check pins every** | milliseconds between two looks at the pins | 100 |
| **Only pass on pins that were written or changed** | pins that were read and did not change are not put on the list | on |

### The pins

> Each line is a pin number, then read or write. "17 write high" sets pin 17 high. "27 read up block" reads pin 27 with its pull-up on and waits until it changes.
>
> Write options: high, low. Read options: up, down, off (pull resistor, default down) and block (wait for a change). Variables like {my_state} work too.

| Line | Does |
|---|---|
| `17 write high` | sets pin 17 high: the relay on it closes, the lamp lights |
| `17 write low` | sets it low |
| `17 write {lamp}` | sets it by a [variable](Jobs-Variables.md) of the job that holds `high` or `low` |
| `27 read` | reads pin 27, with the pull-down resistor on |
| `27 read up` | reads it with the pull-up on: for a switch wired to ground |
| `27 read off` | reads it with no pull resistor |
| `27 read up block` | reads it and **waits** until it changes |

A pin that is read needs a pull resistor, or its value drifts when nothing is connected. A switch between the pin and ground wants `up` and reads LOW when pressed; a switch between the pin and 3.3 V wants `down` and reads HIGH when pressed.

## What it hands on

> Each pin becomes an item pin://number/mode/STATE, for example pin://27/read/HIGH.

The items it was given, and one item per pin. Each pin's item has `{state}`, HIGH or LOW, besides its address. A [Jump](Task-Jump.md) behind the task decides by it: `{state}` **Equals** `HIGH`.

The pins are not files: give tasks that work on files a filter, or exclude the `pin://` items first.

## Waiting for a button

| Field | Value |
|---|---|
| **Pins** | `27 read up block` |
| **Wait for this many changes** | `1` |
| **Stop waiting after** | `0` |

The task holds the run until the pin changes, then hands on the pin with its new state. A job that begins with this task, runs once, and is started again by its own schedule every minute with **Single instance only** ticked, is a button that starts a job.

With a time limit, the task goes on when the time is over, with a line in the log: "Timeout waiting for pin changes."

## Commands

The task does not talk to the pins itself. It runs a small program of the Raspberry Pi's system for it.

| Field | Empty means |
|---|---|
| **Write command** | `pinctrl set {pin} op {drive}` |
| **Input setup command** | `pinctrl set {pin} ip {pull}` |
| **Read command** | `pinctrl get {pins}` |

> Empty means pinctrl, which comes with Raspberry Pi OS. Commands run directly, not through a shell.

On a system without `pinctrl`, give the commands of the tool it has. In them:

| Written | Becomes |
|---|---|
| `{pin}` | the GPIO number |
| `{pins}` | all pins to read, as `17,27` |
| `{pin_list}` | all pins to read, as separate arguments |
| `{value}` | 1 or 0 |
| `{drive}` | `dh` or `dl` |
| `{pull}` | `pu`, `pd` or `pn` |
| `{bias}` | `pull-up`, `pull-down` or `disable` |

> gpiod example: write "gpioset -c gpiochip0 -t0 {pin}={value}", input "gpioget -c gpiochip0 -b {bias} {pin}", read "gpioget -c gpiochip0 {pin_list}".

## Things to know

- **The account needs the right** to use the pins. On Raspberry Pi OS that is membership of the group `gpio`, or running as root.
- **A pin that was set stays set** after the task and after the run, until something sets it otherwise or the Pi restarts.
- **3.3 volts.** The pins of a Raspberry Pi take 3.3 V and little current. A relay or a lamp is switched through a driver board, never wired to a pin directly.
- **Jobs from CrushFTP 11** that used the Java task with a Raspberry Pi class keep working: they are run by this task. Their pin numbers are WiringPi numbers.
- **Errors**: "No pins configured.", "Invalid GPIO number", "Invalid WiringPi pin number", and "Exit code …" with what the tool printed when a command failed.
