- A Relay Control Board:
- An Arduino http://arduino.cc/en/Main/ArduinoBoardUno
- 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
- Plug in Arduino to your computer.
- Start the Arduino software
- copy the contents the file in PowerSwitch/src/node.pde to the editor in the Arduino software
- Press the upload button.
. 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
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.