Webserver hangs/timeout

Arduino + ethernet shield + relay module + dht11

im sending on and off commands to my realy module via webserver but if i send too many request the server hang and doesnt repond to my ping

/*
Created by Benedict A. Acosta
Arduino with Ethernet Shield
*/
#include <DHT.h>
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#include <SPI.h>
#include <Ethernet.h>

int led = 4;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 111 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(8989); //server port
String readString;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
dht.begin();
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, subnet);
server.begin();
delay(10);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
delay(10);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}

// Create a client connection
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
delay(10); //timing delay else webserver hangs.
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("Benarduino 1.0");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("

Home Automation

");
client.println("
");
client.println("
");
client.println("

Arduino with Ethernet Shield

");
client.println("
");
client.println("

");
client.println("<a href="/?button1on"">On CO1");
client.println(" ");
client.println("<a href="/?button1off"">Off CO1
");
client.println("Limit the send of request every 10 seconds!");
client.println("

");
client.println("
");
// output the value of the DHT-11
client.println("

");
client.print("Humidity: ");
client.println("

");
client.println("

");
client.println("

");
client.print(h);
client.print(" %\t");
client.print("");
client.println("

");
client.println("

");
client.println("

");
client.print("Temperature: ");
client.println("

");
client.println("

");

client.print(t*1.8+32);
client.println(" °");
client.println("F");

client.println("
");
client.print(t);
client.println(" °");
client.println("C");
client.println("
");
client.println("

");
client.println("
");

client.println("

Note: This page refreshes automatically every 8 seconds
to update temperature and humidity readings.

");

client.println("");
client.println("");
/*condition of sensor */

if (c == '\n') {
// you're starting a new line

}
else if (c != '\r') {
// you've gotten a character on the current line
}
/* end of condition- sensor */
delay(10);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
delay(10);
digitalWrite(led, LOW);
}
if (readString.indexOf("?button1off") >0){
delay(10);
digitalWrite(led, HIGH);
}

//clearing string for next read
readString="";

}
}
}
}
}

This is not correct.

// change this
 Ethernet.begin(mac, ip, subnet);
// to this
 Ethernet.begin(mac, ip, gateway, gateway, subnet);

You might be using all your sockets. Add this to your sketch and call it after each client connection. If you do not have one socket with a status of 0x14, the w5100 will not accept any connections. If there is not a socket with a status of 0x0, it may not respond to a ping.

#include <utility/w5100.h>

byte socketStat[MAX_SOCK_NUM];

void ShowSockStatus()
{
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    Serial.print(F("Socket#"));
    Serial.print(i);
    uint8_t s = W5100.readSnSR(i);
    socketStat[i] = s;
    Serial.print(F(":0x"));
    Serial.print(s,16);
    Serial.print(F(" "));
    Serial.print(W5100.readSnPORT(i));
    Serial.print(F(" D:"));
    uint8_t dip[4];
    W5100.readSnDIPR(i, dip);
    for (int j=0; j<4; j++) {
      Serial.print(dip[j],10);
      if (j<3) Serial.print(".");
    }
    Serial.print(F("("));
    Serial.print(W5100.readSnDPORT(i));
    Serial.println(F(")"));
  }
}

A status list:
0x0 = available
0x14 = waiting for a connection
0x17 = connected
0x1C = connected waiting for close
0x22 = UDP

I guess your code needs optimisation, Arduino has a limited SRAM.

Hi

You need to use the flash string function to save your SRAM.

Convert this:

client.println("

Arduino with Ethernet Shield

");

to this:

client.println(F("

Arduino with Ethernet Shield

"));

as shown in SurferTims response.

If you are running out of SRAM this will help a lot. Google Arduino freeRAM to find a procedure to monitor your system free SRAM during runtime.

Cheers

Catweazle NZ

you are also using the string library. i originally used the string library in my projects which required continuous operation for years on end. i noticed that the arduino would crash/hang. it turns out that over time it is possible for the string library to not properly clear out memory and you get over flows. i removed the string library entirely and now use only char arrays, and my projects run (with ethernet access) for many months without issue.

the record i have to date was continuous operation for 13 months until i lost power to my entire hour :confused:

while using strings i was lucky to go a few weeks.

Hi

Any application that does not use global Strings and does use functional decomposition will likely be able to use Strings freely within individual procedures provided you have controls in place so avoid very long String coming in from external sources.

My application at www.2wg.co.nz uses Strings extensively and works just fine.

Strings are just dynamic objects and it is ok to use dynamic objects with care in Arduino applications. Objects like files and Ethernetclient objects. When you understand how the heap works you learn how to avoid heap fragmentation when using dynamic objects.

Cheers

Catweazle NZ

Convert this:

client.println("

Arduino with Ethernet Shield

");

to this:

client.println(F("

Arduino with Ethernet Shield

"));

The down side to using the F() macro a lot is that apparently each byte sent is put into an individual tcp/ip packet, slowing the upload speed.

been so long since i've check my account. so do i have to convert all the html tags with that F()?