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();
}
}