0

I'm using an arduino UNO with ethernet shield. When following a few guides online for a school project I came across this issue. First code sample:


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

    EthernetClient client;

    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192, 168, 1, 25);
    byte server[] = { 64, 233, 187, 99 }; // Google

    void setup() {  
      Ethernet.begin(mac, ip);
      Serial.begin(9600);
      delay(1000);
      Serial.println(Ethernet.localIP());
    }

    void loop() {
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET /search?q=arduino HTTP/1.0");
        client.println();
      } else {
        Serial.println("connection failed");
        Serial.println(client.connect(server, 80));
      }
    }

This doesn't work unfortunately. We successfully connect to our network, but client.connect() doesn't work and returns 0. Expected result:

SUCCESS: 1 TIMED_OUT: -1 INVALID_SERVER: -2 TRUNCATED: -3 INVALID_RESPONSE: -4

According to the documentation it shouldn't be able to return this. Why wouldn't this work? Thanks in advance.

  • dosen't work is not very useful because it does not describe the observed behavior ... what were you expecting client.connect() to return? – jsotola Jan 16 at 16:04
  • one of the values specified in the documentation, which would be: 1, -1, -2, -3 or -4. – Simon V. Jan 16 at 16:05
0

You need to provide more than just the IP address. The Arduino also needs to know the default router address in order to send packets to the right place.

Unfortunately there isn't a way of giving it that information without also giving it a DNS address, so you have to do that as well.

You need to have:

IPAddress gateway(192, 168, 1, 1);
IPAddress dns(8, 8, 8, 8);
IPAddress ip(192, 168, 1, 25);

And then in setup:

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

Alternatively you can rely on DHCP to give you all the details, and just use:

Ethernet.begin(mac);
| improve this answer | |
  • Unfortunately this does not resolve my issue – Simon V. Jan 16 at 16:08
  • I am assuming that your router is 192.168.1.1 - it may not be. You should make sure to use the right IP address there for your network. – Majenko Jan 16 at 16:09
  • the library defaults to gateway and dns calculated from provided IP (last number changed to 1) and net mask 255.255.255.0 – Juraj Jan 16 at 16:09
  • I have checked and I am indeed using the correct gateway. Would a powerline adapter influence this? I am using the gateway of the powerline adapter I'm connected to. – Simon V. Jan 16 at 16:11
0

The documentation is wrong. The connect function only returns 1 on success and 0 on fail. All examples have if (client.connect(host, port)) { (0 is false, 1 is true).

Try the WebClient example provided in the IDE Examples menu for the Ethernet library. It has better diagnostics of Ethernet link.

You can try the WebServer example. It doesn't need Internet connection.

You can try ping the IP address of the Ethernet shield.

The shield should be connected to a router or switch and the gateway IP address should point to a device routing the network traffic to Internet.

| improve this answer | |
  • Initialize Ethernet with DHCP: Failed to configure Ethernet using DHCP connecting to www.google.com... connection failed disconnecting. Received 0 bytes in 0.0000, rate = 0.00 kbytes/second is the response im getting – Simon V. Jan 16 at 16:18
  • this tells us that the shield is detected and the link is active. did you set the IP address and dns server address? – Juraj Jan 16 at 16:21
  • Yes, I set the ip, dns, and gateway to my own – Simon V. Jan 16 at 16:23
  • To a switch yes. Ping returns 0% loss. – Simon V. Jan 16 at 16:29
  • ok. try the WebServer example. it doesn't need Internet connection – Juraj Jan 16 at 16:30

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.