Hello Ninja - writing your first Ninja app

The Ninja Platform aims to make it trivial to write applications that inteact with devices. This guide shows you how to write a "Hello World" app in Node.js.



$ mkdir helloNinja
$ cd helloNinja
$ npm install ninja-blocks  # install the ninja blocks node library
  • NPM is node's package manager. Dependancies declared in package.json are installed in a directory called node_modules.
  • ninja-blocks is the Ninja Blocks library

Instantiate Ninja

In your favorite editor (we like Sublime around here) create a file called index.js with the snippet below. This includes our libs and instantiates a ninja object with your access token.

At this point we can run our app

$ node index.js

If you have pasted your token correctly it will just exit cleanly. Now it's a bit of a pain to save your app, switch windows, kill your app, press up and restart, so I recommend you install a handy node utility called nodemon. This simply runs your app as before, but when changes are detected it kills it and restarts it.

$ npm -g install nodemon    # -g installs it globally not in this project
$ nodemon index.js          # run with nodemon
7 Feb 11:24:29 - [nodemon] v0.7.2 7 Feb 11:24:29 - [nodemon] watching: /Users/pete/tmp/helloNinja2 7 Feb 11:24:29 - [nodemon] starting `node index.js` 7 Feb 11:24:29 - [nodemon] clean exit - waiting for changes before restart

Now that you are running with nodemon position your editor and terminal next to each other. As you make changes simply save and you will see the result.

Get devices

The simplest thing you can possibly do is get all your devices, add the following lines to index.js:

When you save you should see a big tree of json in the terminal. Woot! To the non-js people, note the pattern here, ninja.devices() fetches the devices from the API and passes the result to the anonymous callback function. In javascript land the only way to make ensure something is sycronous is to put it in a callback - for a lot things this is awsome but until you get used to it, it can be confusing.

Access your sensor data

You might have noticed that devices were returned as hash of objects keyed by GUID. A quick and dirty way to get the data we want is iterate through this object. (If Object.keys... seems obtuse, you might want to checkout the very hand underscore.js library)

Now we know what the temperature is for all our temperature sensors, just what we always wanted.

Actuating stuff

In Sydney, we are soft. I admit it, you would be too if you lived here. We either think it is too hot or too cold, if we wanted Nina's Eyes to indicate what we should be complaining about, its easy. Green for 22-25 for goldilocks zone and Red & Blue for too hot and cold.

This is not a great way to implement this, but then again I should just HTFU. Next steps would be to get the temperature reading as a callback and control a heater and/or fan to keep me in the golidlocks zone.

What's next

Check out the Ninja library source for more stuff built into the node library, there are also other libraries too. Keep in mind you can always fall back to the REST API, the libraries are just wrappers. In a later episode we will walk you through subscribing to and acting on a sensors data and deploying to heroku. Future topics will include:

  • subscribing to realtime sensor data,
  • creating an app that anyone can use,
  • creating devices,
  • working with browser-ninja-blocks, 
  • whatever you want, suggestions?

Directors Cut

Pete Moore
Pete Moore