{"id":2267,"date":"2012-02-29T19:50:49","date_gmt":"2012-02-29T18:50:49","guid":{"rendered":"http:\/\/svay.com\/blog\/?p=2267"},"modified":"2012-03-01T01:00:38","modified_gmt":"2012-03-01T00:00:38","slug":"hacking-rfid-with-nodejs","status":"publish","type":"post","link":"https:\/\/svay.com\/blog\/hacking-rfid-with-nodejs\/","title":{"rendered":"Hacking RFID with NodeJS"},"content":{"rendered":"<p><em>tl;dr: you can write JS code (see below) to read RFID tags with NodeJS.<\/em><\/p>\n<p><iframe loading=\"lazy\" width=\"570\" height=\"340\" src=\"http:\/\/www.youtube.com\/embed\/XuvjUeyzwg0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Last week-end, I found my old RFID reader in a drawer. It&#8217;s a mir:ror, made by Violet, you know, the company that invented <a href=\"http:\/\/www.karotz.com\/\">Wi-Fi rabbits<\/a>. The device is a USB HID device and used to ship with a lousy app that could trigger a few predefined actions.\u00a0But the company is kind of dead now and the driver is no longer available.<\/p>\n<p>So, how hard would it be to write a driver that could do whatever I want?\u00a0The thing is that I need it to work on OSX. And it would be great if programs could be written in JS, because it&#8217;s a language I&#8217;m comfortable with.<\/p>\n<p>Being a lazy programmer, I first searched the Web for existing drivers. I found some software written in <a href=\"https:\/\/github.com\/leh\/ruby-mirror\">ruby<\/a>\u00a0and <a href=\"http:\/\/sourceforge.net\/projects\/libmirror\/\">.Net<\/a>. The first one expects the mir:ror to be available in \/dev, which is not the case on my Mac (no idea why). The second one is for Windows obviously.\u00a0Moreover, I don&#8217;t know any of these languages.<\/p>\n<p>Out of luck, I wondered: is it possible to communicate with USB HID devices in NodeJS?\u00a0Thirty seconds of googling later, I stumbled on this cool node extension: <a href=\"https:\/\/github.com\/hanshuebner\/node-hid\">Node HID<\/a>\u00a0that does exactly what I&#8217;m looking for (you could also program keyboards, keypads, mice, etc.).<\/p>\n<p>The next step was to code the driver. Hopefully, the protocol is simple and documented on this blog:\u00a0<a href=\"http:\/\/blog.nomzit.com\/2010\/01\/30\/decoding-the-mirror-comms-protocol\/\">http:\/\/blog.nomzit.com\/2010\/01\/30\/decoding-the-mirror-comms-protocol\/<\/a>.<\/p>\n<p>Below is the code of the driver. It can read RFID tags, detect when the device is upside down, and disable the annoying lights and sound.<\/p>\n<pre>var HID = require('HID');\r\n\r\nvar devices = new HID.devices(7592, 4865);\r\nvar hid;\r\nif (!devices.length) {\r\n  console.log(\"No mir:ror found\");\r\n} else {\r\n  hid = new HID.HID(devices[0].path);\r\n  hid.write([03, 01]); \/\/Disable sounds and lights\r\n  hid.read(onRead);\r\n}\r\n\r\nfunction onRead(error, data) {\r\n  var size;\r\n  var id;\r\n\r\n  \/\/get 64 bytes\r\n  if (data[0] != 0) {\r\n\r\n    console.log(\"\\n\" + data.map(function (v) {return ('00' + v.toString(16)).slice(-2)}).join(','));\r\n\r\n    switch (data[0]) {\r\n    case 1:\r\n      \/\/Orientation change\r\n      switch (data[1]) {\r\n      case 4:\r\n        console.log(\"-&gt; mir:ror up\");\r\n        break;\r\n      case 5:\r\n        console.log(\"-&gt; mir:ror down\");\r\n        break;\r\n      }\r\n      break;\r\n    case 2:\r\n      \/\/RFID\r\n      switch (data[1]) {\r\n      case 1:\r\n        console.log(\"-&gt; RFID in\");\r\n        break;\r\n      case 2:\r\n        console.log(\"-&gt; RFID out\");\r\n        break;\r\n      }\r\n\r\n      size = data[4];\r\n      id = (data.splice(0)).splice(5, size);\r\n      console.log(id.map(function (v) {return ('00' + v.toString(16)).slice(-2)}).join(','));\r\n      break;\r\n    }\r\n  }\r\n  hid.read(onRead);\r\n}<\/pre>\n<p>Now that NodeJS can react to RFID tags, we can do all sorts of things, like switching between spaces on a Mac. Check out the video to see a demo.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>tl;dr: you can write JS code (see below) to read RFID tags with NodeJS. Last week-end, I found my old RFID reader in a drawer. It&#8217;s a mir:ror, made by Violet, you know, the company that invented Wi-Fi rabbits. The device is a USB HID device and used to ship with a lousy app that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,26],"tags":[],"_links":{"self":[{"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/posts\/2267"}],"collection":[{"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/comments?post=2267"}],"version-history":[{"count":27,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/posts\/2267\/revisions"}],"predecessor-version":[{"id":2498,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/posts\/2267\/revisions\/2498"}],"wp:attachment":[{"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/media?parent=2267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/categories?post=2267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/svay.com\/blog\/wp-json\/wp\/v2\/tags?post=2267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}