Tuesday, February 22, 2011

Controlling Big, Mean, Devices.....With Javascript!

It amazing how far from the browser javascript has come with server side technologies like NodeJS. Now we can even control things outside of the browser or the server...How about electricity? Using an arduino it is now possible to control things with javascript outside of your computer. To do this you will need the following things:

  1. A Relay Control Board:
    http://www.sparkfun.com/tutorials/119

    http://www.instructables.com/id/Arduino-Controlled-Relay-Box/

  2. An Arduino http://arduino.cc/en/Main/ArduinoBoardUno

  3. Knowlege of javascript and git.

  4. A working install of NodeJS NPM and the Arduino software.



Yeah, I know this is alot of requirements but you gotta pay to play. I am not going to go over these things either, search the web and you can find much more info then I could type in this blog post, and plus I want to get to the code!

First thing is first. I am going to make a new folder to hold all my js code lets call it "PowerSwitch". I am going to create a directory "deps" under "PowerSwitch" cd into the "deps" directory then clone the node-arduino project. This is a project created by the guys at uxebu to control an Arduino over a serial port via NodeJS.

git clone https://github.com/tobeytailor/node-arduino.git

For whatever reason (I am not sure) I cannot get the "node-serialport" submodule to init. When I do:

git submodule init

In the node-arduino folder I get:

No submodule mapping found in .gitmodules for path 'deps/node-serialport'

No bother though we can install node-serialport from NPM which is even easier anyways. We will update the reference in code later.

npm install serialport

Now that we have the node-arduino library we have to upload the Arduino code to the arduino. Ok. Ok. I lied this is not ALL javascript but all you should need to do is upload this processing script to the Arduino one time. This script basically accepts commands over the serial port. You can view more detailed on this process here http://arduino.cc/en/Guide/HomePage. Overall here are the steps:

  1. Plug in Arduino to your computer.

  2. Start the Arduino software

  3. copy the contents the file in PowerSwitch/src/node.pde to the editor in the Arduino software

  4. Press the upload button.



You should see "Done uploading" if everything was successful. Now its time for some javascript code. First thing we will need to do is update arduino.js in PowerSwitch/deps/node-arduino/lib. Change this:

to:
. This will change the arduino library to reference the serialport library we installed via NPM instead of the library that was installed as a submodule relative the node-arduino library. Next lets create a file called server.js and put copy this code into the file.

This code depends on express. If you dont have express install it.

npm install express

Now create a new folder in PowerSwitch called public and put this index.html file there



Now run server.js

node server.js

Now hook your relay control board to your arduino. I used pin 12. Now plug in your appliance to relay controlled outlet. Then navigate to http://localhost:3000 and you should see a button click the button and the power should turn on. click it again and the power should turn off. Now I noticed a few issues with the node-arduino library. If it does not work the first time just stop and start the app a few times and you should eventually get it work. I have forked the project and I am hoping to get all the issues fixed.

Here is a video of it in action.

8 comments:

  1. Way cool! Nice video. One step above the clapper?

    ReplyDelete
  2. Couldn't you accomplish the exact same thing with off-the-shelf PHP and some AJAX? Rather than use all these obscure libraries?
    Also; you could set the code on the Arduino to let you get the current state with AJAX (GET /state for example) to set up the script, rather than assume off on each reload. (Think away-from-home.)

    ReplyDelete
  3. This is great! But... I agree with DarkStar851, It is achievable with some very simple PHP and Javascript (PHP serial class). Although, this method seems much more responsive. It seems this would have better application for real-time projects. (e.g. Robot Control)

    ReplyDelete
  4. @DarkStar851 and @Ros - yes, it could be done with PHP instead, but then the page title would have to be "Controlling Big, Mean, Devices.....With PHP!" =)

    Sometimes the less beaten path is worth exploring and, as a fan of Node.js, I appreciate seeing what can be done with it. Hell, I wrote a UUencode routine for a Z80-based computer that could never reasonably use it, just because I wanted a go.

    ReplyDelete
  5. Well technically you're not controlling it with pure JavaScript. None of this code is in the official specifications for JS, it's all custom handled functions.

    ReplyDelete
  6. Hey Guys thanks for the comments. Yes this could be done with a million other languages but I wanted to experiment with nodejs :P. And of course there are lots of improvements to be made I may work on this project some more but if not lets see what you guys can come up with!

    ReplyDelete
  7. Thanks for this tutorial!

    I'm getting an error that says Invalid "baudrate" though. Is it happening to anyone else?

    ReplyDelete
  8. DarkSuiyoken,

    I have not gotten that error before. This tutorial is actually pretty old and I would start using my new arduino javascript library seen here: https://github.com/jgautier/firmata . I may write a new tutorial using that library instead.

    ReplyDelete