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!