Documentation

Toggles

Use toggles to turn devices on and off, control LEDs, and manage GPIO output pins.

Overview

Toggles are simple on/off components in MudPi. They represent binary-state devices such as LEDs, switches, and GPIO output pins. Toggles can be controlled manually through events or automatically via triggers.

Interfaces

Toggles support several hardware interfaces depending on your setup:

Interface Description
example A test interface for development and debugging
gpio Raspberry Pi GPIO pins for direct hardware control
nanpy Arduino boards connected via the Nanpy firmware

Configuration

Toggles are defined in the toggles array of your configuration file:

JSON
{
  "toggles": [
    {
      "key": "led_indicator",
      "interface": "gpio",
      "pin": 18,
      "name": "Status LED"
    }
  ]
}

Actions

Toggles support the following actions to control their state:

Action Description
toggle Flip the current state (on → off, off → on)
turn_off Set the toggle state to off
turn_on Set the toggle state to on

Events

ToggleUpdated

When a toggle's state changes, a ToggleUpdated event is emitted with the following data:

JSON
{
  "event": "ToggleUpdated",
  "component_id": "led_indicator",
  "state": 1,
  "updated_at": "2024-03-15T10:30:00Z"
}

Controlling Toggles via Events

You can control toggles by publishing events to the event system. The following event types are available:

Event Description
Switch Set the toggle to a specific state value
Toggle Flip the current state
TurnOff Force the toggle to the off state
TurnOn Force the toggle to the on state

Tip

Toggles pair well with triggers for automation. For example, turn on an LED when soil moisture drops below a threshold.