I wanted to outline a pretty fun hack I did using On{X}, NFC tags, Philips Hue, and Ninja Blocks. The goal is to be able to control your light bulbs using NFC tags.
Why?
I think it'd be pretty useful to turn off all the lights in your house when you put your phone down on your bedside table at night. I can imagine a world where NFC tags augment light switches, as they allow you to do far more sophisticated logic on what lights turn on (or anything plugged into a power socket), and when. So I built this little proof-of-concept I wanted to share.
How?
You will need (and I've linked to what I have):
- A light that the Ninja platform works with. Right now this is Philips Hue/LimitlessLED. You can also use the RGB LEDs on a Ninja block, or an RGB LED with an ethernet enabled Arduino. Other IP lights will probably need a module to be written for it.
- An Android device that supports NFC.
- A few writeable NFC tags. Get ones that you can stick down to something.
- an On{X} account
- the Ninja client code, checked out to our develop branch.
Lets begin!
Step 1 - be running the Ninja client code
There are instructions on how to do that here.
Once your client code is running develop, it should look something like this:

What your console should look like.
Step 2 - Setup your lights.
I'll outline the Hue setup process below - LimitlessLEDs, and the RGB LEDs mentioned before, should just appear on your dashboard and you can skip this step.
Once the client code is running, go and press the 'link button' on the top of your Hue base station. Once you do, go to https://a.ninja.is/home - it should look something like this:

Your Ninja dashboard, approximately.
Find the light bulb you want to actuate, click the 'cog' and then 'info'. You can get all the required info for the next step in that modal.
Step 3 - Setup your On{x} rule
Here is a link straight to the page you need. Click create and paste the following:
// You MUST change the following:
var ACCESS_TOKEN='API_ACCESS_TOKEN'; // Obtained from https://a.ninja.is/hacking
var NODE = "F9C342600F237BBE"; // In 'block' in the info modal
var G = '01'; // This is 'port' in the info modal
var V = 0; // This is 'VID'
var D = 1007; // This is 'DID'
// You CAN change the rest, if you want:
// You can use the local IP of the block if you know it (use http in that case).
var url = 'https://api.ninja.is';
var port = 443; // Should be 8000 if talking locally
//Register for NFC event to handle discovered tag.
device.nfc.on("found", handleNfcTag);
function handleNfcTag(tag) {
var firstRecord = tag.messages[0].records[0];
changeColour(firstRecord.text);
}
function changeColour(colour) {
var guid = [NODE,G,V,D].join('_');
var payload = JSON.stringify({ DA:colour });
var opts = {
url: url+':'+port+'/rest/v0/device/'+guid+'?user_access_token='+ACCESS_TOKEN,
type: 'POST',
data: payload,
headers: {
'Content-Type': 'application/json'
}
};
console.log(opts.url);
console.log(opts.data);
console.log("Attempting to change to ",colour)
device.ajax(opts,
function onSuccess(body, textStatus, response) {
var parsedBody;
if(!(body && (parsedBody = JSON.parse(body)))) {
var error = {};
error.message = 'invalid body format';
error.content = body;
console.error('error: ',error);
}
console.info('successfully received http response!');
notification.content = 'successfully received http response!';
notification.show();
},
function onError(textStatus, response) {
var error = {};
error.message = textStatus;
error.statusCode = response.status;
console.error('error: ',error);
});
}
You will need to edit the 'var's at the top with relevant information. Then, click 'save and send to phone', and then fire up because you're about to write some NFC tags.
Step 4 - Write your NFC tags
I used NXP TagWriter. If you are using this app, you want to go "Create and write" > New > "Plain text' > and replace "Please enter text" with the HEX code of the colour you want to actuate the light to without the leading #. Here are 500 colour names with their hex representation. White is just ffffff and off is 000000.
Step 5 - It puts the device on the tag...

What yours might look like.
And with some luck, success!
Some afterthoughts:
- This rule needs to be changed if you wish to actuate a group of lights. I think long term this concept should be baked into a 'lights app', ideally written on the Ninja platform. :)
- I did want to create this as an On{X} recipe, but figured that since everything else is still very beta keeping the obstacle of editing a bit of javascript was probably a good idea.
- Its kinda difficult to show a picture of this. Imagine the phone that took the picture on the right is sitting on top of one of those tags.
And that's it! I hope you had a bit of fun, and I'm sure there will be some edits as I think of other stuff. I'd love to know what else you guys come up with with this hack.
Dan
