Arduino Ethernet shield can't send

I am new to Arduino . i bought Arduino Uno R3 Microcontroller A000066 and RioRand ™ Upgraded Ethernet Shield W5100 for Arduino UNO R3 & MEGA 2560 Duemilanove …

now i have a very strange issues . if i assign IP and mac to my Ethernet Shield without any code inside the loop fucntion i can successfully ping it without any issue … but once i start using any code inside the function i get 98% request time out trying to ping my Ethernet Shield .

also it seems that the Ethernet Shield can’t ping or send any traffic but it can receive only .

here is the code that i used first to assign the IP and successfully ping without any issue

/*
  Ping Example
 
 This example sends an ICMP pings every 500 milliseconds, sends the human-readable
 result over the serial port. 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 30 Sep 2010
 by Blake Foster
 
 */

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// max address for ethernet shield
byte ip[] = { 10,10,18,198 }; // ip address for ethernet shield
IPAddress pingAddr( 10.10.18.129 ); // ip address to ping
IPAddress subnet(255, 255, 240, 0);


SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() 
{
  // start Ethernet
  Ethernet.begin(mac, ip, subnet);
  Serial.begin(9600);
}

void loop()
{

}

and here is the full code that when i use i can’t ping the ethernet shield and also the echoReply.status never get success . i used loopback ip 127.0.0.1 and also same ip as the shield . with no luck .

i am sure about cables , RJ , switch , etc .

/*
  Ping Example
 
 This example sends an ICMP pings every 500 milliseconds, sends the human-readable
 result over the serial port. 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 30 Sep 2010
 by Blake Foster
 
 */

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// max address for ethernet shield
byte ip[] = { 10,10,18,198 }; // ip address for ethernet shield
IPAddress pingAddr( 127,0,0,1 ); // ip address to ping
IPAddress subnet(255, 255, 240, 0);


SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() 
{
  // start Ethernet
  Ethernet.begin(mac, ip, subnet);
  Serial.begin(9600);
}

void loop()
{
  ICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
  }
  Serial.println(buffer);
  delay(500);
}

also i tried this code which get IP from my dhcp server and try to get HTTP , but again no luck .

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//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

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // 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(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    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

}

but i never get any response from the server . i got only this to my serial output

Better client ip test 3/1/13
Send an e in serial monitor to test

any idea how to fix this . i just need to be able to ping one IP and based on that i can do something else . but till now i can see that this Ethernet shield cant send any packet .

IPAddress pingAddr( 127,0,0,1 ); // ip address to ping

The Arduino Ethernet Shield doesn't know the concept of a localhost address. So it cannot ping itself. Use the IP address of another host on the same network for the ping target.

@pylon

i added that ip 127.0.0.1 as last resort to test . but before i added the IP on the network , i tried also same ip as the Ethernet shield ( i already mention all this )

and nothing work.

If you don't send a character over the Serial interface, it will never do anything.

@SurferTim
i am already using

Serial.println(buffer);

the problem is it can't ping any ip or send any packet outside .that is what i need help for

i added that ip 127.0.0.1 as last resort to test . but before i added the IP on the network , i tried also same ip as the Ethernet shield ( i already mention all this )

As far as I know the Ethernet Shield isn't able to ping itself, even if you ping the IP you set for it. Try to ping your local router or any other device that does answer pings on your network (be sure that no firewall blocks ICMP messages).

also i tried this code which get IP from my dhcp server and try to get HTTP , but again no luck .

At least your shield seems to get an IP from your local DHCP server (probably your router). So communication is possible and the network works.

You might want to try one of the webserver examples and then access your Arduino by your browser.

@pylon

yes it can assign ip from DHCP and it is in the same subnet . as i said i already tried that in the third code i mention in my first post and still it cant connect ..

i am asking about 2 things

1- why when i add any code in the loop the Ethernet shield get unstable ( even it is a very simple code )

2- why the Ethernet shield cant send any packet to any host in the network ( same subnet )

1- why when i add any code in the loop the Ethernet shield get unstable ( even it is a very simple code )

The code you added in your first example is NOT simple code. Post an example of SIMPLE code in loop() that causes a problem.

2- why the Ethernet shield cant send any packet to any host in the network ( same subnet )

Mine can, so your problem must be with your hardware (which seems to be working if you can ping the Arduino) or with your network (router, router configuration, firewall(s), etc.).

@PaulS

this simple code made the Ethernet shield has 90% request time out when i ping it . without the code inside the loop i got 100% reply

/*
  Ping Example
 
 This example sends an ICMP pings every 500 milliseconds, sends the human-readable
 result over the serial port. 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 30 Sep 2010
 by Blake Foster
 
 */

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// max address for ethernet shield
byte ip[] = { 10,10,18,198 }; // ip address for ethernet shield
IPAddress pingAddr( 10,10,18,129 ); // ip address to ping
IPAddress subnet(255, 255, 240, 0);


SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() 
{
  // start Ethernet
  Ethernet.begin(mac, ip, subnet);
  Serial.begin(9600);
}

void loop()
{
  ICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
  }
  Serial.println(buffer);
  delay(500);
}

thats why it is so strange .

i took this lib and this code from here

https://playground.arduino.cc/Code/ICMPPing

thats why it is so strange .

There is NOTHING strange about it. The code in loop() is sending pings. The Arduino can NOT respond to a ping while it is waiting for a reply from a ping that it sent.

@PaulS

Thanks for explaining that to me .

but even with this code , there is no output showing that it success in sending any packet outside .
i will test another code tomorrow this one

/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

and see if it can connect or not … i will update here tomorrow

Since you will be using a domain name for the server, you should change this:

// change this
  if (client.connect(server, 80)) {

// to this
  if (client.connect(server, 80) == 1) {

The only success return value is 1. A zero or negative number is a fail.

Thanks all for your help . every thing is working fine now . this issue was due to a firewall located in the target PC drop the ping :slight_smile: .. yes i know sometimes it happens