Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including sbrick-protocol with all npm packages installed. Try it out:

var sbrickProtocol = require("sbrick-protocol")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sbrick-protocol v3.0.0

Control the SBrick from nodejs

david-dm

SBrick Protocol

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

  • connection parameters
  • PWM counter
  • quick drive
  • SBrick Plus measurements
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free