Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit 0c35a96

Browse files
committed
2 parents 10e33a4 + 71e6bbb commit 0c35a96

28 files changed

+2419
-418
lines changed

README.md

Lines changed: 158 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,190 @@
1-
# MetaWear SDK for Javascript by MBIENTLAB
1+
# MetaWear SDK for Javascript by MBIENTLAB
22

3-
[![Platforms](https://img.shields.io/badge/platform-linux--64%20%7C%20win--32%20%7C%20osx--64%20%7C%20win--64-lightgrey?style=flat)](https://github.com/mbientlab/MetaWear-SDK-JavaScript)
4-
[![License](https://img.shields.io/cocoapods/l/MetaWear.svg?style=flat)](https://mbientlab.com/license)
5-
[![Version](https://img.shields.io/badge/node-%3E%3D%208.0.0-brightgreen?style=flat)](https://github.com/mbientlab/MetaWear-SDK-JavaScript)
3+
[![Platforms](https://img.shields.io/badge/platform-linux%20%7C%20-lightgrey?style=flat)](https://github.com/mbientlab/MetaWear-SDK-JavaScript)
4+
[![License](https://img.shields.io/cocoapods/l/MetaWear.svg?style=flat)](https://github.com/mbientlab/MetaWear-SDK-JavaScript/blob/master/LICENSE.md)
5+
[![Version](https://img.shields.io/badge/node-%3D%2010.23.0-brightgreen?style=flat)](https://github.com/mbientlab/MetaWear-SDK-JavaScript)
66

77
![alt tag](https://raw.githubusercontent.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS/master/Images/Metawear.png)
88

9-
SDK for creating MetaWear apps that run on node.js or in the browser (web-bluetooth support not widespread yet, but growing). This is a thin wrapper around the [MetaWear C++ API](https://github.com/mbientlab/Metawear-CppAPI) so you will find the C++ [documentation](https://mbientlab.com/cppdocs/latest/) and [API reference](https://mbientlab.com/docs/metawear/cpp/latest/globals.html) useful.
9+
SDK for creating MetaWear apps that run on node.js. Supported on Linux only.
1010

11-
Also, check out the JavaScript [examples](https://github.com/mbientlab/MetaWear-SDK-JavaScript/tree/master/examples).
11+
This is a thin wrapper around the [MetaWear C++ API](https://github.com/mbientlab/Metawear-CppAPI) so you will find the C++ [documentation](https://mbientlab.com/cppdocs/latest/) and [API reference](https://mbientlab.com/docs/metawear/cpp/latest/globals.html) useful.
1212

13-
Under the hood it uses [Noble](https://github.com/mbientlab/noble) for Bluetooth Low Energy communications.
13+
Also, check out the JavaScript [examples](https://github.com/mbientlab/MetaWear-SDK-JavaScript/tree/master/examples).
14+
15+
Under the hood it uses [Noble-Device](https://github.com/mbientlab/noble-device) and [Noble](https://github.com/mbientlab/noble) for Bluetooth Low Energy communications. These third party libraries have been abandoned and we are currently supporting a custom fork (with help from @abandonware).
16+
17+
> ADDITIONAL NOTES
18+
You can try to get our JS SDK running on OSX or Windows at your own risk. This requires that you get Noble to work under those OSs yourself. We do not provide examples or support for this; experts ONLY. Please see the Noble README.
1419

1520
### Overview
1621

1722
[MetaWear](https://mbientlab.com) is a complete development and production platform for wearable and connected device applications.
1823

19-
MetaWear features a number of sensors and peripherals all easily controllable over Bluetooth 4.0 Low Energy using this SDK, no firmware or hardware experience needed!
24+
MetaWear features a number of sensors and peripherals all easily controllable over Bluetooth 4.0/5.0 Low Energy using this SDK, no firmware or hardware experience needed!
2025

2126
The MetaWear hardware comes pre-loaded with a wirelessly upgradeable firmware, so it keeps getting more powerful over time.
2227

2328
### Requirements
2429
- [MetaWear board](https://mbientlab.com/store/)
25-
- A linux or Windows 10+ machine with Bluetooth 4.0
30+
- A linux machine with Bluetooth 4.0/5.0
2631

2732
### License
28-
See the [License](https://github.com/mbientlab/MetaWear-SDK-JavaScript/blob/master/LICENSE).
33+
See the [License](https://github.com/mbientlab/MetaWear-SDK-JavaScript/blob/master/LICENSE.md).
2934

3035
### Support
3136
Reach out to the [community](https://mbientlab.com/community/) if you encounter any problems, or just want to chat :)
3237

3338
## Getting Started
3439

40+
### Pre-Installation
41+
42+
#### Node and NPM
43+
You need to make sure you have node and npm installed on your machine. Here's a quick rundown but you should google-fu proper steps for your specific OS and Node version.
44+
45+
We are currently supporting Node 10.23.0. Here are steps to install Node on Linux (Ubuntu). You have 3 options:
46+
47+
##### 1. You can install Node from the repositories:
48+
```
49+
sudo apt install nodejs
50+
sudo apt install npm
51+
nodejs -v
52+
```
53+
This will install the latest Node. You may need to alias nodejs to node.
54+
55+
##### 2. You can install Node from a PPA:
56+
```
57+
cd ~
58+
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
59+
sudo bash nodesource_setup.sh
60+
sudo apt install nodejs
61+
nodejs -v
62+
```
63+
This will install node v10.23.0 (latest stable release of node 10). You can replace the 10 with 12 if you want to install node 12. You may need to alias nodejs to node.
64+
65+
##### 3. Using NVM (preferred method):
66+
```
67+
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh
68+
bash install_nvm.sh
69+
source ~/.profile
70+
nvm install 10.23.0
71+
nvm use 10.23.0
72+
node -v
73+
```
74+
Check the latest version of NVM before you install (it might be higher than v0.35.3). You can go online to learn how to use NVM to switch node versions on the fly.
75+
76+
##### Using sudo - a Warning
77+
It is important to note that because our scripts use OS level Bluetooth libraries, it may be required to use sudo (or you will get a warning and the scripts won't work). You need to decide if you are ok to use sudo or not. If you are not, follow this [guide](https://github.com/sandeepmistry/noble#running-on-linux)
78+
79+
You also need to check that the version of node you are using is as expected for sudo:
80+
```
81+
$ node -v
82+
v0.10.23
83+
$ sudo node -v
84+
v0.11.8-pre
85+
```
86+
As you can see here, the sudo node version is not the same as the current user version. Here's a [workaround](https://stackoverflow.com/questions/21215059/cant-use-nvm-from-root-or-sudo). You can google-fu more about this topic.
87+
```
88+
n=$(which node); \
89+
n=${n%/bin/node}; \
90+
chmod -R 755 $n/bin/*; \
91+
sudo cp -r $n/{bin,lib,share} /usr/local
92+
```
93+
94+
##### Using bluez, BLE Dongles, and Node
95+
At the time of this release, Node 10 is supported. Node 12 is not currently supported as `node-ffi` and some other libraries have not been updated.
96+
We have to use a custom fork of node-ffi until then (see package.json). Hopefully node-ffi will be updated to support Node 10 and Node 12 officially.
97+
98+
Bluez 5.50 works but 5.54 might not work. Here's a good [tutorial](https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation)
99+
100+
If you are not using a BLE dongle, you need to make sure your system is working and supports Bluetooth 4.0 or later (Bluetooth low energy).
101+
102+
If you are using a BLE dongle, you need to make sure it's working. You can google-fu how to use tools such as `bluetoothctl`, `hciconfig`, `btmon` and more to confirm this.
103+
104+
This may or may not work on arm64. Works on armhf and raspberryPi 3 and RaspberryPi 4.
105+
35106
### Installation
36107

37-
Before getting started, you need to setup the [prerequisites for Noble](https://github.com/mbientlab/noble#prerequisites). While you are there, familiarize yourself with the reset of the README since there a few limitiations and other gotchas spelled out.
108+
You have three options for installation:
38109

39-
Then you can simply install the NPM module:
40-
```javascript
110+
#### 1. Use NPM
111+
The Mbient JavaScript SDK relies on [Noble](https://github.com/mbientlab/noble) and [Noble-Device](https://github.com/mbientlab/noble-device) for Bluetooth Low Energy communications.
112+
113+
You need to setup the relevant [prerequisites for Noble](https://github.com/mbientlab/noble#prerequisites) and then [install Noble](https://github.com/mbientlab/noble#install). Make sure you use our versions of these libraries as the original packages have been abandoned.
114+
115+
Then you can simply install the MetaWear package lib with NPM using the command line:
116+
```
41117
npm install metawear
42118
```
119+
This step takes a long time as all the packages are installed and the MetaWear CPP library will be compiled on your machine. You may or may not need to update.
120+
```
121+
npm update metawear
122+
```
43123

44-
### Usage
124+
#### 2. Use our Repository
125+
You can install the metawear package straight from our repository by using:
126+
```
127+
npm install https://github.com/mbientlab/MetaWear-SDK-JavaScript.git
128+
```
129+
This step takes a long time as all the packages are installed and the MetaWear CPP library will be compiled on your machine.
130+
131+
#### 3. Clone our Repository
132+
We packaged everything for you already in this repository with the package.json file ready to be installed with npm.
133+
134+
Make sure that when you clone this repository, that you clone the submodule with it.
135+
```
136+
git clone --recurse-submodules https://github.com/mbientlab/MetaWear-SDK-JavaScript.git
137+
```
138+
139+
Then you can simply install all the dependencies you need by building the npm project with command line:
140+
```javascript
141+
npm install
142+
```
143+
This step takes a long time as all the packages are installed and the MetaWear CPP library will be compiled as well.
144+
145+
#### Errors and Issues
146+
If you have any issues with the npm installation, make sure you are using the correct version of node, npm, nvm (if used), bluez, and that your machine is bluetoothLE compliant. Follow the errors and correct them as indicated by the npm output log.
147+
148+
If you have any issues compiling the `MetaWear-CPP-SDK` (this is a post script that runs at the end of npm install), simply build it from source.
149+
If you cloned the repo:
150+
```
151+
cd MetaWear-SDK-Cpp/
152+
make
153+
```
154+
If you ran an npm command:
155+
```
156+
cd node_modules/
157+
cd MetaWear-SDK-Cpp/
158+
make
159+
```
45160

161+
#### Running your first Script
162+
Once the install is successful, you can run our example scripts in the example folder (see the example folder in our repository):
163+
```javascript
164+
node led.js
165+
```
166+
167+
Please note that depending on your node and npm installation, you may need to run sudo:
168+
```javascript
169+
sudo node led.js
170+
```
171+
172+
Please note that the examples in our examples folder will use the local metawear libraries (as this repository is meant for development):
173+
```javascript
174+
var MetaWear = require('../index')
175+
```
176+
This is using the local metawear code.
177+
178+
Simply change it to this:
179+
```javascript
180+
var MetaWear = require('metawear');
181+
```
182+
This would be using the metawear code in your local npm `node_modules` directory as installed with npn.
183+
184+
#### Notes
185+
You should familiarize yourself with this README and our tutorials since there a few limitiations and other gotchas spelled out, such as the maximum number of simultaneous Bluetooth connections.
186+
187+
### Usage
46188
Require the metawear package
47189

48190
```javascript
@@ -67,7 +209,7 @@ device.connectAndSetUp(function (error) { ... }
67209
At this point you can call any of the MetaWear API's, for example, you can blink the LED green
68210
```javascript
69211
var pattern = new MetaWear.LedPattern();
70-
MetaWear.mbl_mw_led_load_preset_pattern(pattern.ref(), MetaWear.LedPreset.BLINK);
212+
MetaWear.mbl_mw_led_load_p_pattern(pattern.ref(), MetaWear.LedPreset.BLINK);
71213
MetaWear.mbl_mw_led_write_pattern(device.board, pattern.ref(), MetaWear.LedColor.GREEN);
72214
MetaWear.mbl_mw_led_play(device.board);
73215
```
@@ -95,5 +237,4 @@ MetaWear.discover(function (device) {
95237
```
96238
97239
### Tutorials
98-
99-
Tutorials can be found [here](https://mbientlab.com/tutorials/).
240+
Tutorials can be found [here](https://mbientlab.com/tutorials/).

examples/acc_thresh_detect.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
var MetaWear = require('../index')//require('metawear');
2+
var ref = require('ref');
3+
// Store the log event for later download. If your program needs to terminate
4+
// before performing the log download, you will need to use mbl_mw_metawearboard_serialize
5+
// to store the device state and that pass that state as the second argument to
6+
// connectAndSetUp when you are ready to download. Use mbl_mw_logger_lookup_id
7+
// to retrieve this accelLogger object
8+
var thsLogger = null;
9+
10+
MetaWear.discoverByAddress('f9:b5:f9:81:3f:77', function(device) {
11+
console.log('Discovered');
12+
device.connectAndSetUp(async function (error) {
13+
14+
// setup accelerometer (odr 50Hz and 2Gs)
15+
console.log('Set up acc');
16+
MetaWear.mbl_mw_acc_bmi160_set_odr(device.board, 6);
17+
MetaWear.mbl_mw_acc_set_range(device.board, 1);
18+
MetaWear.mbl_mw_acc_write_acceleration_config(device.board);
19+
20+
// start to setup rms->avg->thresh->log chain
21+
console.log('Get acc signal');
22+
var acc = MetaWear.mbl_mw_acc_get_acceleration_data_signal(device.board);
23+
24+
// create RMS - root mean square of acc X,Y,Z
25+
console.log('Create RMS');
26+
let promise = new Promise((resolve, reject) => {
27+
var rms = MetaWear.mbl_mw_dataprocessor_rms_create(acc, ref.NULL, MetaWear.FnVoid_VoidP_DataProcessorP.toPointer(function onSignal(context, dataPtr) {
28+
console.log('RMS Created');
29+
resolve(dataPtr);
30+
}));
31+
});
32+
let rms = await promise;
33+
console.log(rms);
34+
35+
// setup averager - averages over 8 RMS samples @ 50Hz
36+
console.log('Create AVG');
37+
promise = new Promise((resolve, reject) => {
38+
var avg = MetaWear.mbl_mw_dataprocessor_average_create(rms, 8, ref.NULL, MetaWear.FnVoid_VoidP_DataProcessorP.toPointer(function onSignal(context, dataPtr) {
39+
console.log('AVG Created');
40+
resolve(dataPtr);
41+
}));
42+
});
43+
let avg = await promise;
44+
console.log(avg);
45+
46+
// setup event on avg - reset averager
47+
console.log('Record COMMAND');
48+
MetaWear.mbl_mw_event_record_commands(avg);
49+
console.log('First command: reset averager after it fires -> 8 data pts');
50+
MetaWear.mbl_mw_dataprocessor_average_reset(avg);
51+
console.log('End Record COMMAND');
52+
promise = new Promise((resolve, reject) => {
53+
var rec = MetaWear.mbl_mw_event_end_record(avg, ref.NULL, MetaWear.FnVoid_VoidP_EventP_Int.toPointer(function onSignal(context, dataPtr, lstatus) {
54+
console.log('COMMAND Created');
55+
resolve(lstatus);
56+
}));
57+
});
58+
let rec = await promise;
59+
console.log(rec);
60+
61+
// setup threshold detector - detect anything above 1
62+
console.log('Create THRESH');
63+
promise = new Promise((resolve, reject) => {
64+
var ths = MetaWear.mbl_mw_dataprocessor_threshold_create(avg, 1, 1.0, 0.0, ref.NULL, MetaWear.FnVoid_VoidP_DataProcessorP.toPointer(function onSignal(context, dataPtr) {
65+
console.log('THRESH Created:' + dataPtr);
66+
resolve(dataPtr);
67+
}));
68+
});
69+
let ths = await promise;
70+
console.log(ths);
71+
72+
// setup logger - log the final signal of the averaged data
73+
console.log('Create LOGGER');
74+
promise = new Promise((resolve, reject) => {
75+
var thsl = MetaWear.mbl_mw_datasignal_log(ths, ref.NULL, MetaWear.FnVoid_VoidP_DataLoggerP.toPointer(function onSignal(context, logger) {
76+
console.log('LOGGER Created: ' + logger);
77+
resolve(logger);
78+
}));
79+
});
80+
thsLogger = await promise;
81+
console.log(thsLogger);
82+
83+
// Start logging
84+
startLogging(device, function (error) {
85+
if (error) {
86+
console.log(error);
87+
process.exit(1);
88+
}
89+
console.log('wait 10s');
90+
// Stop logging after 10 seconds
91+
setTimeout(function () {
92+
console.log('done waiting');
93+
downloadLog(device, function (error) {
94+
device.once('disconnect', function (reason) {
95+
console.log('disconnect');
96+
process.exit(0);
97+
});
98+
MetaWear.mbl_mw_debug_reset(device.board);
99+
});
100+
}, 10000);
101+
});
102+
});
103+
});
104+
105+
function startLogging(device, callback) {
106+
console.log('Enable Acc and Start logging');
107+
MetaWear.mbl_mw_acc_enable_acceleration_sampling(device.board);
108+
MetaWear.mbl_mw_acc_start(device.board);
109+
MetaWear.mbl_mw_logging_start(device.board, 0);
110+
callback(null);
111+
}
112+
113+
function downloadLog(device, callback) {
114+
console.log('Disable Acc and Stop logging');
115+
// Shutdown accel and logger
116+
MetaWear.mbl_mw_acc_stop(device.board);
117+
MetaWear.mbl_mw_acc_disable_acceleration_sampling(device.board);
118+
MetaWear.mbl_mw_logging_stop(device.board);
119+
console.log('Setup Download');
120+
// Setup handerl for accel data points
121+
console.log(thsLogger);
122+
MetaWear.mbl_mw_logger_subscribe(thsLogger, ref.NULL, MetaWear.FnVoid_VoidP_DataP.toPointer(function onSignal(context, dataPtr) {
123+
var data = dataPtr.deref();
124+
var pt = data.parseValue();
125+
console.log('received_entry: ' + data.epoch + ' ' + pt);
126+
}));
127+
// Setup the handlers for events during the download
128+
var downloadHandler = new MetaWear.LogDownloadHandler();
129+
downloadHandler.received_progress_update = MetaWear.FnVoid_VoidP_UInt_UInt.toPointer(function onSignal(context, entriesLeft, totalEntries) {
130+
console.log('received_progress_update entriesLeft:' + entriesLeft + ' totalEntries:' + totalEntries);
131+
if (entriesLeft === 0) {
132+
// Remove all log entries if told to stop logging
133+
MetaWear.mbl_mw_metawearboard_tear_down(device.board);
134+
callback(null);
135+
}
136+
});
137+
downloadHandler.received_unknown_entry = MetaWear.FnVoid_VoidP_UByte_Long_UByteP_UByte.toPointer(function onSignal(context, id, epoch, data, length) {
138+
console.log('received_unknown_entry');
139+
});
140+
downloadHandler.received_unhandled_entry = MetaWear.FnVoid_VoidP_DataP.toPointer(function onSignal(context, dataPtr) {
141+
var data = dataPtr.deref();
142+
var dataPoint = data.parseValue();
143+
console.log('received_unhandled_entry: ' + dataPoint);
144+
});
145+
// Actually start the log download, this will cause all the handlers we setup to be invoked
146+
console.log('Start Download');
147+
MetaWear.mbl_mw_logging_download(device.board, 20, downloadHandler.ref());
148+
}
149+

examples/anonymous_datasignals.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
2-
/**
3-
* Created by sschiffli on 11/2/17.
4-
*/
51
var MetaWear = require('../index')//require('metawear');
62
var ref = require('ref')
73

File renamed without changes.

0 commit comments

Comments
 (0)