Controlling an Arduino from Node.js

Posted Friday, March 02, 2012
comments

After expressing an interested in learning microcontroller programming, a friend of mine purchased for me an Arduino Duemilanove, which arrived yesterday. Being someone who enjoys web programming, I of course wasted no time in figuring out how I could get it connected to a web page. I opted to use Node.js for my integration.

To communicate with the microcontroller, I used node-serialport, which allows you to make a connection to a serial port for reading and writing. For this test, I wrote a sketch to turn the onboard LED on pin 13 off when a 0 is read from the serial connection, and to turn the LED on when a 1 is read. The contents of this sketch follows; just upload it to your Arduino.

Next up, we need to send a 0 or 1 byte to the Arduino from Node; here’s a short CoffeeScript program that will blink the LED every second (I’ve hardcoded the device where my Arduino lives; substitute your own).

From here, it’s not difficult to adapt this example into a more complete sample including a web server. Here’s a complete listing of my program, including a web page to access at the root URL to control the LED using jQuery Ajax requests.

Start the server and visit http://localhost:8080 and click the buttons to control the lights!


Comments