项目作者: fvdm

项目描述 :
Read and control your Spark Core with node.js :)
高级语言: JavaScript
项目地址: git://github.com/fvdm/nodejs-spark.git
创建时间: 2014-01-11T01:17:50Z
项目社区:https://github.com/fvdm/nodejs-spark

开源协议:The Unlicense

下载


sparkcloud

Read and control your Spark Core with node.js using the Particle Cloud API.

Installation

Stable: npm install sparkcloud

Develop: npm install fvdm/nodejs-spark#develop

Configuration

When you load the module into your script you must provide
the access_token from your account settings.

  1. var spark = require ('sparkcloud') ('your_access_token');

Optionally you can override the default HTTP request timeout of
10 seconds, in milliseconds:

  1. // set timeout to 30 seconds
  2. var spark = require ('sparkcloud') ('your_access_token', 30000);

Callback & errors

The last parameter of each method must be the callback function.
This is the only way to receive results as the functions themselves
don’t return anything.

  1. function myCallback (err, data) {
  2. if (!err) {
  3. console.log ('We got data: ', data);
  4. } else {
  5. console.log (err);
  6. }
  7. }

The first parameter err is null when all went fine, otherwise it
is an instanceof Error. Depending on the kind of error it may have
additional properties to further explain the problem.

Errors

message description additional
request failed The request had an error err.error
api error API returned an error err.code, err.error, err.error_description

devices ( callback )

List all cores linked to your account.

  1. spark.devices (console.log);
  1. [
  2. {
  3. id: '123456789',
  4. name: 'myCore',
  5. last_app: null,
  6. connected: true
  7. }
  8. ]

events ( onMessage, [onError], [onOpen] )

Listen for events on your entire account, included events from all your devices.

See device.events below for details.

  1. spark.events (console.log);

device ( deviceId )

Get core specific methods.

  1. var core = spark.device ('123456789');

Returns device methods below, no callback.

Connect multiple cores

  1. var coffee = spark.device ('12345');
  2. var alarm = spark.device ('67890');
  3. // Make delicious coffee and wake me up after 5 min
  4. coffee.makeCoffee ();
  5. alarm.wakeMeUp (5);

device.info ( callback )

Get basic information about a core including its functions and variables.

  1. core.info (console.log);
  1. {
  2. id: 'abc123',
  3. name: 'Spark Core',
  4. connected: true,
  5. variables:
  6. {
  7. ledState: 'int32'
  8. },
  9. functions:
  10. [
  11. 'led'
  12. ],
  13. cc3000_patch_version: '1.28',
  14. product_id: 0,
  15. last_heard: '2015-06-25T06:19:18.824Z'
  16. }

device.variable ( varName, callback )

Read a variable from the core.

  1. core.variable ('ledState', console.log);
  1. {
  2. cmd: 'VarReturn',
  3. name: 'ledState',
  4. result: 0,
  5. coreInfo:
  6. {
  7. last_app: '',
  8. last_heard: '2015-06-25T06:17:48.022Z',
  9. connected: true,
  10. last_handshake_at: '2015-06-25T06:00:24.099Z',
  11. deviceID: 'abc123'
  12. }
  13. }

device.func ( functionName, [param], callback )

Run a function on the core. Optionally include one parameter.

  1. core.func ('led', 'on', console.log);
  1. {
  2. id: 'abc123',
  3. last_app: '',
  4. connected: true,
  5. return_value: 0
  6. }

device.events ( onMessage, [onError], [onOpen] )

Listen to realtime events published from the core.

  1. function message (ev) {
  2. console.log (ev.event);
  3. console.log (ev.data);
  4. }
  5. function error (err) {
  6. console.log ('Events error:');
  7. console.log (err);
  8. }
  9. // Switch LED every 5 seconds
  10. core.events (message, error, function () {
  11. setInterval (function () {
  12. core.func ('switchLED');
  13. }, 5000);
  14. });

Output

  1. {
  2. type: 'message',
  3. event: 'LED off',
  4. data:
  5. {
  6. data: 'From 1',
  7. ttl: '10',
  8. published_at: '2015-06-25T10:39:03.521Z',
  9. coreid: 'abc123'
  10. },
  11. lastEventId: '',
  12. origin: 'https://api.particle.io'
  13. }

Hint: when you publish JSON data (string) from your device it will be decoded too.

Example

If you send the following code to you Spark core,
the javascript example should work fine.

Spark code

  1. int LED = D7;
  2. int State = 0;
  3. void setup() {
  4. pinMode (LED, OUTPUT);
  5. Spark.function ("led", switchLED);
  6. Spark.variable ("ledState", &State, INT);
  7. }
  8. int switchLED (String args) {
  9. if (args.length () >= 1) {
  10. State = args == "on" ? 0 : 1;
  11. }
  12. digitalWrite (LED, State == 1 ? LOW : HIGH);
  13. return State = State == 1 ? 0 : 1;
  14. }

Node javascript

  1. var spark = require ('sparkcloud') ('your_access_token');
  2. var core = spark.device ('123456789');
  3. // just switch on/off
  4. core.func ('led', console.log);
  5. // force on
  6. core.func ('led', 'on', console.log);

Each time you run this script the blue LED next to the big RGB LED
should switch on or off.

Enjoy!

Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Author

Franklin van de Meent

Buy me a coffee