I have been trying to connect my Thunderboards to my raspberry pi. I have successfully done this with a nodejs nodual but I can't get to save to the same file sequentially.
The final goal is to connect to 3 Thunderboards of known ID sequentially, get the data from them and save it to a text file. (connect to 1 save data, connect to 2 save data, connect to 3 save data and repeat). I know the full GATT database of uuid's for the services and characteristics.
Here is my code. This finds a thuderboard and then opens a file and saves to it. However, it overwrites it every time. How can I get it to append the file every time?
var noble = require('noble');
var ThunderboardReact = require('node-thunderboard-react');
var thunder = new ThunderboardReact({'noble': noble});
var fs = require('fs');
var path = "test.txt";
var position= 0;
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) =>
{
fs.open(path, 'r+', function(err, fd)
{
if (err)
{
return console.error(err);
}
// Show the event data
console.log('- Orientation:');
console.log(' - alpha :' + res.alpha + '°');
console.log(' - beta :' + res.beta + '°');
console.log(' - gamma :' + res.gamma + '°');
fs.writeSync(fd, 'alpha: ');
fs.writeSync(fd, (res.alpha).toString());
fs.writeSync(fd, '° beta: ');
fs.writeSync(fd, (res.beta).toString());
fs.writeSync(fd, '° gamma: ');
fs.writeSync(fd, (res.gamma).toString());
fs.writeSync(fd, '° ');
fs.close(fd, function(err)
{
if (err)
{
console.log(err);
}
});
});
});
}
I have read all the relevant sections nodejs.org including all of the fs section.
I would rather do this in C but I have had no luck when searching through people.csail.mit.edu/albert/bluez-intro. All the examples and tutorials I don't think are relevant.
via Kieron
No comments:
Post a Comment