2. Javascript

This tutorial will set you up to run the examples in our Node SDK and use Javascript to communicate with MetaSensors.

2.1. Prerequisites

You should have follow the steps on the Linux installation page.

2.2. Installation

2.2.1. Install Pip

Run the following command to install pip:

>>> sudo apt-get install python-pip

2.2.2. Install Git

Run the following command to install Git:

>>> sudo apt-get install git-core

Check that everything works:

>>> git --version

2.2.3. Install Dependencies

Run the following command to install dependencies:

>>> sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
_images/ubuntu-19.png

2.2.4. Install Node

Follow the instructions here: https://github.com/nodesource/distributions/blob/master/README.md

>>> curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
>>> sudo apt-get install -y nodejs

To compile and install native addons from npm you may also need to install build tools:

>>> sudo apt-get install -y build-essential
>>> sudo apt-get install npm
>>> sudo apt-get update
>>> sudo apt-get upgrade

Check with:

>>> node -v
>>> npm -v
_images/ubuntu-25.png

2.2.5. Download Examples

Head over to our Javascript Github page: https://github.com/mbientlab/MetaWear-SDK-JavaScript

You can clone the repository or simply download as a ZIP file:

>>> git clone --recursive https://github.com/mbientlab/MetaWear-SDK-JavaScript
_images/ubuntu-22.png

Then run install:

>>> npm install
_images/ubuntu-21.png

2.3. Run Script

You can now navigate to the examples folder and run any example using the command:

>>> node led.js

I modified the commented out section of the JS file and added the specific MAC address of my MetaMotionC.

_images/ubuntu-23.png

I also had to run as sudo because of noble:

>>> sudo node led.js
_images/ubuntu-24.png

The LED example will cause the led to blink green a few times. You can try to run the other examples in the examples directory and build on top of them.

Note, if you run into any issues, try:

>>> npm rebuild

2.4. Learn More

Play with the additional example scripts to get familiar with our JS SDK and don’t forget to refer to our developer documentation.

2.4.1. Adding MetaWear to your Node Application

Simply install the NPM module:

>>> npm install metawear

To use, simply require the metawear package:

var MetaWear = require('metawear');

Discover the first MetaWear device seen:

MetaWear.discover(function (device) { ... }

Or a specific MAC address:

MetaWear.discoverByAddress('cb:7d:c5:b0:20:8f', function(device) { ... }

There are other options too, documented in Noble Device.

After that, you must connect to the device:

device.connectAndSetUp(function (error) { ... }

At this point you can call any of the MetaWear SDK’s, for example, you can blink the LED green:

var pattern = new MetaWear.LedPattern();
MetaWear.mbl_mw_led_load_preset_pattern(pattern.ref(), MetaWear.LedPreset.BLINK);
MetaWear.mbl_mw_led_write_pattern(device.board, pattern.ref(), MetaWear.LedColor.GREEN);
MetaWear.mbl_mw_led_play(device.board);

Complete Example:

var MetaWear = require('metawear');

MetaWear.discover(function (device) {
    device.connectAndSetUp(function (error) {
            var pattern = new MetaWear.LedPattern();
            MetaWear.mbl_mw_led_load_preset_pattern(pattern.ref(), MetaWear.LedPreset.BLINK);
            MetaWear.mbl_mw_led_write_pattern(device.board, pattern.ref(), MetaWear.LedColor.GREEN);
            MetaWear.mbl_mw_led_play(device.board);
            // After 5 seconds we reset the board to clear the LED, when we receive
            // a disconnect notice we know the reset is complete, so exit the program
            setTimeout(function () {
                device.on('disconnect', function () {
                        process.exit(0);
                });
                MetaWear.mbl_mw_debug_reset(device.board);
            }, 5000);
    });
});