I’ve created a new library for Node.js that I thought might be helpful to people here as well.
It’s available on GitHub along with further documentation, and you can install it via npm install --save sparknode
.
The idea is that you don’t have to do much aside from providing your auth token and this library queries the Cloud API, creates a Collection of cores based on the token, and fetches and exposes all your Cloud functions and variables.
Alternatively, you can supply a token and id to the Core constructor for faster access to the core.
Either way, all the cloud-exposed functions and variables on your device become methods you can call directly.
##Example:##
var sp = require('sparknode');
var collection = new sp.Collection(myAuthToken);
collection.on('connect', function() {
//Turn on an led
collection.core1.digitalwrite('d7,HIGH');
//Brew some coffee, then email me.
collection.core2.brew('coffee', function(err, timeUntilFinished) {
setTimeout(function() {
//General awesomeness goes here.
emailMe();
sendSocketIoMessage();
addCreamer();
}, timeUntilFinished);
})
//Get a variable
collection.core2.remainingCoffeeTime(function(err, value) {
//Do something with value
})
And an example of a single core.
var randomCore = new sp.Core(myAuthToken, deviceId);
randomCore.on('connect', function() {
randomCore.turnOnLights();
});
This library should also work cross platform as it doesn’t rely on curl behind the scenes. I’m hoping it also makes it much easier for me to wire custom functions to a webapp.
I’m also tracking some of the data that comes back from the spark cloud on the core objects themselves, such as online
, though I’m not sure how useful that will end up being.
###Future:###
I have several ideas I’d like to implement such a CLI for quick access.
sparknode core2 brew "coffee"
An API for the server sent events will also be a high priority as soon as that cloud API comes out.
I’m also thinking about writing a custom firmware that lets you add many more than 4 functions, directly from the CLI or even programmatically, using string parsing on the client side. I don’t know about anyone else, but I don’t need 64 characters of input very often, so I figured they’d be more useful this way. Check out the issues tracker on GitHub to add feature requests and see some of the plans I have.