Go Down

Topic: Ethernet Shield + Arduino MEGA 2560 (Read 2 times) previous topic - next topic

aavang

Hello,

I recently purchased a SainSmart Arduino Mega2560 + SainSmart Ethernet Sheild w/SD card.

I have gotten the SD card to function correctly, but I'm having difficulty with the Ethernet.  I can have the Arduino setup an IP Address using DHCP, but whenever I try connecting to a server, for example using the WebClient example (v1.01), it always fails to connect.

I have seen in a variety of forums that you have to modify the pins and spi.h file to get the Mega to play nicely with the Ethernet chip, but the board is labeled "Mega Compatible".  I even saw where you still have to set Hardware SS (Pin 53) as an output to make the SPI library function.

Am I missing anything obvious?

SurferTim

#1
Aug 15, 2012, 05:05 pm Last Edit: Aug 15, 2012, 05:09 pm by SurferTim Reason: 1
I use the ethernet shield with a Mega2560. You need to initialize both devices (w5100 and SD) correctly if you want to use them together. The setup code in this link works for me.
http://arduino.cc/forum/index.php/topic,111352.msg837189.html#msg837189

edit: Insure you are using IDE v1.0.1. I had problems with the ethernet library prior to that.

aavang

Thanks for the prompt reply!

I ran the code you linked to and both the ethernet and SD card initialized fine.  Does this mean I just have issues on the network end, not the Arduino?

Like I said before, I can get an IP Address using DHCP, but I'm not able to connect to anywhere.

SurferTim

If it initialized ok, then the network should be fine. That required dhcp to start.

Once you have the two set up, now you must get the code for each to work together. I use this code for a test. It downloads Google home page every 10 seconds. Change the server ip to yours, and see if you can download the home page from there.
http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629
It disables the SD card while testing the download.

aavang

Alrighty.  So I ran that other program as well, but it always failed on the client.connect() call.

This is where it's always been failing for me.

SurferTim

Did you use the correct network settings? The download test sketch uses a static ip.

If you want to use dhcp, replace the setup routine in the download test with the setup routine I posted earlier, then see if it works.

aavang

Alas, even using DHCP I am not able to connect to anything.

Here is the code I'm using.  Note that I changed the values of pinMode(10,OUTPUT) to pinMode(53,OUTPUT), since I'm on a Mega.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

IPAddress server(209,85,148,147); // Google

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

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

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

  // disable w5100 while setting up SD
  pinMode(53,OUTPUT);
  digitalWrite(53,HIGH);

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) Serial.println("failed");
  else Serial.println("OK");
  // the SD.begin(4) function returns with its SPI disabled, so nothing needs to be done

  // set up w5100 with dhcp
  Serial.print("Initializing ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println("ok");

  // the Ethernet.begin(mac) function returns with its SPI enabled, so disable it
  digitalWrite(53,HIGH);
}

void loop()
{
  if(loopCount < 10)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;

    if(!getPage(server,"/")) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }   

  loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[64];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println("failed");
    return 0;
  }

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
    }
  }

  Serial.println();

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

  return 1;
}


And the response
Code: [Select]
Initializing SD card...OK
Initializing ethernet...ok
connecting...failed
Fail 1
connecting...failed
Fail 2

SurferTim

I did not say anything about pin 53. I'm on a Mega. I used the code I posted on my Mega 2560 with your Google server ip, and it downloaded that Google webpage ok.

aavang

I reverted back to using pin 10, but no change.  Is there anything in the indicator lights that might tell me something?  I see the Link and RX lights flashing but never the TX

aavang

So I did some more digging and this is what I found:

I traced the failure back through the different .cpp files.  It starts here in EthernetClient.cpp, even though I modified it to give me some more debug info about how far it got in the process.
Code: [Select]
int elsewhere = 0;
  uint8_t stat;
  while (status() != SnSR::ESTABLISHED) {
    delay(1);
    if ((stat = status()) == SnSR::CLOSED) {
      _sock = MAX_SOCK_NUM;
      if(!elsewhere) return -3;
  else return elsewhere;
    }
elsewhere = stat;
  }
    }

For some reason, it gets as far as sending the SYN packet, but it never gets a SYN back.  After waiting for one, it times out and dies.

And I have no idea why that would be happening...

Go Up