1

I recently bought a W5100 Ethernet shield and I'm trying to run this simple example, which is a little improvement of the basic WebServer sketch provided by Arduino.cc

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress DnsServer (192, 168, 1, 6);
IPAddress GateWay (192, 168, 1, 1);
IPAddress SubNetMask(255, 255, 255, 0);
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80

String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default

void setup() {
  Ethernet.begin(mac, ip, DnsServer, GateWay, SubNetMask); // initialize Ethernet device
  server.begin(); // start to listen for clients
  Serial.begin(9600); // for diagnostics
  pinMode(2, OUTPUT); // LED on pin 2
}

void loop() {
  EthernetClient client = server.available(); // try to get client
  if (client) { // got client?
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) { // client data available to read
        char c = client.read(); // read 1 byte (character) from client
        HTTP_req += c; // save the HTTP request 1 char at a time
 // last line of client request is blank and ends with \n
 // respond to client only after last line received
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          // send web page
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Arduino LED Control</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1>LED</h1>");
          client.println("<p>Click to switch LED on and off.</p>");
          client.println("<form method=\"get\">");
          ProcessCheckbox(client);
          client.println("</form>");
          client.println("</body>");
          client.println("</html>");
          Serial.print(HTTP_req);
          HTTP_req = ""; // finished with request, empty string
          break;
        }
 // every line of text received from the client ends with \r\n
        if (c == '\n') {
 // last character on line of received text
 // starting new line with next character read
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      } // end if (client.available())
    } // end while (client.connected())
    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl) {
  if (HTTP_req.indexOf("LED2=2") > -1) {
    // see if checkbox was clicked
 // the checkbox was clicked, toggle the LED
    if (LED_status) {
      LED_status = 0;
    } else {
      LED_status = 1;
    }
  }

  if (LED_status) { // switch LED on
    digitalWrite(2, HIGH);
    // checkbox is checked
    cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
  } else { // switch LED off
    digitalWrite(2, LOW);
    // checkbox is unchecked
    cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
  }
}

So I proceed with connecting my shield via a LAN cable to my router (Gateway 192.168.1.1), but if I ping 192.168.1.20 (my Arduino) it says "Host Unreachable", my router use DHCP which always worked and still works fine, I'm sure that there aren't any MAC address or IP conflict. I googled around a lot but nothing seems to work.

My DNS server is 192.168.1.6 because I'm running PI-Hole (it's an ad blocker) on a Raspberry, and I don't think this is the problem because all devices connected to it are working fine.

Please any help? Do I have to assume that my shield is faulty?

I tried also using DHCP connection in order to get an IP from the router by doing a:

Ethernet.begin(mac);

But no good as well.

  • Genuine Arduino shield, or Chinese knock-off? Those Chinese knock-offs often need a few resets to get them to actually communicate. – Majenko Sep 1 '16 at 10:51
  • @Majenko it's a Chinese one, what do you mean by few resets? Clicking the reset button few times? – FabioEnne Sep 1 '16 at 10:53
  • That usually worked for me, yes. – Majenko Sep 1 '16 at 10:53
  • Oh, and you might like to wrap all your string literals in F("...") to move them out of precious RAM. – Majenko Sep 1 '16 at 10:54
  • @Majenko alright, I'll try when I get back from work ;) – FabioEnne Sep 1 '16 at 12:02
1

"...Please any help? Do I have to assume that my shield is faulty?..."

Before trashing the shield, I'd run through a sligtly more comprehensive troubleshooting approach, from the lowest level of the network stack (the physical layer) up to the ICMP/IP layer. Let me expand over this process:

  1. Physical layer: I bet your router has some LEDs whose purpose is to show IF some ethernet device is connected to related physical router port... or not. So, as a very first step, supposing you connect your ethernet shield to PORT-1 of your router, you should check that once you connect the cable and turn on the Arduino.... the "port 1" LED on the router should turn on. If it turns on... let's proceed to next step. Otherwise... let's launch a different question (or expand accordingly your OP);

  2. Ethernet layer: once the LED is on, we can be sure that the physical layer is OK. So let's go up at Ethernet layer. In order to communicate on an Ethernet network, every device need to have a MAC address. Also, even if not strictly necessarily, we can bet that every ethernet device (including your shield) implement the ARP resolution protocol. This is needed in order, for other ethernet devices, to obtain the MAC address of your shield, in order to include such a MAC address in all the ethernet datagrams to be sent over the network (to communicate with your shield). So, if you configured your shield with MAC DE:AD:BE:EF:FE:ED and IP 192.168.1.6 I would:

    1. connect my Linux box to your router (BTW: sorry! I can't help with other operating systems, as I switched to a linux-only notebook lots of years ago!);
    2. configure my Linux ethernet interface (eth0) with an IP like 192.168.1.99;
    3. run an arping -I eth0 192.168.1.6 (where arping is this tool)

If everything is working correctly, you should receive something like:

root@iMac-Chiara:~# arping -I eth0 10.20.20.2
ARPING 10.20.20.2 from 10.20.20.1 eth0
Unicast reply from 10.20.20.2 [A4:BA:DB:98:FC:A4]  0.736ms
Unicast reply from 10.20.20.2 [A4:BA:DB:98:FC:A4]  0.729ms
Unicast reply from 10.20.20.2 [A4:BA:DB:98:FC:A4]  0.720ms
[...]

where you see that my notebook (10.20.20.1) is getting ARP-replyies from 10.20.20.2, who has the A4:BA:DB:98:FC:A4 MAC address.

If you don't see replyies... we have an Ethernet problem (but not a physical one) and... chances are high that you have a faulty shield (BTW: the message "Host unreachable" you mentioned in your OP, let me think that this is your problem);

Viceversa, if you obtain replyies, than the Ethernet level is OK and problems are located on upper layers. Let's focus on this last particular case

  1. IP layer: once the physical layer and the ethernet layer are OK, you should be able to properly PING your target (I'm excluding the case where you have some firewall that, somewhere along the path from the two hosts, could "cut" some traffic. I'm also supposing that IP addresses and netmask are properly configured [from an IP point of view]). I'm not getting deeper with such a case, as you should be already familiar with PING (BTW: from your POST, it's not clear from where you were PINGing your Arduino. Directly from the "router"? or from you PC?)

Having said all the above, let me conclude by saying that several further checks could be putted in place but... it's much better to previously run above checks :-)

Hope this help!

  • I read with a lot of interest, but yesterday I passed in front of a shop in Milan who was selling an original Arduino Ethernet Shield 2 for 22€ so I couldn't resist and I've bought one, connect and everything worked fine in less than 3secs..I suppose that the oder CHine clone for 4€ was a waste of money ;) Thanks everybody by the way – FabioEnne Sep 3 '16 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.