Hi all,
I'm a bit stumped. I've tried the following simple code:
#include <SPI.h>
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0x90, 0xA6, 0xEA, 0xA0, 0x13, 0xA7 };
//the IP address for the shield:
byte ip[] = { 192, 168, 1, 111};
// the router's gateway address:
byte gateway[] = { 192,168,1,1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server = EthernetServer(51);
void setup()
{
Serial.begin(9600);
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void oldloop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
void loop(){
int i;
//Serial.println(Ethernet.localIP());
// Create a client connection for the PUBLIC Web Server
EthernetClient cl = server.available();
if (cl == true) {
Serial.println("got a client...");
while (cl.connected()) {
if (cl.available()) {
Serial.println("got something avail...");
char c = cl.read();
//if HTTP request has ended
if (c == '\n') {
cl.println("HTTP/1.1 200 OK");
cl.println("Content-Type: text/html");
cl.println();
cl.println();
cl.println("<html>");
cl.println("<HEAD>");
cl.println("<TITLE>My Test Page</TITLE>");
cl.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">");
cl.println("<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">");
cl.println("</HEAD>");
cl.println("<body bgcolor=\"#EFF5D5\">");
cl.println("<h1><u><b>My Test Page</b></u></h1>");
cl.println("</body>");
cl.println("</html>");
cl.flush();
cl.stop();
break;
}
}
}
cl.flush();
cl.stop();
} else {
Serial.println("failed to get client...");
}
delay(1000);
} // loop
And all it does is spit out the failure message over and over that it can't get a client on the server. What I've tested is pinging the IP address in a dos prompt like this:
ping 192.168.1.111
and this works. So I know my ethernet shield is alive. If I remove the ethernet cable, then ping stops working. I'm using an ATMEGA2560 board, but I know it's flashing right since the "blink" test script works. Also, this whole same hardware did a fine web server prior to me upgrading to 1.0.1. I was on pre 1.0.0 before when it worked.
I am using as you can see the new api's like EthernetServer and EthernetClient. I'm quite confused why my server won't work now. Anyone have ideas?
Or does anyone have a simple web page sketch that works for them on 1.0.1 I can try out on my board?