Control Arduino remotely with HTML forms

[for everyone who is continuing to read this, it probably won't work anymore and it isn't too reliable in general. I would suggest a better method like this : http://blog.datasingularity.com/?p=148. It works for botnets right? ]

Well, this is the culmination of a lot of posts I have been writing. I devised a simple way to get HTML form data, or URL encoded CGI data, to the Arduino using a remote server. Here is the package to download. Here is a flow chart of how the system works:


CGI_Arduino chart
Let’s look at this from the top down. The “user input” in the demonstration is an HTML form with “radio” buttons. Take a look at the source for cgi_socket_script.html either in the downloaded package or right here. I basically hacked the HTML file from this post, giving it a new CGI parameter [name="message"] and sending it to the new script. The CGI script is also a hack from the post on python CGI. It opens up a fifo in the filesystem and writes the value of the “message” parameter to the fifo:

    form = cgi.FieldStorage()

    message = form.getvalue('message')

    pipe = open("./cgififo", 'w')

    pipe.write(str(message))

    pipe.close()

I got this fifo idea from this post. All you do is use the *nix command ‘mkfifo’ and it creates a fifo in the file-system. I used this to communicate between the CGI script and the socket server processes. To get an idea of how to get this section to run, read my post on python CGI linked above. Now to the socket server. This is basically a work in progress but I think it is good enough for right now. I encountered a lot of weird problems that caused me to write some irresponsible code but it isn’t exactly meant to be used without modification so no problem. It simply waits for a client then continually checks the fifo and passes the information off to the client. The client comes in 2 flavors. First is a bare command line interface and the second has a tkinter GUI and is multi-threaded. The problem with the bare single threaded version is that it doesn’t inform the server when the socket is disconnected. The GUI version may or may not work for you. I built a mac app in the dist folder on my intel based MacBook Pro. You can rebuild it on your machine. To get this socket stuff working, look at my post on python sockets. Now for the serial side.

Inside the socket client thread is a pySerial instance. It works exactly the same as my post on sending strings to the Arduino. It waits for the socket to get a string, sends the header to the Arduino, sends the string, then sends another header. The Arduino program is exactly the same as it is on that post as well. You should configure the program to do whatever it is you want to do with that string.

To use all this software, you have to run them in a certain order. First you give the proper permissions to the cgi script and make the fifo. Call it ‘cgififo’ so you don’t have to change any code. Then you run the socket server on your server. After that, you can run the client on your machine and connect. Here are the definitions for the fields:

CGI_APP

For host, choose your domain name [e.g. www.datasingularity.com]. The port number is the port which you have bound your socket server. The serial location is the tty file used to identify your Arduino. Baud rate is the serial baud rate and the header character is your serial header byte. Have your ssh or telnet window open to the socket server process on the remote server. When you hit connect you should see the connection in your server window. When you hit start, it will start the child thread and start listening for data. Quit closes the connection and ‘hopefully’ tells the server that you have disconnected. If not, you will need to restart the server to use again.

In conclusion, this code may be pretty ragged, but the concept is what is important and it should be enough to get you started. You obviously can’t do anything too intense with this. It could be used to do some simple but cool things though such as controlling your house from your cell phone, or feeding your dog or cat over the internet, etc. Once again, please e-mail me if you need help getting it working for your project.

**Update** Here is a demonstration video:

12 comments ↓

#1 hgecom » Control Arduino remotely with HTML forms on 11.21.07 at 7:59 am

[...] here for full [...]

#2 HOW TO Control an Arduino remotely with HTML forms » Developages - Development and Technology Blog on 11.21.07 at 2:46 pm

[...] Here’s how to control an Arduino remotely over the internet via HTML forms and/or CGI URL encoding… Link. [...]

#3 CMD-c && CMD-v » Control Arduino remotely with HTML forms on 11.21.07 at 4:09 pm

[...] yonnage wrote an interesting post today onHere’s a quick excerpt [...]

#4 HOW TO Control an Arduino remotely with HTML forms on 11.21.07 at 5:15 pm

[...] Here’s how to control an Arduino remotely over the internet via HTML forms and/or CGI URL encoding… Link. [...]

#5 DIY HACK - HOW TO Control an Arduino remotely with your browser! | zedomax.com - a blog about DIYs, hacks(wii hacks, iphone hacks), and satiric opinions on gadgets and technology today. on 11.23.07 at 5:25 am

[...] This is kinda cool way to control an LCD hooked up to Ardunino with browsers including your mobile phone. [...]

#6 Blog de Daniel Bernat » Conociendo Arduino (II) on 12.12.07 at 4:59 pm

[...] HTML: how to control an Freeduino or Arudino remotely with HTML forms [...]

#7 theLosh.com » Python Web based serial control on 12.13.07 at 1:53 am

[...] sent in his efforts to use Python to provide a web interface for his Aurdino. Python is usually pretty easy to [...]

#8 Python Web based serial control on 01.14.08 at 12:29 pm

[...] under: cellphones hacks, misc hacks, robots hacks[Benjamin] sent in his efforts to use Python to provide a web interface for his Aurdino. Python is usually pretty easy to [...]

#9 del.icio.us links-- sketchbook on 01.16.08 at 2:11 pm

[...] CMD-c && CMD-v » Control Arduino remotely with HTML forms (tags: arduino network programming python web) [...]

#10 WWW.Analyst-TW.com » Arduino 新增題材 on 11.05.09 at 6:11 am

[...] http://blog.datasingularity.com/?p=50 [...]

#11 More Arduino + LCD + PHP fun « Arduinian Tales on 02.11.10 at 1:56 am

[...] http://blog.datasingularity.com/?p=50 (control arduino through html) [...]

#12 Lars Jansen » Huge index of Arduino & Freeduino Knowledge on 07.25.12 at 4:09 am

[...] [...]