Last active
July 30, 2016 06:03
-
-
Save jessecogollo/68b074795372a55acd7a637d7dda7ffb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// important: particle flash 400033001047343432313031 tinker | |
// permite que se ejecute codigo js. | |
const spark = require('spark') | |
spark.on('login', function () { | |
// If login is successful we get and accessToken, | |
// we'll use that to call Spark API ListDevices | |
var devicesPr = spark.listDevices() | |
devicesPr.then( | |
// We get an array with devices back and we list them | |
function (devices) { | |
console.log('API call List Devices: ', devices) | |
// callback to be executed by each core | |
var callback = function (err, data) { | |
if (err) { | |
console.log('An error occurred while getting core attrs:', err) | |
} else { | |
console.log('Core attr retrieved successfully:', data) | |
} | |
} | |
// The function needs to be defined in the firmware uploaded to the | |
// the Spark core and registered to the Spark cloud, same thing we do | |
// with variables. You pass along the name of the function and the params. | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D1:HIGH:255:069:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D2:HIGH:255:069:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D3:HIGH:255:069:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D4:HIGH:255:069:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D5:HIGH:255:069:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D6:HIGH:000:000:255', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D7:HIGH:000:000:255', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D8:HIGH:255:000:000', callback) | |
spark.callFunction(devices[0].id, 'digitalwrite', 'D9:HIGH:255:000:000', callback) | |
// spark.callFunction(devices[0].id, 'digitalwrite', 'D10:HIGH', callback) | |
// spark.callFunction(devices[0].id, 'digitalwrite', 'D11:HIGH', callback) | |
// spark.callFunction(devices[0].id, 'digitalwrite', 'D12:HIGH', callback) | |
// Once you hvae a device/core instance you can use that to call functions on it. | |
// The main difference between this and directly using the main `spark` instance is | |
// that you no longer need to pass the id. | |
var core = devices[0] | |
setTimeout(function () { | |
core.callFunction('digitalwrite', 'A1:LOW', callback) | |
core.callFunction('digitalwrite', 'A2:LOW', callback) | |
core.callFunction('digitalwrite', 'A3:LOW', callback) | |
core.callFunction('digitalwrite', 'A4:LOW', callback) | |
core.callFunction('digitalwrite', 'A5:LOW', callback) | |
core.callFunction('digitalwrite', 'A6:LOW', callback) | |
core.callFunction('digitalwrite', 'A7:LOW', callback) | |
core.callFunction('digitalwrite', 'A8:LOW', callback) | |
core.callFunction('digitalwrite', 'A9:LOW', callback) | |
// core.callFunction('digitalwrite', 'A8:LOW', callback) | |
// core.callFunction('digitalwrite', 'A10:LOW', callback) | |
// core.callFunction('digitalwrite', 'A11:LOW', callback) | |
// core.callFunction('digitalwrite', 'A12:LOW', callback) | |
}, 5000) | |
console.log('core: ', core) | |
}, | |
function (err) { | |
console.log('API call failed: ', err) | |
} | |
) | |
}) | |
// Login as usual | |
spark.login({ username: '[email protected]', password: 'Alejandra-1987' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment