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

  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.

  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
    Happy coding!

Either connect the Hub to your router through ethernet or configure WiFi using the Flic app.

While connected to the Hub in the Flic app, go to Settings and make sure "Enable SDK Access" is turned on.

Now visit the online IDE. Your Hub should show up in the list of Hubs on your network. If not, you can manually input the IP address of the Hub. The IP address can be found using the FLic App, under Hub Settings, or in 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");