Arduino relay control over ethernet!

I have an arduino uno and a relay board and an Ethernet shield, i need some assistance with the coding part for the arduino to host a web-server and let me push 2 buttons one for on and the other for off? Can someone help me code this? I have all the hardware just i need help coding the web-server to control the relay board.... SOMEONE PLEASE HELP ME.

Web server test code that probably could be used to operate a relay.

//zoomkat 3-17-12
//simple button GET server code to control servo and arduino 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 
//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
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED 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>");

          // DIY buttons
          client.println("<a href=\"/?on1\"\">ON</a>"); 
          client.println("<a href=\"/?off1\"\">OFF</a>
"); 

          // mousedown buttons
          client.println("
<input type=\"button\" value=\"ON\" onmousedown=\"location.href ('/?on2');\"/>"); 
          client.println("<input type=\"button\" value=\"OFF\" onmousedown=\"location.href ('/?off2');\"/>");        

          // mousedown radio buttons
          client.println("

<input type=\"radio\" value=\"ON\" onmousedown=\"location.href ('/?on3');\"\">ON</>"); 
          client.println("<input type=\"radio\" value=\"OFF\" onmousedown=\"location.href ('/?off3');\"\">OFF</>");        

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0) {
            digitalWrite(5, HIGH);
            Serial.println("Led on1");
          }
          if(readString.indexOf("off1") >0) {
            digitalWrite(5, LOW);
            Serial.println("Led off1");
          }

          if(readString.indexOf("on2") >0) {
            digitalWrite(6, HIGH);
            Serial.println("Led on2");
          }
          if(readString.indexOf("off2") >0) {
            digitalWrite(6, LOW);
            Serial.println("Led off2");
          }

          if(readString.indexOf("on3") >0) {
            digitalWrite(7, HIGH);
            Serial.println("Led on3");
          }
          if(readString.indexOf("off3") >0) {
            digitalWrite(7, LOW);
            Serial.println("Led off3");
          }
          //clearing string for next read
          readString="";          
        }
      }
    }
  }
}

OK - first look at reading the buttons then at controlling the relays then at the server bits and then its done. Or in general split the problem up and then work on each part, one at a time. Simple!

Mark

There are lots of examples of Arduino web servers and it shouldn't be hard to find an example that uses an incoming request to trigger an I/O action. I suspect you could also get something working using Firmata if you don't want to make the web front end yourself.

Well i tried zoomkats code and nothing happens in my browser, i tried changing the ip address and everything is correct but i still cant get anything, chrome just tells me that the server took to long to respond?

Gamerdude2956:
Well i tried zoomkats code and nothing happens in my browser, i tried changing the ip address and everything is correct but i still cant get anything, chrome just tells me that the server took to long to respond?

Is the LAN IP address of your router 192.168.1.1?

I can vouch for zk's code... it's running here as we speak, with a few mods to read my rtc and lm35. (Edit.... except I've just zapped it and replaced with Part 16 of the Tut to which I link below.)

If you're accessing it from the lan side it should be simple. Bit more complicated to come in from the outside, since your router will need to forward the right port. Mine wouldn't work on :80, had to go with :8085.

Edit... added this snippet

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  10, 0, 0, 200}; // ip in lan
byte gateway[] = { 
  10, 0, 0, 2 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8085); //server port

More edit.... You might find this tutorial useful.

Where and how would i add that in, do i need to have an sd card in the ethernet shield?

Gamerdude2956:
Where and how would i add that in, do i need to have an sd card in the ethernet shield?

It's already in zk's code, just showing you that the numbers might need changing.

No, you don't need an SD card necessarily, but if you don't use one you should add this in setup() to disable it:

// to be safe, disabling SD explicitly
    pinMode(4, OUTPUT);
    digitalWrite(4, HIGH);

When i try to access the local page nothing happens, im using the ip 10.1.1.102 that's what i set the local ip to. i tried resetting my modem and pushing the reset button on the Ethernet shield but nothing happens in my browser? The lights on the ethernet shield blink and show status when i try accessing a the local page but in my browser it says the server took to long to respond?

Run the below code and see if an IP address is returned, and post the IP address if it is returned.

//open serial monitor to see assigned IP address

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup()
{
  Serial.begin(9600); 

  // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());
}

void loop() {

}

All the serial moniter says is starting W1500...

So what does this mean?

Is your Ethernet shield w5100 chip based, or some other chip?

My ethernet shield: http://www.amazon.com/gp/product/B006J4FZTW/ref=oh_details_o00_s00_i02?ie=UTF8&psc=1

Gamerdude2956:
All the serial moniter says is starting W1500...

So what does this mean?

That's very strange, since that code should either give you a "failed" or the DHCP IP address on the line after "starting..."

So it's not completing the Ethernet.begin one way or the other (success or fail) I'd say, which which might account for the browser complaining about the time taken.

So what should i do to try and resolve this?

So what do you suggest i do to resolve my issue

No idea- maybe the shield's faulty?

I just tried that test code on mine, and the time between the "starting" message and it telling me the ip addy is the blink of an eye.

Disconnect the relay board in case it is causing an issue. You said "i tried resetting my modem". What type of "modem" do you have the Ethernet shield plugged into? Is it a DSL or cable modem? Do you have a router in your home setup?

zoomkat:
Disconnect the relay board in case it is causing an issue.

That's a good call.... also show us a schematic, or describe the power setup. The 5100 is regarded as power hungry, so perhaps - depending on how you have it all wired up- the shield is dying from malnutrition?