spark-io

Spark Core IO

npm install spark-io
20 downloads in the last day
190 downloads in the last week
643 downloads in the last month

Spark-io

Build Status

Spark-io is a Firmata-compatibility IO class for writing node programs that interact with Spark devices.

Getting Started

In order to use the spark-io library, you will need to load the special voodoospark firmware onto your device. We recommend you review the Getting Started over there before continuing further.

The "Hello World" of microcontroller programming:

var Spark = require("spark-io");
var board = new Spark({
  token: "{{yours}}",
  deviceId: "{{yours}}"
});

board.on("ready", function(data) {
  console.log("CONNECTED", data);

  var byte = 0;

  setInterval(function() {
    console.log("message");
    this.digitalWrite("D7", (byte ^= 1));
  }.bind(this), 500);
});

Johnny-Five IO Plugin

Spark-IO can be used as an IO Plugin for Johnny-Five:

var five = require("johnny-five");
var Spark = require("spark-io");
var board = new five.Board({
  io: new Spark({
    token: "{{yours}}",
    deviceId: "{{yours}}"
  })
});

board.on("ready", function() {
  var led = new five.Led("D7");
  led.blink();
});

API

This is copied directly from The Tinker API.

digitalWrite(pin, value)

Sets the pin to 1 or 0, which either connects it to 3.3V (the maximum voltage of the system) or to GND (ground). Pin D7 is connected to an on-board LED; if you set pin D7 to HIGH, the LED will turn on, and if you set it to LOW, it will turn off.

The parameters must be the pin (A0 to A7, D0 to D7), followed by either HIGH or LOW, separated by a comma. The return value will be 1 if the write succeeds, and -1 if it fails.

Tinker API: digitalWrite

Example:

// This will turn on the on-board LED
board.digitalWrite("D7", 1);

analogWrite(pin, value)

Sets the pin to a value between 0 and 255, where 0 is the same as LOW and 255 is the same as HIGH. This is sort of like sending a voltage between 0 and 3.3V, but since this is a digital system, it uses a mechanism called Pulse Width Modulation, or PWM. You could use analogWrite to dim an LED, as an example.

The parameters must be the pin (A0 to A7, D0 to D7), followed by an integer value from 0 to 255, separated by a comma. The return value will be 1 if the write succeeds, and -1 if it fails.

Tinker API: analogWrite

Example:

// Crank an LED to full brightness
board.analogWrite("A7", 255);

servoWrite(pin, value) This is an alias to analogWrite

digitalRead(pin, handler) Setup a continuous read handler for specific digital pin.

This will read the digital value of a pin, which can be read as either HIGH or LOW. If you were to connect the pin to 3.3V, it would read HIGH; if you connect it to GND, it would read LOW. Anywhere in between, it’ll probably read whichever one it’s closer to, but it gets dicey in the middle.

The parameters must be the pin (A0 to A7, D0 to D7). The return value will be between 0 and 4095 if the read succeeds, and -1 if it fails.

Tinker API: digitalRead

Example:

// Log all the readings for D1
board.digitalRead("D1", function(data) {
  console.log(data);
});

analogRead(pin, handler) Setup a continuous read handler for specific analog pin.

This will read the analog value of a pin, which is a value from 0 to 4095, where 0 is LOW (GND) and 4095 is HIGH (3.3V). All of the analog pins (A0 to A7) can handle this. analogRead is great for reading data from sensors.

The parameters must be the pin (A0 to A7, D0 to D7). The return value will be between 0 and 4095 if the read succeeds, and -1 if it fails.

Tinker API: analogRead

Example:

// Log all the readings for A1
board.analogRead("A1", function(data) {
  console.log(data);
});

License

See LICENSE file.

npm loves you