1

I have an Arduino Uno with and Ethernet shield. There was a project which had it connecting to the network. It no longer does. Through debugging, it seems that Ethernet.begin(mac) == 0 never returns, causing a time out.

What could be causing this to be unable to connect?

The code hasn't changed, we updated the IP address of the server were attempting to connect to, and still no connection. The Ethernet cord also is working. I've also verified the MAC address on the shield. The server responds to pinging. No SD card either.

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x2F, 0x4D }; // Shield mac address
IPAddress server(10,0,9,124);                        // lunchbutton.dev.returnpath.net

const int BUTTON_PIN   = 2;
const int LED_SUCCESS  = 8;
const int LED_ERROR    = 9;
const int LED_WORKING  = 7;
const int LED_TIMEOUT  = 5000;
const int REFRESH_TIME = 7200000;

int buttonState    = 0;
boolean connected  = false;
unsigned long time = 0;

// 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 startSerial() {
    Serial.begin(9600); // Open serial communications
    while (!Serial);    // wait for serial port to connect. Needed for Leonardo only
}

boolean startEthernet() {
    // Serial.println("Trying to connect...");
    if (Ethernet.begin(mac) == 0) {
        // Serial.println("Failed to configure Ethernet using DHCP");
        return false;
    }
    // Serial.println("Connected");
    // give the Ethernet shield a second to initialize:
    delay(1000);
    return true;
}

boolean startConnection() {
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
        // Serial.println("connected");
        makeHttpRequest();
        connected = true;
        return true;
    }
    // kf you didn't get a connection to the server:
    // Serial.println("connection failed");
    showAll(true);
    startEthernet();
    showAll(false);
    return false;
}

void makeHttpRequest() {
    // Make a HTTP request:
    client.println("GET /mail.php HTTP/1.1");
    client.println("Host: lunchbutton.dev.returnpath.net");
    client.println();
}

void getStatus() {
    // if there are incoming bytes available
    // from the server, read them and print them:
    String status = "";
    boolean collectStatus = false;
    while (client.available()) {
        // Serial.println("Calling server...");
        char c = client.read();

        if (c == '\n') {
            if (collectStatus) {
                while (client.available()) {
                    c = client.read();
                    status += c;
                }

                showSuccess(status.substring(0,7) == "success");
                return;
            }
            else {
                collectStatus = true;
            }
        }
        else {
            if(c != '\r') {
                collectStatus = false;
            }
        }
    }
    //Serial.println("BAD");
}

void showWorking (boolean working) {
    if(working) { digitalWrite(LED_WORKING, HIGH); }
    else { digitalWrite(LED_WORKING, LOW); }
}

void showSuccess(boolean success) {
    if(success) {
        digitalWrite(LED_SUCCESS, HIGH);
        digitalWrite(LED_ERROR, LOW);
    }
    else {
        digitalWrite(LED_SUCCESS, LOW);
        digitalWrite(LED_ERROR, HIGH);
    }
    showWorking(false);
    delay(LED_TIMEOUT);
    digitalWrite(LED_SUCCESS, LOW);
    digitalWrite(LED_ERROR, LOW);
}

void showAll(boolean show) {
    if(show) {
        digitalWrite(LED_SUCCESS, LOW);
        digitalWrite(LED_ERROR, HIGH);
        digitalWrite(LED_WORKING, HIGH);
    }
    else {
        digitalWrite(LED_SUCCESS, LOW);
        digitalWrite(LED_ERROR, LOW);
        digitalWrite(LED_WORKING, LOW);
    }
}

void setup() {
    pinMode(BUTTON_PIN, INPUT);
    pinMode(LED_SUCCESS, OUTPUT);
    pinMode(LED_ERROR, OUTPUT);
    pinMode(LED_WORKING, OUTPUT);

    showAll(true);
    // startSerial();
    // Serial.println("Booting");
    startEthernet();
    showAll(false);
}

boolean checkDHCP() {
    //Serial.println("Checking DHCP");
    if (millis() - time > REFRESH_TIME) {
        // Serial.println("Trying to renewing DHCP...");
        showAll(true);
        boolean success = startEthernet();
        showAll(false);
        time = millis();
        // Serial.println("Renewed.");
        return success;
    }
    return true;
}

void loop() {
    buttonState = digitalRead(BUTTON_PIN); // get button state
    if ( !checkDHCP() ) { showSuccess(false); return; }

    // if button is pressed
    if (buttonState == HIGH && !client.connected()) {
        // Serial.println("Button Pressed");
        showWorking(true);
        if( !startConnection() ) { showSuccess(false); return; }
    }

    getStatus();

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

closed as off-topic by per1234, MatsK, gre_gor, jose can u c, Magic-Mouse Oct 6 '17 at 10:17

  • This question does not appear to be about Arduino, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Could you post the full code please? Ever the cliché, have you tried turning the router off and on? – CharlieHanson Jul 31 '15 at 18:17
  • Has your router MAC filtering turned on? – user11597 Aug 3 '15 at 20:53
  • 2
    I'm voting to close this question because any solution is never going to be accepted now the OP can't test it. – Code Gorilla Jul 31 '17 at 9:54
  • 4
    I'm voting to close this question as off-topic because OP can't longer check the answer – user31481 Sep 29 '17 at 11:35
0

There is one way I know to go around this problem (works for me)

Connect your ethernet shield to your computer and if you're using windows then bridge the arduino to the wifi (go to network and sharing center -> change adapter settings-> select ethernet (arduino) and wireless(wifi) -> right click and select bridge connections.

After bridging upload the sketch again.

This highly depends upon your internet connection speed. You can also check if your router/modem supports dhcp and if it does then is it enabled?

Another thing that might work is- Add some delay of about 5-10 seconds to allow the ethernet shield to obtain an ip address from the router/modem.

Do try this out and let me know

I want to thank the moderator to guide me.

  • Tell me! Did it work? – Manav Dec 30 '16 at 17:38
  • Im sorry, I no longer have access to the said arduino. – LikeTheRock Jan 3 '17 at 13:54

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