Mega 2560 with Ethernet shield

I have the Arduino Mega 2560 and the official Ethernet shield with the WIZnet iEthernet W5100 chip, witch I'm trying to use it to control some relays in order to control some higher voltage. There will be a total of 32 relays.

I want to control them by Ethernet (as I will be about 50m away from the card).
So I was wondering if anyone had any idea how I should do this. I'm not very experienced with the programming language, but I know the basics. I just need some help to to get at the right track. (at first at least)

Thank you for your time
StreamCyper

Simple web page control of a pin.

//zoomkat 1-10-11
//routerbot code
//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>

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(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // 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); //uncomment to see in serial monitor
        } 

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

          ///////////////
          Serial.println(readString);

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          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>HTML form GET example</H1>");

          client.println("<FORM ACTION=\"http://192.168.1.102:84\" method=get >");

          client.println("Pin 4 \"on\" or \"off\": <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Change Pin 4!\">");

          client.println("</FORM>");

          client.println("
");

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

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

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

        }
      }
    }
  }
}

thank you very much for your help and fast reply :smiley: hopefully I will be able to work out what I need from this :slight_smile:

I've tried using the code I got from Zoomkat, but no matter what I try I can't connect to my Arduino. anyone has any suggestions for what I should do?

anyone has any suggestions for what I should do?

First question is have you ever gotten any ethernet code to work on your arduino? Second, you may need to provide information as to the lan network you are connecting to. Below is some simple client code you can try to see if it works.

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, subnet, gateway);
  Serial.begin(9600); 
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

No I have not tried any other Ethernet programs on it as it is brand new.
when I try to run the test program I just get connection fail. so it's obvious I'm doing something wrong...

I'm doing something wrong...

Yes, the biggest thing you are doing wrong if you want help is not supplying the requested info on your network hardware and connections.

sorry about that.... I got the the arduino connected to my computer with normal USB cable, and also it's connected to a network switch. my computer is also connected to the switch by cable, that's all that is connected to the switch. I don't need to be able to connect to the arduino through Internet only through LAN.

the switch I use is this one:

I'm using the arduino 1.0 programming software, it's set up like this:
Board: Arduino Mega 2560 or Mega ADK
Serial Port: COM 4
Programmer: AVRISP mkII

Have you tried the sketch that zoomkat posted? What does the serial monitor say? Have you tried pinging the ethernet shield from your computer? Is the IP that you used in the sketch in the same range as your computer?

when I use sketch Zoomkat gave me, (just copy, paste and change mac and IP) and I get connection failed, Disconnecting.

I use IP: 169.254.216.120 for my PC, and IP: 169.254.216.121 for arduino.

When I try to ping the IP address of my arduino it responds.

when I use sketch Zoomkat gave me, (just copy, paste and change mac and IP) and I get connection failed, Disconnecting.

why did you change the mac address?

I use IP: 169.254.216.120 for my PC, and IP: 169.254.216.121 for arduino.

What does the ip address of your pc have to do with the arduino code? These may well be valid routable internet ip addresses and you may be pinging a computer "out there". 192.168.. are usual ip addresses used on a lan. Making random changes to working code usually results in failure.

what IP I use on the computer have nothing to do with the arduino code, but you complained that I didn't give enough info, so I give all info that just far out be useful.

Reason for changing the mac address is only because I changed it to the mac address that what is written on the Ethernet shied, no other reason.

Now I got it working :smiley: I couldn't get anything working so I tried to really get into the code and understand it... then I found the problem with the test code. it tried to connect to "my server" with the IP: 208, 104, 2, 86 and then it failed. then I connected it to my home network and used on of my other computers as server and it worked. Then I went back to the other program for controlling the pins. and I found out that I had messed up the IP's there as well and then it didn't work of course...

Sorry for all the trouble :sweat_smile: and thank you very much for all the help!!!