Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Current »

UPDATED 9/21/23

See how to set up and use the Exhibit SDK first to get familiar with using the Node SDK, as well as the hardware getting started guide for using hardware for the first time.

Overview

Setting Up the MQTT Broker

Built In MQTT Broker

The Exhibit SDK comes with a built in MQTT Broker that the hardware and SDK can use to communicate with each other. To enable the MQTT Broker during the SDK initialization:

const gb = new Gumband(
    TOKEN,
    EXHIBIT_ID,
    MANIFEST,
    {
        useLocalMQTTBroker: true,
        MQTTBrokerPort: 1885,
    },
);

This will instruct the Exhibit SDK to create an MQTT broker at port 1885 (the default port is 1883), and to connect a client to the broker on the Exhibit SDK’s behalf. In this case, the MQTT IP is not required since the SDK has a direct reference to the MQTT broker.

User Defined MQTT Broker

If you want to use your own MQTT Broker instead, you can do that by providing the MQTTBrokerIP option:

const gb = new Gumband(
    TOKEN,
    EXHIBIT_ID,
    MANIFEST,
    {
        MQTTBrokerIP: "123.123.1.12",
    },
);

This will instruct the Exhibit SDK to create a client that connects to the broker at 123.123.1.12:1883

Connecting the Hardware

An Exhibit SDK application will only accept messages from hardware that are whitelisted. There are three options to define this whitelist:

Default: Accept Messages From Any Hardware ID

By default, the whitelist will consist of only a *. This indicates that the SDK will accept and process messages from any hardware that sends them over the MQTT broker.

Only Accept Messages From Hardware IDs in the Approved Whitelist

Set a whitelist of hardware IDs when the Exhibit SDK first initializes:

const gb = new Gumband(
    TOKEN,
    EXHIBIT_ID,
    MANIFEST,
    {
        useLocalMQTTBroker: true,
        allowedHardwareIds: ['ed028e04-41d9-4e77-a5d2-ab4e7346e3a7']
    },
);

This will only allow the Exhibit SDK to accept messages from hardware with an ID of ed028e04-41d9-4e77-a5d2-ab4e7346e3a7. All other messages will still be seen by the SDK, but will be ignored.

Only Accept Messages From Hardware That Are Associated With the Same Exhibit in the Gumband UI

Set the whitelist of hardware IDs to whatever hardwares are associated with the same exhibit as the Exhibit SDK in the Gumband UI:

const gb = new Gumband(
    TOKEN,
    EXHIBIT_ID,
    MANIFEST,
    {
        useLocalMQTTBroker: true,
        allowOnlyCloudHardwareIds: true
    },
);

You can see which Exhibit SDKs and Hardware are associated with any given exhibit by navigating to the Components tab of any exhibit:

Screen Shot 2024-07-22 at 5.05.30 PM.png

If two hardwares with the same ID connect through the MQTT Broker, the registration of the first hardware will be overwritten by the second hardware. This is because the properties of a hardware are stored in the SDK based on ID.

Checking the Hardware LED Status

The hardware’s LED status will correspond to how it is connected.

Color

Description

Cyan

Only Cloud server connected

Blue

Only Application server connected

Green

Both Cloud server and Application server connected


Interacting with the SDK

See SDK Events for more information about these events and the available data in the payloads.

There are two stages that the hardware goes through when it connects to the Exhibit SDK. When the SDK receives its first registration message from the hardware, the hardware is considered “connected”. At this time, the SOCKETS.HARDWARE_CONNECTED event is emitted through the SDK class.

After all system properties and at least one application property has been registered with the Exhibit SDK, that hardware is considered “registered” and the SDK will emit a SOCKETS.HARDWARE_REGISTERED event. The “registered” event is the more significant of the two, since that event means the SDK is ready to start sending/receiving property updates to/from the hardware.

Hardware connected/disconnected event

// Event when hardware connects to the Exhibit SDK, but before it registers properties
gb.on(SOCKETS.HARDWARE_CONNECTED, async (payload) => {
    console.log(`Hardware with ID ${payload.id} connected.`);
});
// Event when hardware disconnects from the Exhibit SDK
gb.on(SOCKETS.HARDWARE_DISCONNECTED, async (payload) => {
    console.log(`Hardware with ID ${payload.id} disconnected.`);
});

Hardware registered event

// Event when hardware registers with the Exhibit SDK.
gb.on(SOCKETS.HARDWARE_FULLY_REGISTERED, async (payload) => {
    //  payload: {
    //    id: "ed028e04-41d9-4e77-a5d2-ab4e7346e3a7",
    //    system: {
    //      info: {
    //        ...systemInfo
    //      },
    //      properties: {
    //        ...systemProperties
    //      }
    //    },
    //    app: {
    //      info: {
    //        ...appInfo
    //      },
    //      properties: {
    //        ...appProperties
    //      }
    //    }
    //  }
});

Receive hardware property event

// Event when hardware system or app property is changed
gb.on(SOCKETS.HARDWARE_PROPERTY_RECEIVED, async (payload) => {
  //  {
  //      id: 'ed028e04-41d9-4e77-a5d2-ab4e7346e3a7',
  //      info: {
  //        system: { info: [Object], properties: [Array] },
  //        app: { info: [Object], properties: [Array] }
  //      },
  //      value: [ "myValue" ],
  //      property: 'My Property',
  //      source: 'app'
  //  }
});

Set hardware property

gb.hardware.setProperty(hardwareId,"my/property/path", value);

Get hardware property

value = gb.hardware.getProperty(hardwareId, "my/property/path");

Get list of currently registered hardware devices

hwlist = gb.hardware.getAllRegisteredHardware();

To see the full the full SDK API with examples, see our API Reference Docs.

Examples

SDK Button LED Example

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

The firmware for the hardware is the Remote LED and Button example in Arduino (version 1.8.4 or newer).

 Remote LED and Button Hardware Arduino sketch
/*
 * 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;
  }
}
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);
    }
  }
});

  • No labels