Hello Everyone,
Great to be a part of this community. A big thanks to all the contributors and developers.
I built a custom module to connect Adafruit Bluetooth module nrf8001 with the MM2. I am not seeing anything on the mirror and can’t figure out the issue. Please find attached the module files:
node_helper.js
var nrfuart = require('./index.js');
module.exports = NodeHelper.create({
start: function () {
console.log('BLE helper started ...');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'TEMP') {
console.log('Notification TEMP in ' + this.name + ' received');
this.sendNotificationTest(this.anotherFunction());
}
else
{
console.log('ERRRRROR');
}
},
sendNotificationTest:function(payload){
this.sendSocketNotification('TEMPER',payload);
},
anotherFunction: function(){
nrfuart.discoverAll(function(ble_uart) {
'use strict';
ble_uart.on('disconnect', function() {});
ble_uart.connectAndSetup(function() {
var writeCount = 0;
ble_uart.readDeviceName(function(devName) {});
ble_uart.on('data', function(data) {
var x = data.toString();
var con = x.substring(0,2);
var dec = x.substring(2,4);
con = con.concat('.');
con = con.concat(dec);
console.log(con);
return con;
});
setInterval(function() {}, 3000);
});
});
}
});
index.js
/*
* Adafruit nRF8001 breakout in UART mode. This might also work for other
* nRF8001 boards but has only been tested with the Adafruit board.
*/
var NobleDevice = require('noble-device');
var events = require('events');
var util = require('util');
var NRFUART_SERVICE_UUID = '6e400001b5a3f393e0a9e50e24dcca9e';
var NRFUART_NOTIFY_CHAR = '6e400003b5a3f393e0a9e50e24dcca9e';
var NRFUART_WRITE_CHAR = '6e400002b5a3f393e0a9e50e24dcca9e';
var nrfuart = function(peripheral) {
// call nobles super constructor
NobleDevice.call(this, peripheral);
// setup or do anything else your module needs here
};
// tell Noble about the service uuid(s) your peripheral advertises (optional)
nrfuart.SCAN_UUIDS = [NRFUART_SERVICE_UUID];
util.inherits(nrfuart, events.EventEmitter);
// and/or specify method to check peripheral (optional)
nrfuart.is = function(peripheral) {
'use strict';
//return (peripheral.advertisement.localName === 'UART');
return true;
};
// inherit noble device
NobleDevice.Util.inherits(nrfuart, NobleDevice);
// Receive data using the 'data' event.
nrfuart.prototype.onData = function(data) {
'use strict';
this.emit("data", data);
};
// Send data like this
// nrfuart.write('Hello world!\r\n', function(){...});
// or this
// nrfuart.write([0x02, 0x12, 0x34, 0x03], function(){...});
nrfuart.prototype.write = function(data, done) {
'use strict';
if (typeof data === 'string') {
this.writeDataCharacteristic(NRFUART_SERVICE_UUID, NRFUART_WRITE_CHAR, new Buffer(data), done);
}
else {
this.writeDataCharacteristic(NRFUART_SERVICE_UUID, NRFUART_WRITE_CHAR, data, done);
}
};
nrfuart.prototype.connectAndSetup = function(callback) {
'use strict';
var self = this;
NobleDevice.prototype.connectAndSetup.call(self, function() {
self.notifyCharacteristic(NRFUART_SERVICE_UUID,
NRFUART_NOTIFY_CHAR, true, self.onData.bind(self), callback);
});
};
// export device
module.exports = nrfuart;
bleEchoTst.js
Module.register("bleEchoTst",{
temper: '999',
defaults: {
text: '100'
},
start: function() {
this.temper = this.config.text;
this.sendSocketNotification('TEMP',this.config);
},
socketNotificationReceived: function(notification,payload)
{
if(notification === 'TEMPER')
{
this.temper = payload;
//this.temper = '999';
this.updateDom();
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
wrapper.innerHTML = this.temper;
return wrapper;
}
});
Any help is appreciated.
Thanks