Spark-io
Spark-io is a Firmata-compatibility IO class for writing node programs that interact with Spark devices. Spark-io was built at Bocoup
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 VoodooSpark's Getting Started before continuing.
We also recommend storing your Spark token and device ID in a dot file so they can be accessed as properties of process.env
. Create a file in your home directory called .sparkrc
that contains:
export SPARK_TOKEN="your spark token"export SPARK_DEVICE_ID="your device id"
Then add the following to your dot-rc file of choice:
source ~/.sparkrc
Ensure your host computer (where you're running your Node.js application) and the Spark are on the same local network.
Blink an Led
The "Hello World" of microcontroller programming:
var Spark = ;var board = token: processenvSPARK_TOKEN deviceId: processenvSPARK_DEVICE_ID; board;
Johnny-Five IO Plugin
Spark-IO can be used as an IO Plugin for Johnny-Five:
var five = ;var Spark = ;var board = io: token: processenvSPARK_TOKEN deviceId: processenvSPARK_DEVICE_ID ; board;
API
MODES
The
MODES
property is available as a Spark instance property:
var board = ...;boardMODES;
- INPUT: 0
- OUTPUT: 1
- ANALOG: 2
- PWM: 3
- SERVO: 4
pinMode(pin, MODE)
Set a pin's mode to any one of the MODES. PWM is supported on A0, A1, A4, A5, A6, A7, D0 and D1.
Example:
var board = ...; board;
digitalWrite(pin, value)
Sets the pin to
1
or0
, which either connects it to 3.3V (the maximum voltage of the system) or to GND (ground).
Example:
var board = ...; board;
analogWrite(pin, value)
Sets the pin to an 8-bit 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. PWM is available on D0, D1, A0, A1, A4, A5, A6 and A7.
Example:
var board = ...; board;
servoWrite(pin, value)
Sets the pin to a value between 0 and 180, where the value represents degrees of the servo horn. The value is converted to a PWM signal. PWM is available on D0, D1, A0, A1, A4, A5, A6 and A7.
Example:
var board = ...; board;
digitalRead(pin, handler) Setup a continuous read handler for specific digital pin (D0-D7).
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 a 3.3V source, it would read HIGH (1); if you connect it to GND, it would read LOW (0).
Example:
var board = ...; board;
analogRead(pin, handler) Setup a continuous read handler for specific analog pin (A0-A7). Use with all analog sensors
Example:
var board = ...; board;
License
See LICENSE file.