Blog

I write code, this blog is a dream.

2014.09.14Moving Arduino with Haxe/JS

I recently created a small Arduino prototype, controlled by a simple Quiz game, itself controlled by a Leap motion controller.

Leap -> Game -> Arduino

I really like coding games using HTML5 and CSS. I regularly create comissionned HTML5 games and nothing beat the web platform to iterate fast. The game had to be in Haxe (Javascript target).

Using the Leap javascript API is as simple as including the right script in the HTML and running Leap.loop().

Javascript then uses a Websocket to talk to the local Leap server, connected to the LEAP device.

As far as Arduino is concerned, there is a great javascript library out there to communicate with the board using the Firmata protocol : Johnny-five.

Using the Arduino IDE, install the Examples > Firmata > StandardFirmata sketch on your Arduino board and you will then been able to communicate with your board from Node.js using the serial connection.

Firmata is great : you control your board from your computer and receive sensor's values too. Moreover the Johnny-five library have quite a few helper classes to work with Led, Motor, gyro, joystick, lcd, etc.

Using some elbow grease you can even make it work with node-webkit (it requires a few fixes to make it work as expected).

Another really convenient option is to create a Google Chrome Application instead of a node-webkit one. The main difference is that you have access to the browser' Serial API and can switch to This version of Johnny five to communicate directly with the Arduino Board.

npm install browser-serialport
npm install garrows/johnny-five

Note: I had to use Browserify to merge dependencies.

The initial setup is easier and the iteration is faster, you can start your application from the command line.

google-chrome --load-and-launch-app=`pwd`

You can refresh the app with right click / Reload App.

And of course you have access to the regular Chrome developer tools.

To break the Google Chrome dependency and create universal web pages connected to physical stuff we could create a Firmata proxy program. People could install this program on their computer, connect something to their computer and then surf to a dedicated webpage to run a physical JS app. Very much like the LEAP does.

My little prototype won't be available on the web so it doesn't matter but it's a good thing to know that it would be easy to engineer :)

If you need more input or maybe a tutorial to do this kind of job drop me a word.

(And some useless photos just because you cannot talk about Arduino without showing yours:)

Circuit Circuit