Spark Core - Variables

In this example, we're going to request a variable from the Spark Core. To do this, we'll need to load some custom firmware onto the Spark. You can do this via Spark's Built tool, or using the cylon spark upload command that comes with cylon-spark.

For more info on the cylon spark upload command, please see the cylon-spark README: https://github.com/hybridgroup/cylon-spark#upload

Either way, upload this script to your Spark Core before you get started.

To begin, let's load up Cylon:

var Cylon = require('cylon');

With that done, we can begin to setup our robot:

Cylon.robot({

Our robot has one connection, which we'll call spark. It uses the Spark adaptor, and has our Spark access token and the device id from our core:

  connections: {
    spark: {
      adaptor: 'spark',
      accessToken: '[YOUR_ACCESS_TOKEN]',
      deviceId: '[YOUR_DEVICE_ID]'
    }
  },

For our robot's work, we're going to request the value of the 'randomNumber' variable from our Spark every five seconds, and print the value to the console:

  work: function(my) {
    every((5).seconds(), function() {
      my.spark.variable("randomNumber", function(err, data) {
        if (err) {
          console.log("An error occured!", err);
        } else {
          console.log("The random number is:", data);
        }
      });
    });
  }

With our connection to the Spark and work defined, all that's left is to tell the robot to start:

}).start();