Flic Hub SDK Tutorial

Introduction

The Flic Hub SDK lets you connect to your Flic Hub using our online IDE to write packages that can run on the hub.

Packages are written in Javascript and are stored locally on the hub.

The connection between the online IDE and the Flic Hub happens locally over a websocket so to connect you need to be on the same LAN as the Hub and connect using its local IP address. The local IP address can be found by connecting to the hub using the Flic app and check under Settings, or by finding it through your routers admin interface.

dsa

Setup

To access the Flic Hub SDK, please follow these steps:

  1. Power the Flic Hub
    Power the Flic Hub using the USB port with a power adapter with at least 2A output.

  2. Connect the Flic Hub to your network
    Either plug in the Ethernet cable or configure WiFi using the Flic app.
    Get the Flic app: iOS Android

  3. Enable SDK Access
    Using the Flic app, connect to the Hub and make sure "Enable SDK Access" is turned on
    If you do not see this option you need to update your Hubs firmware.
    If the app tells you a firmware update is needed, use the "Update Firmware" button below.

  4. Make sure you are on the same local network
    Make sure your computer is connected to the same local network as the Flic Hub.

  5. Access the Hub using the Online IDE
    If the Flic Hub is online and on the same local network it should appear in the list.
    If the Hub does not show up, you can enter the IP address which can be found on the Hub settings view in the Flic app.
    The password is written on the label on the Flic Hub.
    Happy coding!

Packages

The code you write on the hub are divided into packages. Each package has a main.js which will be executed when the package runs.

Modules

Different hub features, such as button management, network, http etc, are accessed through modules.

You can include a module using the require(); semantics:

var buttonManager = require("buttons");

More information about available modules can be fount in the Flic Hub SDK Module Documentation.

Run Modes

You can choose to run packages with or without the "Restart after crash" flag.

This flag will ensure that the package is re-run after a crash or boot.

Command Prompt

A simple command prompt is available through the online IDE with some simple commands to manage buttons and WiFi configurations.

Examples

List all buttons and print all the information in JSON format to the console:

var buttonManager = require("buttons");
var buttons = buttonManager.getButtons();

for (var i = 0; i < buttons.length; i++) {
	var button = buttons[i];
	console.log(JSON.stringify(button));
}

 

Listen for click events and send a request with button serial number and click type to specified endpoint:

var buttonManager = require("buttons");
var http = require("http");
var url = "YOUR_URL_HERE";

buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
	var button = buttonManager.getButton(obj.bdaddr);
	var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";

	http.makeRequest({
		url: url,
		method: "POST",
		headers: {"Content-Type": "application/json"},
		content: JSON.stringify({"serial-number": button.serialNumber, "click-type": clickType}),
	}, function(err, res) {
		console.log("request status: ", err, res);
	});
});

console.log("Started");