Code for controlling Arduino Ethernet over web

I want to be able to control my arduino ethernet over the web by a computer not on my home network and I want to be able to perform a simple task of just turning on pin 7 or 8... I followed a code out of a book and was unsuccessful. If anyone knows the code to perform this could you please break down the setting up of the byte ip, subnet, gateway... I don't mean to sound like a retard here but I am new to the arduino ethernet. I have a website and my intent is to control my arduino ethernet on a page on my website. Please Help!

well, you could use a VPN or Virtual Private Network. This would enable you to act like you are in your network but not. So, you could control your arduino using your phone, computer, or more because it would allow you to send packets to your arduino.

The Arduino can act as a server, serving up a page, or it can act as a client, making requests of servers. Which do you want?

It sounds like you already have a server, and want the Arduino to ask it for information periodically. That is not typically the way to control an Arduino over the internet.

Basically I want to use a page on my website to control the arduino ethernet so that I can control when certain pins are on and off which in turn controls certain lights in my house. Of course i'll have to hook up relays and some other things but my main issue is how to accomplish the task of controlling pins over the internet.

Basically I want to use a page on my website to control the arduino ethernet

Far simpler to have the Arduino serve up the page.

You could, I suppose, have your server serve up the page containing a form with submit buttons, and then have the action be the Arduino, but that's harder to implement, since a redirect is required to get back then to the original page.

I don't mind the idea of having the arduino serve up the page, I was just under the impression that if you did this you could only access the arduino from your network not from any computer.

paul_qua19:
I don't mind the idea of having the arduino serve up the page, I was just under the impression that if you did this you could only access the arduino from your network not from any computer.

That has nothing to do with the Arduino and everything to do with your local network.

Simple web based control test code. You don't have to serve a control page from the arduino. If you don't, then you will need to modify the control links in the page to the full url for your arduino.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

That has nothing to do with the Arduino and everything to do with your local network.

Specifically, how your router is assigned an IP address (typically by your internet service provider) and how that router is configured to forward data to the other devices on the local network.

A static IP addressed assigned to your router, and port forwarding enabled to forward data for a specific port (that can be 80) to the Arduino is one way. Internet service providers charge for static addresses, because they don't want you running a server on their network.

A service running on a PC in your network that gets the forward facing IP address of the router and updates DNS servers with your new IP address every time the internet service provider assigns a new one is another way.

In any case, make sure that your router is configured to always assign the Arduino the same address whenever it restarts.

You could simply store the data that you want the Arduino to have, on your server, and have the Arduino as client periodically do a GET to get that data. Probably a lot of unnecessary traffic that way, though.

In any case, make sure that your router is configured to always assign the Arduino the same address whenever it restarts.

Or just hard code the IP address in the arduino code.

Or just hard code the IP address in the arduino code.

If the Arduino says it is 192.168.0.123, and the router thinks it is 192.168.0.14, the router is not going to route packets addressed to 192.168.0.123 to the Arduino.

and the router thinks it is 192.168.0.14,

So what is going to make the router think the arduino's IP address is 1 92.168.0.14, or any other number for that matter. I think most routers will accept an internally assigned IP address unless there is an IP address conflict.

So what is going to make the router think the arduino's IP address is 1 92.168.0.14

I told my router to reserve a specific address for my Arduino. That way, it doesn't assign that address to another device.

I use that address on the Arduino, then, so that packets contain the correct from address, so that replies can be routed back to the Arduino.

I think most routers will accept an internally assigned IP address unless there is an IP address conflict.

Reserving an address is a way to assure that there are not address conflicts. It's an easy step that is worth doing, in my opinion.

Reserving an address is a way to assure that there are not address conflicts. It's an easy step that is worth doing, in my opinion.

In my router I limit the DHCP range to a low reasonable number (nobody but me should be using my router), then I can test code with DHCP included, or use hard coded IP address if desired, without going back into the router setup.

In my router I limit the DHCP range to a low reasonable number (nobody but me should be using my router)

There were 4 of us at one time using the router, with a couple of computers and a phone apiece, along with TIVO and some iPads. You run out of addresses quickly that way...

There were 4 of us at one time using the router, with a couple of computers and a phone apiece, along with TIVO and some iPads. You run out of addresses quickly that way...

Well, if you are the neighborhood "hotspot" I guess that might happen. You could set 192.168.1.2 thru 192.168.1.99 for DHCP use, and the remainder for static IPs.

Well, if you are the neighborhood "hotspot" I guess that might happen.

The router is not accessible without knowing the WEP key.

You could set 192.168.1.2 thru 192.168.1.99 for DHCP use, and the remainder for static IPs.

Yep. That's what we did, except that I think the upper end is 120.