We keep hearing about the Internet of Things. The basic idea is simple: Everyday objects (like a watch or even a toothbrush) will soon be connected to Internet, and will be able to communicate with each other.
The last hack day at marmelab allowed me to learn more about this subject, using the range of objects brought by Ninja blocks.
Time for me to suit up my outfit, take my shurikens and transform into a fast & furtive Ninja. Thanks to Ninja Blocks and their multiple captors/actuators & NodeJS.
You can ensure the safety of your defense if you only hold positions that cannot be attacked (Sun Tzu - The Art of War)
A great Ninja is a ninja who cannot be detected, so when he’s working he shouldn’t be surprised by his sensei opening the door of his office. He must protect his workstation even if he’s going to the restroom.
To be able to detect theses actions, we are going to use 2 captors (proximity & movement). They are packaged in the Ninja Blocks kit. The first one is going to detect opening doors while the second will determine if the sensei is entering or if we are leaving the office.
Let’s imagine the a Ninja isn’t on a website related to his job. In this - very rare - case, a new tab will be opened on his favorite browser (with a more professional website, like github.com) if the sensei is coming into his office.
To prevent our sensei from discovering the page of our favorite social network, the current session will be locked when we will leave the room.
Finally, a snapshot & facial recognition functionality will be added to ensure that no other Ninja can sit at our place without our permission.
Ninja Blocks offers a great interface to manage rules, install applications created by our fellows Ninja or handle access to the API.
A dashboard will show us the state of our sensors connected to the Block: a graph of temperature & humidity evolution, buttons to change the colors of the Blocks eyes, etc.).
A rule is created in 3 simple steps:
All of theses actions can be natively used with services like Facebook, Dropbox, or a free sending SMS service.
The proximity sensor triggers an action when both parts are separated (at least 2 centimeters). In our installation, this sensor is fixed to the door (for the first part), and to the door frame (for the other one). Each time the door opens, an action is triggered.
The movement sensor is placed in the room pointing to the door. It is used to know if a sensei is coming or if we are leaving the room.
Ninja Blocks contains a “web hook” system. In the interface, we can configure rules related to events.
Here we choose to call some URLs:
/detectwhen the movement sensor is triggered
/door-openwhen someone moves in front of the movement sensor.
Node.js will gather all of theses events (with the
express module) in a “server” application:
The goal is to apply theses conditions:
Observation is a basic principle in the Ninja philosophy.
Then we are going to create 3 statuses in our “server” application:
When the door opens, the
doorOpened status is set to 1. Then, a timeout starts to change the
exitDetected status within 5 seconds.
When a movement is detected, the timeout is cleared ; and if the
doorOpened status was set to 1, the
senseiDetected status changes to 1.
dispatchInfos() method sends theses 3 statuses to the computer to protect.
During the hack day, my computer wasn’t able to receive data from the Internet (despite my Ninja talents, I couldn’t get access to the Wi-Fi router).
So the application server is hosted on an external service (like an Amazon EC2 instance). The Ninja’s computer is able to connect to this server via socket.io et receive the last 3 statuses:
When the distant server sends the status (via
dispatchInfos()), it's directly processed with the socket connection. So there is no need to create a polling system by requesting an URL each second.
exit status changes to 1 ("Ninja is leaving"), we launch the command to lock his session (on OSX):
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
When the presence of the sensei is detected, a github.com page if opened in a new tab of Chrome (Ninja’s favorite browser):
open -a Google\ Chrome "http://github.com"
Theses commands are specific to the OSX environment, a simple search allows to find alternative commands running on another environment, like on gnome:
To avoid bad tricks of your teammate like duck tape under our mouse or glue on the keyboard, we are going to setup a facial recognition system of the person in front of our computer.
When the alarm is armed and another Ninja sits on our chair (e.g. when the movement sensor placed next to the computer is triggered), a picture is taken with the webcam of the computer.
This picture is resized and uploaded to our server application so it can be analyzed via the Skybiometry webservice to retrieve a matching ratio (from 0 to 100).
If this ratio is under 50, a SMS will be sent to prevent the intrusion.
The Ninja Blocks remote is used to arm & disarm the alarm..
The first thing to do is to create a rule triggered by the remote which calls the
/button URL on our server.
2 additional rules are also set up to change the color of our Ninja Block's eyes, in order to show if the alarm is armed or not. Theses rules will be launched with a web hook on the
Now we can change the method called when a movement is detected to change the intrusion status:
Imagesnap is a command line application used to take picture with the webcam of a *nix computer. This application doesn't include drivers, so you should have the correct drivers for your hardware already installed.
Depending your webcam quality, the picture can weight few Mb, too heavy for a Ninja - especially when it should be uploaded.
In this case, the picture is resized by ImageMagick.
Installing ImageMagic (OSX):
sudo port install ImageMagick
Adding the node module in
Setting up a facial recognition system during the second part of the hack day looked ambitious. A Ninja is quite productive but he can’t stop the time.
Another rule of the Ninja philosophy is to hide and seek a better opportunity to reach the victory. So I started to search an API allowing to recognize a person on a picture and I found Skybiometry.
This service brings a free API in the limit of 100 calls per hour and 5000 per day, which is reasonable for our usage.
After sign up, we should create a namespace in the administration interface of Skybiometry. This namespace is called "WorkmateProtection".
Then we need a “tag” to recognize us. A tag allows identifying a person on a picture. So we have to send a "reference" picture of us via the webservice:
This service returns a temporary tag like
We can now associate this temporary tag to a name, in the previously created namespace "[email protected]":
http://api.skybiometry.com/fc/tags/save.json?api_key=xxx&api_secret=xxx&[email protected]&[email protected]
We can now use this service to retrieve a matching ratio (from 0 to 100) with the reference picture.
When the intrusion status is sent, the application can take a picture with the webcam:
takeSnapShotAndCompareTo(tag, done) method takes the path of a picture taken with the webcam.
Then, this picture is resized and uploaded to the server (using scp). Skybiometry uses only hosted pictures, no raw data.
When the uploading is done, we are going to call the webservice to retrieve the matching ratio. If this ratio is under 50, a SMS is sent to prevent the intrusion. This is done thanks to a new rule with a webhook as a trigger and a SMS sending for the action:
We have seen during this article that being a Ninja is not a small job. Thanks to the range of sensors brought by Ninja Blocks, we’ve got the ninjustu to do quick application for a more beautiful life.
Rule creation and webhooks allow to plug in every device to Ninja Blocks objects.
The application presented in this post can be enhanced by:
It's difficult to speak about all the features in an article; there are a lot of possibility with the API / applications / custom blocks. Theses other features will be discussed in another post later.
Now you can take your own Hattori Hanzō saber and start hacking this Block.