2. Javascript

2.1. Node.Js

This tutorial will set you up to run the examples in our Node SDK.

2.1.1. Prerequisites

To get started, get your Linux or similar environment up and running (commands may change based on your environment):

  1. Git
  2. Noble
  3. Node
  4. Dependencies

Full instructions are available on the MetaHub tutorial page to setup a Raspbian / RaspberryPi device.

2.1.2. 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);
        });
});

2.1.3. First App

Please check out the example code and README of the Javascript Github page: https://github.com/mbientlab/MetaWear-SDK-JavaScript:

>>> git clone --recursive https://github.com/mbientlab/MetaWear-SDK-JavaScript

Then install the project (this will build the CPP libraries and Node metawear module)

>>> npm install

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

>>> node led.js

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.

2.1.4. Notes

A few notes:

  1. You may have to run the examples using sudo node led.js.
  2. You may have to change the first line in the code from var MetaWear = require('../index') to require('metawear'); or vice versa depending on how you are running your project.
  3. The best way to use the metawear module is using the MAC address of your device: MetaWear.discoverByAddress('cb:7d:c5:b0:20:8f', function(device).
  4. Make sure you have ssh access to Github.

If you run into issues with npm and your build, these websites can help:

  1. https://www.bitpi.co/2015/02/11/how-to-change-raspberry-pis-swapfile-size-on-rasbian/
  2. https://docs.npmjs.com/getting-started/installing-npm-packages-globally
  3. https://docs.npmjs.com/getting-started/fixing-npm-permissions
  4. http://www.competa.com/blog/how-to-run-npm-without-sudo/
  5. https://help.github.com/articles/connecting-to-github-with-ssh/