New feature: SparkJS! Blink an LED with Javascript

Hi, cool people! Christine here, Office Manager at Spark. We’ve released SparkJS, our official Javascript library!

Here’s the related blog post:

If you don’t want to head over to the blog, though, here’s all the info below!

As an intro to SparkJS, I decided to go with the “Hello World” equivalent of the Spark universe: blinking an LED.

I wired my LED to the D0 pin like so:

The next thing for me to do (as always) was to connect my Spark Core to my Wi-Fi network. I connected with the Spark CLI. The basics of this are illustrated in the gif below (c/o Zach).

If you have trouble with this step, it’s good to go here for help.

Next, the Javascript. SparkJS can run in the browser or on a server using Node.js; I’ve chosen the latter. I can install SparkJS with a simple:

npm install spark

Next, I wrote a script to blink the LED in javascript, based off of the callFunction() example in the SparkJS repository. In order to program using SparkJS, make sure you have the spark module installed through node.

NOTE: This application assumes that you’re running the built-in Tinker firmware that is pre-programmed on the Spark Core. If you’re not, you can reinstall it with the Spark CLI by typing spark flash {core_id} tinker.

Having installed all the things I needed, I wrote an LED-blinking script, available here.

This may be a little bit overkill in terms of asynchrony handling, but I just wanted to demonstrate that SparkJS lets you use callbacks, promises, events, or any combination thereof.

I also employed callFunction() in two different ways: spark.callFunction(core_id, command, input, callback) before the device info is retrieved and core.callFunction(command, input, callback) after. As you can see, either works – it just depends on whether you want to use it before or after you retrieve the devices.

This is fun and everything, but to really take advantage of SparkJS, I’d love to do something on the web with an HTTP request. I never check the weather, but I wanted to be reminded to take my umbrella in the case of rain. I decided to have my Core check it for me.

Using the Open Weather Map API and the node request module, I wrote a quick script to check for rain, which I can call once a day using cron. (Weather codes are here in case you want to write your own script to identify clouds or clear skies!)

A gist of the weather code is here!

Documentation on SparkJS is available here:

http://docs.spark.io/javascript/

Let us know what you think and share all your lovely projects on the Spark Community. Have fun!

6 Likes

Hi Zach !

I am doing some experiments with SparkJs and all the things worked except two things

a) On-event
b) Publish-event.

Every time I want to call my published event it says “the permission is denied”. The login function works well but it does not call the published event.

Any idea why ?? Please let me know.

Publishing into the Cloud from the API (and not from a device) is still in private beta, so it’s only been unlocked for a few accounts; once we’re ready to make it available to everyone, we’ll publicize it. Thanks!

Hi Christine, trying SparkJS to blink an LED but no luck

I cut and paste the first code sample from http://blog.particle.io/2014/09/29/blink-an-led-with-javascript/ into a new JS file and then run node . I get the API response with all the attributes, but then it ends with

An error occurred while getting core attrs: [Error: Function digitalwrite not found]
An error occurred while getting core attrs: [Error: Function digitalwrite not found]

What other dependencies do I need other than (npm install spark)

-abhi

You’d need to make sure that the proper firmware is running on your device, in this case, that’s probably the default ‘Tinker’. Give that a try and report back :smile:

2 Likes