web server example problem

Using IDE 1.8.5 webserver example, on Leonardo or Mega with Wiznet5100.

It wont render on Firefox / opera. Just get "This site can’t be reached"

However it will show on serial monitor that it gets a connection from the browsers as this shows just after I refresh browser

new client
GET / HTTP/1.1
Host: 192.168.0.120
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1

client disconnected

I only did 2 changes to webserver example

byte mac[] = {
  0xAA, 0xAA, 0xAA, 0xBA, 0xAA, 0x20
};
IPAddress ip(192, 168, 0, 120);

Arduino Ethernet shield is plugged directly into Asus RTN16.

Tried 3xLeonardos, 1xMega, 1xUno and 2xW5100, and 2 new ethernet cables.

mac address is unique to my network

Also tried from can't ping outside my network using ICMPPing from playground - Networking, Protocols, and Devices - Arduino Forum

byte mac[] = {
  0xAA, 0xAA, 0xAA, 0xBA, 0xAA, 0x20
};
IPAddress ip(192, 168, 0, 120);
IPAddress gateway(192,168,0,2);
IPAddress subnet(255,255,255,0);

Ethernet.begin(mac, ip, gateway,gateway,subnet);

Any help appreciated

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xAA, 0xAA, 0xAA, 0xBA, 0xAA, 0x20
};
IPAddress ip(192, 168, 0, 120);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

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 and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        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");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Using IDE 1.8.5 webserver example

We want to see your code, not know that you started from an example.

The serial output seems to indicate that the Arduino-as-server got a GET request. If the response did not get back to the client that made the request, the problem is unlikely to be on the Arduino end.

Try to remove the "Refresh" header line. It was a proprietary extension of Netscape to the HTTP 1.0 standard and is not allowed in HTTP 1.1 as far as I know. I never heard the syntax was looking so simple, even if many browsers might still support it, it needs at least an URL to refresh to.

Hi
@ Paul, That is my code, I only changed the mac and IP to match my pool
I tried other arduinos and another w5100 shield
@ Pylon, Deleted client.println("Refresh: 5");

still get from browser "This site can’t be reached
The connection was reset."

Something weird is that 192.168.0.120 doesn't show up on my router or "Fing" android app, yet arduino responds to serialMonitor via ethernet so there is a connection

Thank you

pinging gives me;
Pinging 192.168.0.120 with 32 bytes of data:
Reply from 192.168.0.120: bytes=32 time<1ms TTL=128
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.0.120:
Packets: Sent = 4, Received = 1, Lost = 3 (75% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

arp -a shows
192.168.0.120 aa-aa-aa-ba-aa-20 dynamic

I have rebooted asus router numerous times

Connect the Arduino directly to your Notebook/PC, without the router in between.

75% packet loss is a network problem. Is your WizNet 5100 shield an Original Arduino Ethernet Shield or some Chinese clone? In the later case seriously check the hardware. These cheap boards often have quality problems and often circuit errors keeping them from connecting to many switches.

Hi Pylon,
Connected shield directly to my desktop Ethernet port and it works perfectly immediately.

I connect it directly to my Asus N16 router and it doesn't show webpage.

My router works fine normally and it is on the Asus Sept2017 firmware (which FCC and Asus have now locked down ), may have something to do with my problem.

There are a couple of PCs and 1xPi and numerous phones which all work.
Could it be a problem with the router? and why just the arduino.
The Ethernet shield V1 is genuine, and the leonardo looks genuine.

Try replacing this code:

client.println("HTTP/1.1 200 OK");
          client.print("Content-Type: text/html\r\n");
          client.print("Connection: close\r\n");  // the connection will be closed after completion of the response
          client.print("Refresh: 5\r\n");  // refresh the page automatically every 5 sec
          client.print("\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.print("
\r\n");
          }
          client.println("</html>\r\n");

Thanks Boylesg
I tried that and i still get timeout on browser.

When I pinged now i get

C:\Users\user>ping 192.168.0.120

Pinging 192.168.0.120 with 32 bytes of data:
Reply from 192.168.0.209: Destination host unreachable.
Reply from 192.168.0.209: Destination host unreachable.
Reply from 192.168.0.209: Destination host unreachable.
Reply from 192.168.0.209: Destination host unreachable.

Ping statistics for 192.168.0.120:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

C:\Users\user>

On my router there is no 192.168.0.120(Arduino) on my DHCP log. Also tried Fing.

I have changed the router to another one the same to clear the NVram

PUzzled !!

Have you tried a different ethernet cable?

I tried different ethernet cables USB power cables, Powersupplies and usb ports on PC.
Interestingly;
With your code above I couldn't get the feedback on serial monitor when browser requested page.

To recap, using the webserver example and changing mac and IP to 192.168.0.120, when I request webpage from browser I get;
This site can’t be reached. 192.168.0.120 took too long to respond.

In serial monitor I get;

client disconnected
new client
GET / HTTP/1.1
Host: 192.168.0.120
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
DNT: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8

Tried Opera, Firefox,Chrome + IE

Try increasing this delay: delay(1);

Right a little further on, we're getting close:

on serial monitor I got "server is at 255.255.255.255 "
After plugging/ unplugging / reset
I got "server is at 192.168.0.112"

There must have been a change to the ethernet library since this webserver example was written.
I put the code on a mega and I can access the webserver as one would expect but;

my code says " IPAddress ip(192, 168, 0, 120); "
However to access the webpage I use 192.168.0.112

Could there be a automatic arduino DHCP bit in the library which overwrites 192, 168, 0, 120

Alternatively how should I change the webserver example to use arduino DHCP

On my router there is no 192.168.0.120(Arduino) on my DHCP log. A

As that code does not do a DHCP request that's no surprise.

There must have been a change to the ethernet library since this webserver example was written.

Not in this regard.

byte mac[] = {
  0xAA, 0xAA, 0xAA, 0xBA, 0xAA, 0x20
};
IPAddress ip(192, 168, 0, 120);

Ethernet.begin(mac, ip);

[quote]Alternatively how should I change the webserver example to use arduino DHCP
[/quote]

Maybe your router blocks clients that did not request the IP by DHCP?

will set the IP to 192.168.0.120, the dns server and the gateway to 192.168.0.1 and the netmask to 255.255.255.0. If that doesn't match your network you may need to use a more complex begin() method.
If the accessing PC is on the same network and the network mask is correct, a wrong DNS and gateway shouldn't make any difference.

Right a little further on, we're getting close:

on serial monitor I got "server is at 255.255.255.255 "
After plugging/ unplugging / reset
I got "server is at 192.168.0.112"

Did you change anything in the sketch? Did you choose another begin() method?

Hi Pylon,
Thanks for sticking with this.
I did try the Ethernet.begin(mac, ip, gateway,gateway,subnet); full settings in the first post, but no joy.

After trying loads of things, I noticed using arp -a that there had been a connection registered on the router 192.169.0.244, the webpage didn't work but I put those settings including mac into my sketch and I also fixed the address in my routers DHCP.
This seems to have solved it.

The last time I used arduinos, 2 years or so, I had no issues but since then I updated the Arduino IDE, Asus router Firmware and Windows10. take your pick!!!

Thanks a mill for your suggestions

Actually I think something has changed
I wanted to add another Arduino +Shield onto my network and had the same problems as previously.
Using Library manager in IDE 1.8.5, I changed the ethernet library back to 1.04, reloaded my sketch and hey presto worked immediately.

Pinging yesterdays arduino with ethernet 1.1.2
Pinging 192.168.0.97 with 32 bytes of data:
Request timed out.
Reply from 192.168.0.97: bytes=32 time<1ms TTL=128
Request timed out.
Request timed out.

Ping statistics for 192.168.0.97:
Packets: Sent = 4, Received = 1, Lost = 3 (75% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

Pinging todays 2nd arduino with ethernet 1.04

Pinging 192.168.0.95 with 32 bytes of data:
Reply from 192.168.0.95: bytes=32 time<1ms TTL=128
Reply from 192.168.0.95: bytes=32 time<1ms TTL=128
Reply from 192.168.0.95: bytes=32 time<1ms TTL=128
Reply from 192.168.0.95: bytes=32 time<1ms TTL=128

Ping statistics for 192.168.0.95:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

Both sketches are exactly the same except mac and IP.
Both are leonardos with ethernet.
Both have same 1.5m ethernet wiring and same distance from the router

I am being 100% straight here, The only difference is the ethernet library used

Thanks for your help