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.

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: " + res.statusCode);
	});
});

console.log("Started");