Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. 'use strict'
  2. const spark = require('spark')
  3. spark.on('login', function () {
  4. // If login is successful we get and accessToken,
  5. // we'll use that to call Spark API ListDevices
  6. var devicesPr = spark.listDevices()
  7. devicesPr.then(
  8. // We get an array with devices back and we list them
  9. function (devices) {
  10. console.log('API call List Devices: ', devices)
  11. // callback to be executed by each core
  12. var callback = function (err, data) {
  13. if (err) {
  14. console.log('An error occurred while getting core attrs:', err)
  15. } else {
  16. console.log('Core attr retrieved successfully:', data)
  17. }
  18. }
  19. // The function needs to be defined in the firmware uploaded to the
  20. // the Spark core and registered to the Spark cloud, same thing we do
  21. // with variables. You pass along the name of the function and the params.
  22. spark.callFunction(devices[0].id, 'digitalwrite', 'D7:HIGH', callback)
  23. // Once you hvae a device/core instance you can use that to call functions on it.
  24. // The main difference between this and directly using the main `spark` instance is
  25. // that you no longer need to pass the id.
  26. var core = devices[0]
  27. setTimeout(function () {
  28. core.callFunction('digitalwrite', 'D7:LOW', callback)
  29. }, 5000)
  30. console.log('core: ', core)
  31. },
  32. function (err) {
  33. console.log('API call failed: ', err)
  34. }
  35. )
  36. })
  37.  
  38. // Login as usual
  39. spark.login({ username: 'email@email.com', password: 'pass' })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement