BluVolt is an application for 0-1 volt sensors. BluVolt application is versatile in the sense that it can function with the BluVolt bluetooth adapter as well as the 0-1 volt sensor on the gateway.
Prerequisites- Familiarity with Javascript, HTML, CSS
- Gateway or BluVolt adapter
Let's take a look at the bluvolt.js file which acts as the driver for the device. We define a variable BluVolt which is set the the return of the self induced function. In the function, we define the actual terminal object. You will see that all our drivers follow a similar format when it comes to defining device attributes and functions to interface with.
var SERVICE_UUID = 0x6224;
var VOLTAGE_UUID = 0x6225;
We first define UUIDs for our devices services and characteristics. We interact with one service in our application:
SERVICE_UUID is set to the UUID of what we call the voltage service. It is the parent of one characteristics which contains the voltage data. The length of the data is _ bytes. The UUID allow us to identify where data is coming from as well as have us right data using the devices Gatt Server. The file has the following functions defined:
function BlueVolt(bluetooth)
BluCurrent.prototype.connect
BluCurrent.prototype.disconnect
We will go over these functions individually. We can see, the functions show what we can do with the device. The most complex looking function is the connect function. In reality, it's really just a call to connect to the device followed by a series of promises used to gather all the necessary information post-connection.
var self = this;
var options = {
filters: [{services: [0x6224]}],
optionalServices: [0x6224, "0abb1530-685e-11e5-9d70-feff819cdc9f"]
};
First we define the filters to find the device and use the requestDevice call to start scanning. We set the filter to look for devices with the voltage service UUID. We also create a variable self to hold the reference of the current object as to not lose that reference in a callback function.
.then(function (device) {
self.bluetoothDevice = device;
return device.gatt.connect();
})
Once we get a result, we get the device object from the requestDevice call. We connect to the device that receive.
.then(function (server) {
return server.getPrimaryService(SERVICE_UUID)
})
We start by getting a service for the UUID given. In this case, it's the voltage service.
service.getCharacteristic(VOLTAGE_UUID)
.then(function (characteristic) {
return characteristic.startNotifications()
.then(function () {
characteristic.addEventListener('characteristicvaluechanged',
function (event) {
if (self.updateUI) {
self.updateUI(event.target.value);
}
}
);
});
})
On retrieval of the service, we then try to get the characteristic that contains the voltage data. Once we receive the voltage characteristic, we call startNotification with request the device to 'Notify' us on data changes. When we successfully enabled, we set an event listener for the 'characteristicvaluechanged' and pass it a function to handle the event. In that function, we call a updateUI which will send the data to the UI controller to display.
BluCurrent.prototype.disconnect = function disconnect() {
var self = this;
if (!self.bluetoothDevice) {
return Promise.reject();
}
return self.bluetoothDevice.disconnect()
.then(function () {
self.connected = false;
self.relayCharacteristic = undefined;
self.relayStatus = undefined;
self.bluetoothDevice = undefined;
return Promise.resolve();
});
}
The disconnect function, I assume is very straight forward. If we are not connected, we reject the call. If we are, we call disconnect on that device. The fulfillment of that call clears up any data detrimental to the app to avoid any errors and getting the app properly into a disconnected state.
Comments
Please log in or sign up to comment.