Unable to connect with Ethernet shield

Hi all,

I’ve run into some problems connecting to the internet using the ethernet shield. I eventually want to get a mkrzero connected to losant but figure it’s easier to get a few example ethernet sketches working on an uno first.

I’m using a shield with a W5500 wiznet chip from seeed studio found here.

And have the client sketch working

/*
  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 <Ethernet2.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[] = { 0x2C, 0xF7, 0xF1, 0x08, 0x19, 0x2C };
// 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 Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // 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 {
    // kf 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);
  }
}

But the server sketch does not. It never seems to find an available server: EthernetClient client = server.available();

/*
  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
 */

#include <SPI.h>
#include <Ethernet2.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x2C, 0xF7, 0xF1, 0x08, 0x19, 0x2C
};
IPAddress ip(192, 168, 1, 177);

// 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 Leonardo 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");
  }
}

Laptop is connected over wifi while shield is connected the router using an ethernet cable. Library is ethernet2 1.0.3.

Can anybody help me figure out what I’m doing wrong and how I can connect and use the ethernet shield? I’ve checked numerous tutorials and none seem to work.

first of all - try to ping your Arduino server.

you use different IP-adresses in your sketches

IPAddress ip(192, 168, 0, 177);

vs

IPAddress ip(192, 168, 1, 177);

so I assume you mixed up something in your network configuration.

Thanks,
I figured that since the sketches were separate that IP number didn't matter. Only one sketch was loaded on at the same time.

I tried pinging it and this is what happened

Pinging 192.168.1.177 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

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

It seems like it isn't connected, is there something I can do to get it connected?

try the Ethernet library. yes the old Arduino Ethernet library. it has a new version 2.00 that supports W5500. try the WebClient example of the new version. it has better checks in setup

I figured that since the sketches were separate that IP number didn't matter.

what's the IP adress of your computer?

try ipconfig and post the result

Only one sketch was loaded on at the same time.

this is clear to me, but as long as the two sketches have different IP it's clear for me, that one is working the other not...

Default gateway is 192.168.0.1 and IPV4 is 192.168.0.10 web address of router is 192.168.0.1 (sticker on back)

Can you please teach me why my computer's IP address matters, it isn't clear to me.

Regarding the old ethernet library I was not using it because I intend to use the ethernet shield with a mkrzero to send information to an IoT program called losant. The mkrzero has a different SPI pin setup than the Uno and requires assigning a different SS pin. The old ethernet library won't allow that, but I guess I could modify it so it does. I'm just using the Uno right now to keep things simple.

your non working sketch uses

IPAddress ip(192, 168, 1, 177);

this will not fit to your

192.168.0.1 Gatway adress.

Use the IP adress

IPAddress ip(192, 168, 0, 177);

and it might work.

check the IP adresses carefully!(!!!)

the new old library has Ethernet.init(ss)

Just tried switching the IP numbers and it worked. I don't fully understand why but it did.

I also tried using the old Ethernet library and it worked with the Wiznet 5500 chip. Thanks for pointing out the .init method.

Only problem I has was Atmel Studio 7 with the arduino plugin wouldn't run the ethernet 2.0 library, most likely because it wouldn't let me uninstall the previous version resulting in the program getting confused. I ran it in the arduino IDE and it worked fine.

if you use Ethernet.begin(mac, ip); it will 'calculate' the gateway and dns IP address from provided IP address, changing the last part of iP to 1, in case of 192.168.1.177 it will be 192.168.1.1.

if you would call Ethernet.begin(mac, 192.168.1.177, 192.168.0.1, 192.168.0.1, 255.255.0.0 ) (pseudo code) it would perhaps work, but why to complicate things?

As posted already yesterday:

noiasca:
IPAddress ip(192, 168, 0, 177);

vs

IPAddress ip(192, 168, 1, 177);

With an IP-MASK of 255.255.255.0 these two adresses are in different networks which you can not access from one to another. Easily spoken: you can only change the last octet! If you need more than 255 IP-Adresses you need another IP-MASK.