Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
hwlist = gb.hardware.getOnlineHardware();

Examples

Putting it all together!

SDK Button LED Example

Button presses from the hardware get sent to the SDK, and the SDK toggles the hardware LED in return.

wide
Expand
titleHardware Arduino sketch
Code Block
breakoutMode
languagearduino
/*
 * Gumband Remote Button/LED Example
 * 
 * Demonstrates how to control hardware using an Exhibit Application
 *   - sets up a property to turn the on-board LED on/off.
 *   - sets up a property that updates when on-board button is pressed/released.
 */

// Create the Gumband Properties
GumbandProp button_prop = gumbandCreate("Button", "Press", gmbnd_bool);
GumbandProp led_toggle_prop = gumbandCreate("LED", "Toggle", gmbnd_bool);

// Local variable that keeps track of the button state
static bool button_currently_pressed = false;

// Define our LED/Toggle property write callback
void led_toggle_callback(uint16_t length, void* data)
{
    // Cast our received value to our intended value (we can ignore length since we are expecting one value)
    gmbnd_bool_t toggle_val = GUMBAND_BOOL(data);

    // Turn the on-board LED on or off using the toggle value
    if(toggle_val == 1) {
      gumbandLedOn();
    }
    else {
      gumbandLedOff();
    }
}

void setup()
{
  // Attach the callback to executes when something is written to the LED/Toggle property
  gumbandSetWriteCallback(led_toggle_prop, led_toggle_callback);
}

void loop()
{
  // If the on-board button is pressed
  if(gumbandButtonPressed() && button_currently_pressed == false) {
    
    // Tell the exhibit application the button has been pressed
    gumbandPublish(button_prop, true);

    button_currently_pressed = true;
  }
  else if(!gumbandButtonPressed() && button_currently_pressed == true)
  {
    // Tell the exhibit application the button has been released
    gumbandPublish(button_prop, false);

    button_currently_pressed = false;
  }
}
Expand
titleNode SDK application
Code Block
languagejs
const { Gumband, Sockets } = require('@deeplocal/gumband-node-sdk');

const EXHIBIT_ID = '40';
const EXHIBIT_TOKEN = 'a45ne3...';

const gb = new Gumband(
    EXHIBIT_TOKEN,
    EXHIBIT_ID,
    './manifest.json',
    {
      gbttEnabled: true, // Enable the hardware MQTT broker
      //gbttPort: 1883, // Port for the MQTT broker, defaults to 1883
      //noInternetConnection: true, // SDK in offline mode
      //noInternetHardwareIds: [] // Array of offline hardware IDs
    }
);

gb.on(Sockets.READY, async () => {
    console.log('Gumband Ready!');
});

// Event when hardware comes online/connects
gb.on(Sockets.HARDWARE_ONLINE, async (payload) => {
    console.log(`Hardware with ID ${payload.hardwareId} connected.`);
});

// Event when hardware comes offline/disconnects
gb.on(Sockets.HARDWARE_OFFLINE, async (payload) => {
    console.log(`Hardware with ID ${payload.hardwareId} disconnected.`);
});

// Event when hardware sends a property
gb.on(Sockets.HARDWARE_PROPERTY_RECEIVED, async (payload) => {
  //console.log(`Hardware ID ${payload.hardwareId} sent a property`);
  //console.log(`  property: "${payload.peripheral}/${payload.property}"`);
  //console.log(`  value: ${payload.value}`);
  
  // If we receive the Button/Press property
  if(payload.peripheral === 'Button' && payload.property === 'Press') {
    // If the button is pressed
    if(payload.value == 1) {
      console.log(`Button pressed!`);
      // Set the LED/Toggle property to 1 (on)
      gb.hardware.set(`${payload.hardwareId}/LED/Toggle`, 1);
    }
    // If the button is not pressed
    else {
      console.log(`Button released!`);
      // Set the LED/Toggle property to 0 (off)
      gb.hardware.set(`${payload.hardwareId}/LED/Toggle`, 0);
    }
  }
});