zkiiito / node-sbrick-protocol

SBrick protocol implementation in node.js
MIT License
8 stars 0 forks source link
bluetooth bluetooth-low-energy javascript lego nodejs npm-package sbrick

david-dm

SBrick Protocol

Greenkeeper badge

Control your Lego SBrick creations from node.js!

I've written an article about this thing, read it here!

Requirements

An SBrick with Protocol 17, a device with node.js and a Bluetooth 4.x adapter, which is supported by noble.

Installation

npm install sbrick-protocol

Usage

const SBrick = require('sbrick-protocol');
var sbrick = null;

//scan for SBricks
SBrick.scanSBricks()
    .then((sbricks) => {
        if (sbricks.length > 0) {
            //connect to first SBrick
            sbrick = new SBrick(sbricks[0].uuid);
            return sbrick.connect();
        } else {
            throw new Error('no SBricks found');
        }
    })
    .then(() => {
        //start sbrick main loop
        return sbrick.start();
    })
    .then(() => {
        //subscribe to events
        sbrick.on('SBrick.voltage', (voltage) => {
            console.log('voltage', voltage);
        });

        sbrick.on('SBrick.temperature', (temperature) => {
            console.log('temperature', temperature);
        });

        //set channel 0 to full speed
        sbrick.channels[0].pwm = 255;
    })
    .catch(console.log);

See the complete interface in SBrick.js.

A fully functional, browser-based interface also available at node-sbrick-controller.

Commands not implemented yet