Versions Compared

Key

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

...

Expand
titleRemote LED and Button Hardware Arduino sketch
Code Block
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.
 */
#include <Gumband.h>

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

// LocalDefine variableour thatLED/Toggle keepsproperty trackwrite ofcallback
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);

    void led_toggle_callback(void)
{
  // Turn the on-board LED on or off using the toggle value
 
  if(led_toggle_val == 1prop.getValue()) {
      gumbandLedOnpsocLEDOn();
    }
    else {
      gumbandLedOffpsocLEDOff();
    }
}

void setup()
{
  // Initialize Gumband
  gumbandInit();

  // Attach the callback to executesexecute when something is written to the LED/Toggle property
  gumbandSetWriteCallback(led_toggle_prop, .setWriteCallback(led_toggle_callback);
}

void loop()
{);

  // Automatically Iftell the on-board exhibit application when the button is pressed or released
if(gumbandButtonPressed() && button_currently_pressed == false) {
    
prop.setPublishOnChange();
   // TellPrevent the exhibit application the button has been pressed
 from updating our button
  gumbandPublish(button_prop, true.setReadOnly();

  // Register button_currently_pressed = true;
  }
  else if(!gumbandButtonPressed() && button_currently_pressed == true)
  {
 properties by calling subscribe
  gumbandSubscribe();
}

void loop()
{
  // Get Tellif the exhibit application the on-board button hasis been releasedpressed
    gumbandPublish(button_prop, false.setValue(psocButtonIsPressed());

  // Call update to handle button_currently_pressed = false;incoming Gumband messages
  }gumbandUpdate();
}
Code Block
breakoutModewide
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',
    {
      useLocalMQTTBroker: true, // Enable the hardware MQTT broker
      //MQTTBrokerPort: 1883, // Port for the MQTT broker, defaults to 1883
    }
);

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

// Event when hardware connects and registers
gb.on(Sockets.HARDWARE_REGISTERED, async (payload) => {
    console.log(`Hardware with ID ${payload.id} registered.`);
});

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

// Event when hardware sends a property
gb.on(Sockets.HARDWARE_PROPERTY_RECEIVED, async (payload) => {
  //console.log(`Hardware ID ${payload.id} sent a property`);
  //console.log(`  property: "${payload.property}"`);
  //console.log(`  value: ${payload.value}`);
  
  // If we receive the Button/Press property
  if(payload.property === 'Button/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.id}/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.id}/LED/Toggle`, 0);
    }
  }
});

...