0.0.4 • Published 8 years ago

node-thunderboard-react v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

node-thunderboard-react

The node-thunderboard-react is a Node.js module which allows you to communicate with The Thunderboard React Board Kit sold by Silicon Laboratories.

Thunderboard React Board Kit Thunderboard React Board Kit

This module exposes the APIs which allows you to access the Thunderboard React via BLE easily. This module is based on the document "UG164: ThunderboardTM React (RD-0057-0201) User's Guide" published by Silicon Laboratories.

The node-thunderboard-react provides you with the APIs as follows:

  • Discovery the Thunderboard React Board Kit(s)
  • Access the all services supported by the Thunderboard React Board Kit as follows: Device Information Service Battery Service Environmental Sensing Service Ambient Light Service Cycling Speed and Cadence Service Acceleration and Orientation Service * Automation IO Service (partially)

Dependencies

Installation

$ npm install node-thunderboard-react

Table of Contents


Operating suggestions

When you use the Thunderboard React Board Kit, don't forget turn it on. Be sure that the SW3 is set to "Vbat".

SW3

Besides, before the discovery process is about to start, be sure to press a button (ether SW0 or SW1). Once the button is pressed, the Thunderboard React Board Kit send advertising packets for 30 seconds.

SW0 adn SW1


Quick Start

This section shows how to discover Thunderboard React Board Kit(s), how to get a sensor value the device, and how to set a value to the device.

Reading the sensor data

This sample code shows how to read the Environmental Sensing data (humidity, temperature, UV index).

// Create a ThunderboardReact object
var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact();

// Initialize the ThunderboardReact object
thunder.init((error) => {
  // Discover the Thunderboard React Board Kit
  thunder.startDiscovery((device) => {
    console.log('- Found ' + device.localName);
    // Stop the discovery process
    thunder.stopDiscovery();
    // Connect to the found device
    device.connect((error) => {
      console.log('- Connected ' + device.localName);
      // Get the sensored data
      getEnvironmentalSensing(device);
    });
  });
});

// Get the sensored data
function getEnvironmentalSensing(device) {
  device.getEnvironmentalSensing((error, res) => {
    // Show the data
    console.log('- Sensored data:');
    console.log('  - Humidity    : ' + res.humidity + ' %');
    console.log('  - Temperature : ' + res.temperature + ' °C');
    console.log('  - UV Index    : ' + res.uvIndex);
    // Disconnect the device
    device.disconnect(() => {
      console.log('- Disconnected ' + device.localName);
      process.exit();
    });
  });
}

This sample code will output the result like this:

- Found Thunder React #28932
- Connected Thunder React #28932
- Sensored data:
  - Humidity    : 50.18 %
  - Temperature : 26.65 °C
  - UV Index    : 0
- Disconnected Thunder React #28932

Monitoring the sensor data

This sample code shows how to monitor the sensor data incoming from the device. In this code, the orientation of the device is monitored.

var ThunderboardReact = require('../lib/node-thunderboard-react.js');
var thunder = new ThunderboardReact();

thunder.init((error) => {
  thunder.startDiscovery((device) => {
    console.log('- Found ' + device.localName);
    thunder.stopDiscovery();
    device.connect((error) => {
      console.log('- Connected ' + device.localName);
      startMonitorOrientation(device);
    });
  });
});

// Monitor the sensored data
function startMonitorOrientation(device) {
  // Start to monitor the orientation of the device
  device.startMonitorOrientation((error) => {
    if(error) {
      console.log(error.toString());
      process.exit();
    }
  });

  // Set a listener for orientation events fired on the ThunderboardReactDevice object
  device.on('orientation', (res) => {
    // Show the event data
    console.log('- Orientation:');
    console.log('  - alpha :' + res.alpha + '°');
    console.log('  - beta  :' + res.beta + '°');
    console.log('  - gamma :' + res.gamma + '°');
  });

  // Stop to monitor and disconnect the device in 5 seconds
  setTimeout(() => {
    device.stopMonitorOrientation((error) => {
      // Disconnect the device
      device.disconnect(() => {
        console.log('- Disconnected ' + device.localName);
        process.exit();
      });
    });
  }, 5000);
}

This sample code will output the result like this:

- Found Thunder React #28932
- Connected Thunder React #28932
- Orientation:
  - alpha :3.26°
  - beta  :-0.45°
  - gamma :-26.52°
- Orientation:
  - alpha :-19.47°
  - beta  :-25.4°
  - gamma :-82.63°

...

- Disconnected Thunder React #28932

Setting data

This sample code shows how to set data to he device. This code turns on and off the two LEDs on the device one after the other every 500 milliseconds.

var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact();

thunder.init((error) => {
  thunder.startDiscovery((device) => {
    console.log('- Found ' + device.localName);
    thunder.stopDiscovery();
    device.connect((error) => {
      // Turn on/off the LEDs
      turnOnOffLeds(device, true);
    });
  });
});

// Turn on/off the LEDs
function turnOnOffLeds(device, flag) {
  // Send a message for Turning on/off the LEDs
  var status = {'led0': flag, 'led1': !flag};
  device.setLedStatus(status, (error) => {
    // Recall this method in 500 ms
    setTimeout(() => {
      turnOnOffLeds(device, !flag);
    }, 500);
  });
}

You will see the device like this:

LEDs


ThunderboardReact object

Constructor

In order to use the node-thunderboard-react, you have to load the node-thunderboard-react module as follows:

var ThunderboardReact = require('node-thunderboard-react');

You can get an ThunderboardReact constructor using the code above. Then you have to create an ThunderboardReact object as an instance of the ThunderboardReact constructor as follows:

var thunder = new ThunderboardReact();

The ThunderboardReact constructor takes an argument optionally. It must be a hash object having the properties as follows:

PropertyTypeRequiredDescription
nobleNobleoptiona Noble object of the noble module

The node-thunderboard-react module uses the noble module in order to interact with the Thunderboard React(s) on BLE. If you want to interact other BLE devices using the noble module, you can create an Noble object by yourself, then pass it to this module. If you don't specify a Noble object for the noble property, this module automatically create a Noble object internally.

The sample code below shows how to pass a Nobel object to the ThunderboardReact constructor.

// Create a Noble object
var noble = require('noble');

// Create a ThunderboardReact object
var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact({'noble': noble});

Methods

This section describes the methods implemented in the ThunderboardReact object.

init(callback)

This method initializes the ThunderboardReact object. You have to initialize the ThunderboardReact object immediately after you create it.

The initialization is processed asynchronously. Therefore you have to specify a callback function as the 1st argument of this method.

// Create a ThunderboardReact object
var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact();

// Initialize the ThunderboardReact object
thunder.init((error) => {
	if(error) { // An error was occurred
    // Do something for the error.
  } else { // The initialization process completed successfully
    // Do something using the ThunderboardReact object.
    // Generally, the discover process is started here.
  }
});

startDiscovery(callback)

This method starts the discovery process and tries to find the Thunderboard React devices. Whenever a Thunderboard React device was found, the callback function specified to the 1st argument will be called.

When a device was found, the callback will be passed a ThunderboardReactDevice object as the 1st argument. See the section "ThunderboardReactDevice object" in details.

var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact();

thunder.init((error) => {
  // Discover the Thunderboard React Board Kit
  thunder.startDiscovery((device) => {
    console.log('- Found ' + device.localName);
    // Stop the discovery process
    thunder.stopDiscovery();
		// Do something with the device.
		// Generally, the device should be connected here.
  });
});

stopDiscovery()

This method stop the discovery process. If the startDiscovery() method was called and the targeted device was found, then be sure to call this method before controlling the targeted devices.


ThunderboardReactDevice object

The ThunderboardReactDevice represents a Thunderboard React device. This object can be obtain through the discover process using the startDiscovery() method on the ThunderboardReact object.

This section describes the methods implemented in the ThunderboardReactDevice object and the events fired on the ThunderboardReactDevice object.

Methods

connect([callback])

This method tries to confirm a connection with the device which the ThunderboardReactDevice object represents. If the devcie was connected successfully, the callback specified as the 1st argument will be called. The callback is optional.

// Create a ThunderboardReact object
var ThunderboardReact = require('../lib/node-thunderboard-react.js');
var thunder = new ThunderboardReact();

// Initialize the ThunderboardReact object
thunder.init((error) => {
  // Discover the Thunderboard React Board Kit
  thunder.startDiscovery((device) => {
    console.log('- Found ' + device.localName);
    // Stop the discovery process
    thunder.stopDiscovery();
    // Connect to the found device
    device.connect((error) => {
      console.log('- Connected ' + device.localName);
      // Do something with the device
			...
    });
  });
});

After the connection has been confirmed, you can interact with the device using a lot of methods implemented on the ThunderboardReactDevice object described blow.

disconnect([callback])

This method disconnects the device. If the device was disconnected successfully, the callback will be called. The callback is optional. Once the connection is disabled, the method other than the connect method won't work any more. If you want to interact with the same device again, call the connect method again.

getManufacturerName(callback)

This method reads the Manufacturer Name from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
manufacturerNameStringBasically, the value should be "Silicon Labs".
device.getManufacturerName((error, res) => {
  console.log(res.manufacturerName); // "Silicon Labs"
});

This method reads the characteristic UUID 0x2a29 of the Device Information Service (UUID: 0x180).

getModelNumber(callback)

This method reads the Model Number from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
modelNumberStringBasically, the value should be "RD-0057".
device.getModelNumber((error, res) => {
  console.log(res.modelNumber); // "RD-0057"
});

This method reads the characteristic UUID 0x2a24 of the Device Information Service (UUID: 0x180).

getFirmwareRevision(callback)

This method reads the Firmware Revision from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
firmwareRevisionStringe.g. "1.0.0"
device.getFirmwareRevision((error, res) => {
  console.log(res.firmwareRevision); // "1.0.0"
});

This method reads the characteristic UUID 0x2a26 of the Device Information Service (UUID: 0x180).

getSystemId(callback)

This method reads the System ID from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
manufacturerIdStringManufacturer Identifier (e.g. "000b57fffe")
organizationallyUniqueIdStringOrganizationally Unique Identifier (e.g. "0c7104")
systemIdStringSystem ID, this value is the concatenation of the manufacturerId and organizationallyUniqueId (e.g. "000b57fffe0c7104")
device.getSystemId((error, res) => {
  console.log(res.manufacturerId);           // "000b57fffe"
  console.log(res.organizationallyUniqueId); // "0c7104"
  console.log(res.systemId);                 // "000b57fffe0c7104"
});

This method reads the characteristic UUID 0x2a23 of the Device Information Service (UUID: 0x180).

getDeviceInfo(callback)

This method reads the Manufacturer Name, the Model Number, the Firmware Revision, and the System ID (the Manufacturer Identifier and the Organizationally Unique Identifier) at a time. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
manufacturerNameStringBasically, the value should be "Silicon Labs".
modelNumberStringBasically, the value should be "RD-0057".
firmwareRevisionStringe.g. "1.0.0"
manufacturerIdStringManufacturer Identifier (e.g. "000b57fffe")
organizationallyUniqueIdStringOrganizationally Unique Identifier (e.g. "0c7104")
systemIdStringSystem ID, this value is the concatenation of the manufacturerId and organizationallyUniqueId (e.g. "000b57fffe0c7104")
device.getDeviceInfo((error, res) => {
  console.log(res.manufacturerName);         // "Silicon Labs"
  console.log(res.modelNumber);              // "RD-0057"
  console.log(res.firmwareRevision);         // "1.0.0"
  console.log(res.manufacturerId);           // "000b57fffe"
  console.log(res.organizationallyUniqueId); // "0c7104"
  console.log(res.systemId);                 // "000b57fffe0c7104"
});

getBatteryLevel(callback)

This method reads the Battery Level from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
levelNumberThe battery level in units of %.
device.getBatteryLevel((error, res) => {
  console.log(res.level + ' %'); // "63 %"
});

This method reads the characteristic UUID 0x2a19 of the Battery Service (UUID: 0x180f).

startMonitorBatteryLevel([callback])

This method starts to monitor the battery level notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method prepared to start monitoring successfully, the Error object will be null. The callback is optional.

Note that the callback passed to this method is not called when a notification is received. In order to get the notifications from the device, you have to set an event listener for the battery event fired on the ThunderboardReactDevice object.

device.startMonitorBatteryLevel((error) => {
  if(error) {
    console.log(error.toString());
  } else {
    console.log('Started to monitor.');
  }
});

device.on('battery', (res) => {
  console.log(res.level + ' %'); // "63 %"
});

setTimeout(() => {
  device.stopMonitorBatteryLevel(() => {
    console.log('Stopped to monitor.')
  });
}, 10000);

stopMonitorBatteryLevel([callback])

This method stops to monitor the battery level notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method stops to monitor successfully, the Error object will be null. The callback is optional.

getHumidity(callback)

This method reads the Humidity from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
humidityNumberThe humidity as an float in units of %.
device.getHumidity((error, res) => {
  console.log(res.humidity + ' %'); // "53.34 %"
});

This method reads the characteristic UUID 0x2a6f of the Environmental Sensing Service (UUID: 0x181a).

getTemperature(callback)

This method reads the Temperature from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
temperatureNumberThe temperature as an float in units of °C.
device.getTemperature((error, res) => {
  console.log(res.temperature + ' °C'); // "29.9 °C"
});

This method reads the characteristic UUID 0x2a6e of the Environmental Sensing Service (UUID: 0x181a).

getUvIndex(callback)

This method reads the UV Index from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
uvIndexNumberThe UV Index as an integer (unit-less)
device.getUvIndex((error, res) => {
  console.log(res.uvIndex); // 1
});

This method reads the characteristic UUID 0x2a76 of the Environmental Sensing Service (UUID: 0x181a).

getEnvironmentalSensing(callback)

This method reads the Humidity, the Temperature, and the UV Index at a time. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
humidityNumberThe humidity as an float in units of %.
temperatureNumberThe temperature as an float in units of °C.
uvIndexNumberThe UV Index as an integer (unit-less)
device.getEnvironmentalSensing((error, res) => {
  console.log(res.humidity + ' %');     // "53.34 %"
  console.log(res.temperature + ' °C'); // "29.9 °C"
  console.log(res.uvIndex);             // 1
});

getAmbientLight(callback)

This method reads the Ambient Light from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
luxNumberThe ambient light as an float in units of lux.
device.getAmbientLight((error, res) => {
  console.log(res.lux + ' lux'); // "123.64 lux"
});

This method reads the characteristic UUID 0xc8546913-bfd9-45eb-8dde-9f8754f4a32e of the Ambient Light Service (0xd24c4f4e-17a7-4548-852c-abf51127368b).

startMonitorCsc([callback])

This method starts to monitor the Cycling Speed and Cadence notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method prepared to start monitoring successfully, the Error object will be null. The callback is optional.

Note that the callback passed to this method is not called when a notification is received. In order to get the notifications from the device, you have to set an event listener for the csc event fired on the ThunderboardReactDevice object. The callback for the event will be called with a Response object as the 1st argument whenever a notification is received. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
revolutionsNumberCumulative Wheel Revolutions as an integer
timeNumberLast Wheel Event Time given with a resolution as an float in units of second.
device.startMonitorCsc((error) => {
  if(error) {
    console.log(error.toString());
  } else {
    console.log('Started to monitor.');
  }
});

device.on('csc', (res) => {
  console.log(res.revolutions + ' revolutions in total'); // "42 revolutions in total"
  console.log(res.time + ' sec'); // "1.3017578125 sec"
});

setTimeout(() => {
  device.stopMonitorCsc(() => {
    console.log('Stopped to monitor.')
  });
}, 10000);

This method requests the notifications to the characteristic UUID 0x2a5b of the Cycling Speed and Cadence Service (0x1816).

stopMonitorCsc([callback])

This method stops to monitor the Cycling Speed and Cadence notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method stops to monitor successfully, the Error object will be null. The callback is optional.

startMonitorAcceleration([callback])

This method starts to monitor the Acceleration notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method prepared to start monitoring successfully, the Error object will be null. The callback is optional.

Note that the callback passed to this method is not called when a notification is received. In order to get the notifications from the device, you have to set an event listener for the acceleration event fired on the ThunderboardReactDevice object. The callback for the event will be called with a Response object as the 1st argument whenever a notification is received. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
xNumberAcceleration along X-axis in units of G
yNumberAcceleration along Y-axis in units of G
zNumberAcceleration along Z-axis in units of G
device.startMonitorAcceleration((error) => {
  if(error) {
    console.log(error.toString());
  } else {
    console.log('Started to monitor.');
  }
});

device.on('acceleration', (res) => {
  console.log('X: ' + res.x + ' G'); // "X: 2.024 G",
  console.log('Y: ' + res.y + ' G'); // "Y: 0.291 G",
  console.log('Z: ' + res.z + ' G'); // "Z: 0.876 G"
});

setTimeout(() => {
  device.stopMonitorAcceleration(() => {
    console.log('Stopped to monitor.')
  });
}, 10000);

This method requests the notifications to the characteristic UUID 0xc4c1f6e2-4be5-11e5-885dfeff819cdc9f of the Acceleration and Orientation Service (UUID: 0xa4e649f4-4be5-11e5-885d-feff819cdc9f).

stopMonitorAcceleration([callback])

This method stops to monitor the Acceleration notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method stops to monitor successfully, the Error object will be null. The callback is optional.

startMonitorOrientation([callback])

This method starts to monitor the Orientation notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method prepared to start monitoring successfully, the Error object will be null. The callback is optional.

Note that the callback passed to this method is not called when a notification is received. In order to get the notifications from the device, you have to set an event listener for the orientation event fired on the ThunderboardReactDevice object. The callback for the event will be called with a Response object as the 1st argument whenever a notification is received. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
alphaNumberOrientation alpha angle as a float in units of deg (+180 to –180)
betaNumberOrientation beta angle as a float in units of deg (+90 to –90)
gammaNumberOrientation gamma angle as a float in units of deg (+180 to –180)
device.startMonitorOrientation((error) => {
  if(error) {
    console.log(error.toString());
  } else {
    console.log('Started to monitor.');
  }
});

device.on('orientation', (res) => {
  console.log('alpha: ' + res.alpha + ' °'); // "alpha: -78.65 °",
  console.log('beta : ' + res.beta  + ' °'); // "beta : -2.4 °",
  console.log('gamma: ' + res.gamma + ' °'); // "gamma: -11.93 °"
});

setTimeout(() => {
  device.stopMonitorOrientation(() => {
    console.log('Stopped to monitor.')
  });
}, 10000);

This method requests the notifications to the characteristic UUID 0xb7c4b694-bee3-45dd-ba9ff3b5e994f49a of the Acceleration and Orientation Service (UUID: 0xa4e649f4-4be5-11e5-885d-feff819cdc9f).

stopMonitorOrientation([callback])

This method stops to monitor the Orientation notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method stops to monitor successfully, the Error object will be null. The callback is optional.

getLedStatus(callback)

This method reads the LED status from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
led0NumberThis value represents the status of the LED0 (blue LED) as a integer: 0 means Inactive (OFF), 1 means Active (ON), 2 means Tristate, 3 means Unknown.
led1NumberThis value represents the status of the LED1 (green LED) as a integer: 0 means Inactive (OFF), 1 means Active (ON), 2 means Tristate, 3 means Unknown.
device.getLedStatus((error, res) => {
	var s0 = (res.led0 === 1) ? 'ON' : 'OFF';
	var s1 = (res.led1 === 1) ? 'ON' : 'OFF';
  console.log('LED0: ' + s0 + ', LED1: ' + s1); // "LED0: ON, LED1: OFF"
});

This method reads the characteristic UUID 0x2a56 of the Automation IO Service (UUID: 0x1815).

setLedStatus(status, callback)

This method set the LED status to the device. This method takes two arguments. The 1st argument is an Event object which is the same hash object as the object obtained from getLedStatus() method. When the process was completed, the callback passed to this method as the 2nd argument will be called. The Error object will be passed to the callback. If the request was sent successfuly, the Error object will be null.

var status = {
  'led0': 0, // Turn off the blue LED.
  'led1': 1  // Turn on the green LED.
};
device.setLedStatus(status, (error) => {
  if(error) {
    console.log('Failed to send a request: ' + error.toString());
  }
});

getSwitchStatus(callback)

This method does NOT work for now. It's not known exactly why.

This method reads the Button Switch status from the device. When the process was completed, the callback will be called. The two arguments will be passed to the callback: the Error object and Response object. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
sw0NumberThis value represents the status of the SW0 as a integer: 0 means Inactive (released), 1 means Active (pressed), 2 means Tristate, 3 means Unknown.
sw1NumberThis value represents the status of the SW1 as a integer: 0 means Inactive (released), 1 means Active (pressed), 2 means Tristate, 3 means Unknown.
device.getSwitchStatus((error, res) => {
  var s0 = (res.sw0 === 1) ? 'Pressed' : 'Released';
  var s1 = (res.sw1 === 1) ? 'Pressed' : 'Released';
  console.log('SW0: ' + s0 + ', SW1: ' + s1); // "SW0: Pressed, SW1: Released"
});

This method reads the characteristic UUID 0x2a56 of the Automation IO Service (UUID: 0x1815).

startMonitorSwitchStatus([callback])

This method does NOT work for now. It's not known exactly why.

This method starts to monitor the Button Switch status notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method prepared to start monitoring successfully, the Error object will be null. The callback is optional.

Note that the callback passed to this method is not called when a notification is received. In order to get the notifications from the device, you have to set an event listener for the switch event fired on the ThunderboardReactDevice object. The callback for the event will be called with a Response object as the 1st argument whenever a notification is received. The Response object is a hash object having the properties as follows:

PropertyTypeDescription
sw0NumberThis value represents the status of the SW0 as a integer: 0 means Inactive (released), 1 means Active (pressed), 2 means Tristate, 3 means Unknown.
sw1NumberThis value represents the status of the SW1 as a integer: 0 means Inactive (released), 1 means Active (pressed), 2 means Tristate, 3 means Unknown.
device.startMonitorSwitchStatus((error) => {
  if(error) {
    console.log(error.toString());
  } else {
    console.log('Started to monitor.');
  }
});

device.on('switch', (res) => {
  var s0 = (res.sw0 === 1) ? 'Pressed' : 'Released';
  var s1 = (res.sw1 === 1) ? 'Pressed' : 'Released';
  console.log('SW0: ' + s0 + ', SW1: ' + s1); // "SW0: Pressed, SW1: Released"
});

setTimeout(() => {
  device.stopMonitorSwitchStatus(() => {
    console.log('Stopped to monitor.')
  });
}, 10000);

This method requests the notifications to the characteristic UUID 0x2a56 of the Automation IO Service (UUID: 0x1815).

stopMonitorSwitchStatus([callback])

This method does NOT work for now. It's not known exactly why.

This method stops to monitor the Orientation notifications from the device. When this method finished the process, the callback will be called. A Error object will be passed to the callback as the 1st argument. If this method stops to monitor successfully, the Error object will be null. The callback is optional.

Events

This section describes the events fired on the ThunderboardReactDevice object.

connect event

The connect event is fired on the ThunderboardReactDevice object when the connection is established. You can listen to the connect event in order to know the timing when the connection is established without not specifying a callback to the connect().

device.on('connect', () => {
  // Do something with the device
});
device.connect();

disconnect event

The disconnect event is fired on the ThunderboardReactDevice object when the connection is closed whatever the reason.

The callback for the disconnect event is passed an Event object which is a hash object having the properties as follows:

PropertyTypeDescription
wasCleanBooleanThis value represents the reason why the connection was closed. If the connection was closed by the disconnect method, this value is true. Otherwise, if the connection was closed by an unexpected reason, this value is false.

A BLE connection is often disconnected unexpectedly. Therefore, it is encouraged to listen to the disconnect event and prepare a recovery action.

device.on('connect', () => {
  // Do something with the device
});

device.on('disconnect', (event) => {
	if(event.wasClean === false) { // Closed by an unexpected reason
    // Recovery action
  }
});

battery event

The battery event is fired on the ThunderboardReactDevice object whenever a Battery notification is received from the device after the startMonitorBatteryLevel() method was called. Thiis event can be listened until the stopMonitorBatteryLevel() method was called. See the section "startMonitorBatteryLevel() method" for details.

csc event

The csc event is fired on the ThunderboardReactDevice object whenever a the Cycling Speed and Cadence notification is received from the device after the startMonitorCsc() method was called. This event can be listened until the stopMonitorCsc() method was called. See the section "startMonitorCsc() method" for details.

acceleration event

The acceleration event is fired on the ThunderboardReactDevice object whenever a the Acceleration notification is received from the device after the startMonitorAcceleration() method was called. This event can be listened until the stopMonitorAcceleration() method was called. See the section "startMonitorAcceleration() method" for details.

orientation event

The orientation event is fired on the ThunderboardReactDevice object whenever a the Orientation notification is received from the device after the startMonitorOrientation() method was called. This event can be listened until the stopMonitorOrientation() method was called. See the section "startMonitorOrientation() method" for details.

switch event

This event does NOT work for now. It's not known exactly why.

The switch event is fired on the ThunderboardReactDevice object whenever a the Button Switch notification is received from the device after the startMonitorSwitchStatus() method was called. This event can be listened until the stopMonitorSwitchStatus() method was called. See the section "startMonitorSwitchStatus() method" for details.


License

The MIT License (MIT)

Copyright 2016 Futomi Hatano

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.