I'm having problems troubleshooting an issue with the Ethernet Shield on a MEGA. I'm successfully getting an IP through DHCP. I can ping the address, but when I try to connect to the page through the browser, it fails to connect. I would greatly appreciate any suggestions you may have...I've been banging my head against the wall on this for awhile.
My code is as follows:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x14, 0x30, 0xC6, 0x2D, 0xE2, 0x90 };
EthernetServer server(80);
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Ethernet.begin(mac);
server.begin();
Serial.begin(9600);
Serial.print("Server IP: ");
Serial.println(Ethernet.localIP());
Serial.print("Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
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");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Hello from Arduino!</h1>");
client.println("<p>A web page from the Arduino server</p>");
client.println("</body>");
client.println("</html>");
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
That code gives me the response:
Server IP: 192.168.2.5
Gateway IP: 192.168.2.1
Like I said, I get a response when I ping 192.168.2.5, but when I go to it in my browser, it fails to connect. I've tried ports other than 80. I've tried it on my UNO with the same results. Not sure what the issue could be. Thanks for any insight you may have!
I've tried setting PIN 10 to OUTPUT (tried both LOW and HIGH - I think it should be LOW), but get the same result. I still successfully set the IP address and can ping it, but cannot connect.
As for PIN 4, I have read that the SD interface can interfere with the Ethernet and setting PIN 4 as OUTPUT and setting it HIGH turns the SD interface off, so I did that just to be safe. When I comment the PIN 4 lines out, I also get the same result.
The ethernet shield is connected directly to the router. The PC I'm trying to connect with is on the same network. Both show up on my router's DHCP client list:
I'm seeing the Arduino (as WizNet W5100) at 192.168.2.5
The PC I'm trying to connect with is at 192.168.2.4
And my router, of course, is 192.168.2.1
So the router seems to know the Arduino is at that address (presumably the router assigned it that address).
The fact that "Ethernet.localIP()" returns "192.168.2.5" suggests to me the Arduino is actually using that address. Is there another way to confirm that?
Thanks again for your suggestions. I appreciate the help.
The fact that "Ethernet.localIP()" returns "192.168.2.5" suggests to me the Arduino is actually using that address. Is there another way to confirm that?
Since you don't assign the value in the code, we must assume that the Arduino IS talking to the router.
Now, the question is does the router have port-forwarding enabled, and is it forwarding requests for port 80 to the Arduino?
I'd add some more Serial.print() statements to the Arduino code, to see what, exactly, is happening, if anything is.
I did setup forwarding of Port 80 on my router for the Arduino, but get the same result.
When I add a Serial.println() inside the if(client) block, there is no output when I try to connect.
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x14, 0x30, 0xC6, 0x2D, 0xE2, 0x90 };
EthernetServer server(80); // create a server at port 80
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
Ethernet.begin(mac); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600);
Serial.print("Server IP: ");
Serial.println(Ethernet.localIP());
Serial.print("Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) {
Serial.println("Got Client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Hello from Arduino!</h1>");
client.println("<p>A web page from the Arduino server</p>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
That code still gives me this in the terminal:
Server IP: 192.168.2.5
Gateway IP: 192.168.2.1
...but I never see "Got Client" when I try to connect.
A little more information...when I try to connect to the Arduino's IP address (192.168.2.5), the browser responds fairly quickly with an "Unable to connect" page. This is different than when I go to an unused IP address in my LAN, such as 192.168.2.10, which takes awhile to respond and then gives me a "Connection has timed out" page.
That tells me the browser is seeing the Arduino in some capacity, but isn't the Arduino isn't receiving the client.
Alright, I have it working. For some reason I can only connect to the Arduino when my PC is wired to the router, and not over wireless (even though I'm on the LAN side in both cases). It seems as if this must be a router issue and not an Arduino issue, so apologies for the wild goose chase.
Thanks for the help with the troubleshooting. It is truly appreciated.