metacloud v1.0.0
MetaCloud JavaScript SDK
MetaCloud is a cloud service by MbientLab providing data storage, visualization, and analytics for your sensor data.
In order to run the example code in this tutorial, you will need to have an active subscription. If you need to create an account, follow the instructions on this page to begin a free 3 day trial.
Installation
The package and all dependencies are avaliable via npm
npm install metacloud
Usage
After installing all of the necessary packages, open a new JavaScript file in your favorite text editor and require the metacloud
module. The Session constructor requires several attributes that describe the device and your app:
- Firmware version
- MAC address
- Model name
- Friendly name identifying the device
- App name
- App revision
The example script will stream accelerometer data from a metawear device, so install the metawear
package and look over the README if this is the first time using the JavaScript SDK.
var Session = require('metacloud').Session;
var MetaWear = require('metawear');
MetaWear.discoverByAddress('c7:e2:8b:3c:32:42', function (device) {
device.connectAndSetUp(function (error) {
var session = Session.create(device.firmwareRevision, device.address, device.modelDescription, 'Device #1', 'demo app', '1.0.0');
});
});
Start a data stream from the device and add the received values to the Session object. Here we will stream accelerometer data thus will forward it to the addAccData method.
var ref = require('ref')
// Stream data at 100Hz
MetaWear.mbl_mw_acc_set_range(device.board, 8.0);
MetaWear.mbl_mw_acc_write_acceleration_config(device.board);
var accSignal = MetaWear.mbl_mw_acc_get_acceleration_data_signal(device.board);
MetaWear.mbl_mw_datasignal_subscribe(accSignal, ref.NULL, MetaWear.FnVoid_VoidP_DataP.toPointer(function handler(ctx, dataPtr) {
var data = dataPtr.deref();
var pt = data.parseValue();
session.addAccData(data.epoch, pt.x, pt.y, pt.z);
}));
MetaWear.mbl_mw_acc_enable_acceleration_sampling(device.board);
MetaWear.mbl_mw_acc_start(device.board);
When you are ready to sync the data, stop the notifications and call the sync method.
device.once('disconnect', function () {
// Sync to MetaCloud, replace with actual login credentials
session.sync('username', 'password', function (error, result) {
console.log(error);
process.exit(0);
});
});
// Stop the stream
MetaWear.mbl_mw_acc_stop(device.board);
MetaWear.mbl_mw_acc_disable_acceleration_sampling(device.board);
MetaWear.mbl_mw_datasignal_unsubscribe(accSignal);
MetaWear.mbl_mw_debug_disconnect(device.board);