I am using the heavily plagiarized code below. It works, but is not dependable. After a period of usage the web server will just hang and not respond. On some occasions the web browser will fill with garbage characters. I can still ping the Arduino when the hang occurs, but the only way to get the web server to respond again is to reboot. Do I need some judiciously placed delay statements? Any advice would be appreciated. Am I expecting too much for this to work dependably? Thanks.
#include <SPI.h>
#include <Ethernet.h>
int ch00 = 4;
int ch01 = 5;
int ch02 = 6;
int ch03 = 7;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xA5, 0x9C };
byte ip[] = { 192, 168, 0, 200 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
String readString;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
pinMode(ch00, OUTPUT);
pinMode(ch01, OUTPUT);
pinMode(ch02, OUTPUT);
pinMode(ch03, OUTPUT);
digitalWrite(ch00, LOW);
digitalWrite(ch01, LOW);
digitalWrite(ch02, LOW);
digitalWrite(ch03, LOW);
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 24)
{
readString += c;
}
if (c == '\n')
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("Relay Control");
client.println("");
client.println("");
client.println("
Relay Control
");client.println("
Arduino
");client.println("
");
client.println("<a href="/?ch00on"">Turn CH00 On");
client.println("<a href="/?ch00off"">Turn CH00 Off
");
client.println("
");
client.println("
");
client.println("<a href="/?ch01on"">Turn CH01 On");
client.println("<a href="/?ch01off"">Turn CH01 Off
");
client.println("
");
client.println("
");
client.println("<a href="/?ch02on"">Turn CH02 On");
client.println("<a href="/?ch02off"">Turn CH02 Off
");
client.println("
");
client.println("
");
client.println("<a href="/?ch03on"">Turn CH03 On");
client.println("<a href="/?ch03off"">Turn CH03 Off
");
client.println("
");
client.println("");
client.println("");
client.stop();
if (readString.indexOf("?ch00on") >0)
{
digitalWrite(ch00, HIGH);
}
if (readString.indexOf("?ch00off") >0)
{
digitalWrite(ch00, LOW);
}
if (readString.indexOf("?ch01on") >0)
{
digitalWrite(ch01, HIGH);
}
if (readString.indexOf("?ch01off") >0)
{
digitalWrite(ch01, LOW);
}
if (readString.indexOf("?ch02on") >0)
{
digitalWrite(ch02, HIGH);
}
if (readString.indexOf("?ch02off") >0)
{
digitalWrite(ch02, LOW);
}
if (readString.indexOf("?ch03on") >0)
{
digitalWrite(ch03, HIGH);
}
if (readString.indexOf("?ch03off") >0)
{
digitalWrite(ch03, LOW);
}
readString = "";
}
}
}
}
}