Home > Fun IT Projects, How-to Guides, Utilities And Other Useful Things > Arduino Project 5: Internet Control without Ethernet Shield

Arduino Project 5: Internet Control without Ethernet Shield

I’m getting ready to start an Arduino home automation project, so I started looking at ways to interface with an Arduino across the internet. That way I’ll be able to control all of the lights, locks, etc. anywhere I have internet access.

Now, the obvious answer was to buy an Ethernet shield, but I already run a home web server so that seemed unnecessary. I saw a few solutions using Processing or Python scripts, but that seemed unnecessarily complicated. It took a fair amount of digging and brainstorming, but I’ve found an ultra-easy, ultra-flexible, and ultra-fast solution. Ultra.

There are a few major assumptions here.

  1. You have a computer running a web server that is accessible from the open internet, using a static IP address or a Dynamic DNS name. Your computer is either directly connected to your ISP and using a public IP address or your router is set up for port forwarding. If you don’t have this set up yet, just Google it – there are a lot of tutorials that explain how to set up a home web server.
  2. Your home web server has PHP. If not, this will still work, you’ll just have to re-write it in Java or whatever server-side language you’re using.
  3. The Arduino will be plugged into the web server via USB.

Here’s the basic concept: the Arduino can’t read files from the server via the USB serial connection, so the server will have to “push” the message. The server side code (PHP, Java, whatever you choose) cannot talk directly to the serial COM port, so we need a local script on the web server that can talk to the serial (USB) port. Last, the Arduino sketch has to be written so it can “catch” and process the message.

In this example, we’ll just create a barebones web page for controls that will turn an LED on and off via the Arduino. Let’s start with the sketch. The sketch is just listening on the serial connection for a 1 or 0. If it receives a 1 it will turn the LED on, if it receives a 0 it will turn the LED off.

Alex Glover
February 2013

void setup() {
//set the LED pin to OUTPUT
pinMode(13, OUTPUT);

void loop() {
//wait until the serial connection is open
while (Serial.available() ==0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int val = Serial.read() - '0';

//print to the console for testing

//if we've recieved a '1', turn on the LED and print a message
Serial.println("Received a 1");
digitalWrite(13, HIGH);
//if we've recieved a '0', turn off the LED and print a message
Serial.println("Received a 0");
digitalWrite(13, LOW);

Pretty straightforward, right? OK, now we need two scripts, one to send a ’1′ and one to send a ’0′. In Windows, simply create a text file (call it whatever you want), and give it a .bat extension. In my setup, my files are called serial_out_0.bat and serial_out_1.bat. Each script has only one line of code.

ECHO 0 > COM3:


ECHO 1 > COM3:

Note that you might have to change the COM designation. You can check which COM your Arduino is connected to by looking in the Arduino IDE under Tools –> Serial Port. If you’re not using Windows, you should be able to do this pretty easily in a shell script. At this point you can test to see if the batch scripts will turn the LED off and on. Also ensure that the web server will be able to execute these scripts (don’t assign user-specific privileges or save them in protected directories). If you’re unsure, just put these scripts in the same web root directory where you’ll host your web page.

Easy right? Alright, the last piece is the web form. All you need are buttons within a form that will then execute the batch scripts we wrote earlier. Easiest solution is to use ‘submit’ buttons and then check for which post variables are set. The rest of the code is very straightforward so I’ll let it speak for itself.

if(isset($_POST['submitOn'])) {
else if(isset($_POST['submitOff'])) {
<form action=”control.php” method=”post”>
<input type=”submit” name=”submitOn” value=”Submit On”>
<input type=”submit” name=”submitOff” value=”Submit Off”>


You’ll have to change the paths to correspond with the location of your scripts, but otherwise that’s it. Add some CSS if you want to add some polish to your controls. This is a very simple example, but you should be able to adapt this code to any project.

About these ads
  1. February 18, 2013 at 18:42 | #1

    Awesome idea and easy way around sinking money into an ethernet shield if interfacing your arduino with a PC is reasonable for your application.

    One correction that I’d like to make, however, is this:

    “The server side code (PHP, Java, whatever you choose) cannot talk directly to the serial COM port…”

    There is nothing inherent about server side code not being able to talk to a serial port. You could in minutes write a Python web server that interfaces with any hardware without the need for any auxiliary script to be executed (http.server + pySerial).

  2. February 18, 2013 at 21:15 | #2

    I stand corrected.

    A Redditor actually brought up this same point, suggesting I call the “ECHO 1 > COM3:” directly from the PHP exec() calls instead of calling the intermediary batch script.

    Thanks for contributing.

  3. webUser
    June 23, 2013 at 07:56 | #3

    Great idea! Im gonna use it. Thanks!
    Cheers from Czech Republic!

  4. July 6, 2013 at 07:31 | #5

    My bat files don’t seem to be working, my Arduino is connected via COM26, I edited the files, saved them on my desktop, when I tested them, they didn’t work!
    Any suggestions?!

    Thanks in advance :)

    • July 6, 2013 at 17:29 | #6

      Hey Samuel –

      I’m guessing you’re using the same sketch that I used in my post. If so, try using the Serial Monitor in the Arduino IDE to send 1s and 0s to your Arduino. If that doesn’t work, then there is probably something wrong with your sketch or the way you’ve wired your LED.

      Also, some devices and applications complain when you use higher COM ports (like COM26). Try going into device manager and deleting some of your unused COM ports to get your Arduino back to a reasonable COM port. A quick google of “com port issues with high number” should help get you started.

      If you still have issues after that, send over your code and I’ll take a look.

      Thanks for stopping by, and check out http://alexdglover.com/ when you get a chance.

      • Hicham
        February 10, 2014 at 21:51 | #7

        I have aslo the same problem and it works when I send 0 or 1 using the Serial Monitor.I also tried to change the COM number to 2 (COM2) but still doesn’t work. :(. Any ideas ?

  5. November 25, 2013 at 17:42 | #8

    Hello, could you write the control.php? it could be helpful if you can add it in the page.

    • November 27, 2013 at 17:17 | #9

      Hey techelen, thanks for contributing.

      I apologize, I should have been clearer in my post. The HTML/PHP code IS the control.php file. The form is doing a post to itself. Basically, whatever you name that HTML/PHP file, put that name in your in form action. For example, if you named that file whatever.php, your form tag should be
      <form action="whatever.php" method="post">

      Let me know if you still have questions.

  6. December 14, 2013 at 04:31 | #10

    hello alex.

    whether this method can be used for server hosting. ?

    • December 17, 2013 at 17:59 | #11

      Well in my example I’m communicating with the Arduino via USB, so that would be difficult if you were using a hosting service physically located somewhere else.

  7. January 6, 2014 at 00:10 | #12

    really helpful here, alex…thank you! exactly what i was looking for. (my arduino mega running my automated chicken coop is too full and i didn’t want to have to change everything with a shield)

    question… i’m still a bit new to arduino/c++ (but i know php) and maybe it should be obvious to me, but offhand, is there a way you know of that this communication process can also be done in reverse? meaning, i’d also like to have a php page display arduino serial port messages. i currently have the the arduino reading the temp, heat, cooling fan &door opening/closing and would love to be able to see the coop status read via the web (if that makes sense)

    thanks again and cheers!

  1. No trackbacks yet.

Care to share your two cents?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 46 other followers

%d bloggers like this: