Skip to content

GPIO: Trigger, Simulate & Write

Download ezUI File

Overview

This example demonstrates how to:

  • Select a GPIO device to listen for triggers or send commands to.
  • React to incoming triggers by taking a specific Page on-air.
  • Simulate trigger events for testing.
  • Write Boolean values to GPIO outputs.

GPIOtriggerSimulateWrite

When to Use

  • Linking external GPIO inputs (e.g., physical buttons, sensors) to Studio actions.
  • Sending control signals to GPIO outputs (e.g., relays, tally lights).
  • Quickly emulating hardware signals during development or troubleshooting.

Requirements

  1. At least one GPIO device is configured in Studio → Settings → GPIO.
  2. Know how you want to address the device:
    • Use the GPIO device name, exactly as it is configured in Settings → GPIO, when you want to receive trigger events. Incoming trigger events are routed by name.
    • A numeric index (0, 1, 2, …) may be used for simulate() and write(), which are resolved on the server.
    • Index 0, or an empty value, also receives trigger events, because an event whose device name is not registered falls back to the device object stored under key 0.
  3. Understand your hardware channel numbering (which input/output corresponds to which number).

Warning

An incoming trigger event is looked up by the configured device name. When that lookup misses, the event is delivered to the device object stored under the key 0 instead. Two consequences:

  • Subscribing with an index other than 0 - for example Gpio.device(1).on('trigger', …) - never fires, because the object is stored under the key 1 and nothing ever looks it up.
  • A subscription made with Gpio.device(0) or Gpio.device() receives the triggers of every GPIO device. Check e.device if more than one device is configured.

Subscribe with the device name to receive the triggers of one specific device.

UI Elements

Device - Input Number

The GPIO device to be listened to or written to. In the example UI this is a Number input, so it addresses the device by index; replace it with a Text input holding the configured GPIO name if you need trigger events from a device other than the first one. If left empty, the first available GPIO device is used.

On Trigger:

  • Trigger - Input Number (num_GpioInput): the GPIO input channel to monitor for triggers, and the channel the Simulate button fires.
  • Page - Input Number (triggerPage): page number to take on-air when the trigger is received.
  • Simulate - Button: triggers the specified input channel programmatically for testing.

Write:

  • Input Number (num_GpioInput1): the GPIO output channel to write to.
  • Switch Control (writeValue): sets the Boolean value (True/False).
  • Write - Button (simulate1): sends the selected Boolean value to the specified output channel.

The trigger event

A trigger event carries:

1
2
3
4
5
6
{
  device: 'My GPIO',   // the device name as configured in Settings → GPIO
  deviceIndex: 0,      // its position in the GPIO settings list
  event: 'trigger',
  args: { input: 3 }   // the input channel that fired
}

The subscription is per device, not per input, so the handler runs for every input on that device. Compare e.args.input with the channel you care about, as the sample below does.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Get the GPIO device by its configured name (or index) from the "device" control
const GpioDevice = Gpio.device(device.value);

// Event handler for GPIO "trigger" events - fires when the device receives an input signal
GpioDevice.on('trigger', e => {
  console.log('Gpio on trigger', e);

  // The handler runs for every input on the device, so ignore the other channels
  if (Number(num_GpioInput.value) !== e.args.input)
    return;

  // Read the page number from the "triggerPage" control
  const raw = triggerPage.value;

  // Validate that the value is a finite number
  if (!Number.isFinite(Number(raw))) {
    console.warn('[TakeIn] Invalid page number:', raw);
    return;
  }

  // Convert to an integer (the page number is matched numerically)
  const page = String(Math.trunc(Number(raw)));

  try {
    // Execute a "Take In" command for the specified page in the active program profile
    Api.profile.program.takeIn(page);
  } catch (err) {
    console.error('[TakeIn] Api.profile.program.takeIn failed:', err);
  }
});

// Event handler for the "Simulate" button click - simulates a GPIO input trigger
simulate.onClick = () => {
  const input = Number(num_GpioInput.value); // Input channel number
  if (!Number.isFinite(input)) {
    console.warn('[GPIO] Invalid input channel for simulate');
    return;
  }

  console.log(`SIMULATE INPUT ON GPIO INPUT ${input}`);
  // Trigger a simulated signal on the specified input channel
  GpioDevice.simulate(input);
};

// Event handler for the "Write" button click - sends a value to a GPIO output channel
simulate1.onClick = () => {
  const output = Number(num_GpioInput1.value); // Output channel number
  const value  = Boolean(writeValue.value);    // Boolean value to set (true/false)
  if (!Number.isFinite(output)) {
    console.warn('[GPIO] Invalid output channel for write');
    return;
  }

  console.log(`[GPIO] WRITE to output ${output}: ${value}`);
  // Write the specified value to the selected GPIO output channel
  GpioDevice.write(output, value);
};

Info

Api.profile.program.takeIn(page) resolves the page number against the currently open playlist only, and the comparison is numeric, so '5' and '0005' are equivalent. If no page with that number is in the open playlist, nothing happens and no error is raised.

Other Gpio API calls

  • Gpio.device(name).once(event, callback) and .off(event, callback?) manage subscriptions the same way as on.
  • Gpio.simulate(device, input) and Gpio.write(device, input, value) can be called at module level, without first obtaining a device object. Both are asynchronous and throw when the device cannot be found on the server.