Skip to main content

Controlling an LED on an Arduino over HTTP


Prerequisites
  • You will need node.js and npm installed , and should have at least some knowledge when it comes to node, if you do not , I suggest you try some example code in node before diving into controlling hardware with it. 
  • You must have the Arduino IDE installed on your machine
  • You must know how to get an LED working on pin 13 of an Arduino

Set up the Arduino

Wire up an LED to the Arduino on pin 13 and plug it into your development machine. Now we need to get it ready to talk to johnny-five. To do this simply follow the instructions under johnny-five's readme

Set up the project 

Create a folder on your development machine for the project , in this folder create the server.js and index.html files. Copy the code from the files in this gist to the respective files.

Loading ....

We aren't done yet , we need johnny-five and the narf for this to work. In your project directory run the following command:

npm install narf johnny-five

If that succeeded we now have the johnny-five library and narf installed , the server should be ready to go, so lets run it:

node server.js

You should now be able to browse to the page and control the light.

http://localhost:8079/index.html


We are now in business , if you wish to control the light from another machine you will have to edit the urls in index.html and replace localhost with the IP address of the server. Other than that we are pretty much done, an LED controlled over HTTP. If you want to understand more you should just read through the code(its quite simple) and through the documentation for johnny-five. Don't worry too much about the index.html if thats not your cup of tea, it just makes http requests to the server and serves as a nice interface , the meaty bit is in server.js

An early prototype of the HTTP LED that I hacked together in five minutes , the code has been updated since then to make it slightly more solid

If you want to just get straight to the action check out my GitHub project , this is under development so there is a possibility of seeing new features added here as well.

Comments

Popular posts from this blog

Setting up Qt Creator for assembly

After fiddling with inline asssembly (not very successfully) ,I recently decided to try writing proper assembly and compiling with NASM in Linux. After writing a hello world using gedit and having a terminal open for compiling,linking and running I had a thought.,there has to be a better way to do this. So I tried Qt Creator ,because I know it's easy to add custom build and run commands,and what do you know? I got it to work. Here's how,my screenshots and assembly code are in Linux but the set up should be the same regardless of the operating system,if you are not using Linux then just use the same commands you use to assemble in your operating system. First off ,create a new console application: I named mine ASM Rename the main.cpp to main.asm and delete all the text inside.then insert some assembly: Now open up the “Project” tab and edit the build settings,remove the current build and clean steps and remove the “-build-desktop” from the en...

Using delegates in Objective-C

Why use delegation? Well when writing Objective-C programs in Xcode we are encouraged to use the MVC(Model View Controller) structure.In MVC a model cannot send messages directly to a specific controller.This can become a problem if data in your model changes independantly and you need to update this information in your view via the controller(The model must never communicate directly with the view).This is where a delegate comes in,it allows messages to be sent from the model to a controller that is not specified in the model but is rather specified by the controller,in other words the controller specifies itself. If you dont really understand what im talking about so far ,don't worry.I didn't understand delegates either until I needed one,but hopefully by the end of this tutorial you will understand not only how to use a delegate ,but the reason you would want to use one. Program breakdown For this tutorial we will write a program that has a model that will change an...

Fix ssh -Y with other Macs OSX 10.8 Mountain Lion

You may have realised that when you try use ssh -Y on another Mac from Mountain Lion you get the following error : Warning: No xauth data; using fake authentication data for X11 forwarding.X11 forwarding request failed on channel 0 Fixing this error only takes some simple configuration. All you have to do is add these lines to the following files on both machines: /etc/sshd_config X11Forwarding yes XauthLocation /opt/X11/bin/xauth XauthLocation /usr/X11R6/bin/xauth /etc/ssh_config XauthLocation /opt/X11/bin/xauth XauthLocation /usr/X11R6/bin/xauth That should be all you need to get rid of the authentication error. Hope this was helpful.