Mega 2560 Ethernet connection - unreliable connections

Hi All,

I am using the below code to turn a relay on and off

I am having a problem as the server will refuse connection every now and again i can refresh 40 times or so and it will work every time but then sometime it wont get a response!

Any ideas why this would be I want it to always accept the connections made to it and respond.

Using a MEGA 2560 (MEGA ADK) and an Ethernet shield I have also tested the code on the UNO and it does the same.

#include <SPI.h>
#include <Ethernet.h>

EthernetServer server(5411);// Server port

byte mac[] = { 0x00, 0x08, 0xDC, 0xAB, 0xCD, 0xEF };// Physical MAC address
byte ip[] = { 192, 168, 0, 125 };// Fixed IP address
byte gateway[] = { 192, 168, 0, 1 };// Router Gateway Internet access
byte subnet[] = { 255, 255, 255, 0 };// Subnet mask

const int relay1 = 14; pin 14

String readString;

void setup()
{
  pinMode(relay1, OUTPUT); // this is relay 1


  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //enable serial data print
  // Serial.begin(9600);

}




void loop()
{

  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 200) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          /* Start OF HTML Section. Here Keep everything as it is unless you understands its working */
          client.println(F("HTTP/1.1 200 OK")); //send new page
          client.println(F("Content-Type: text/html"));
          client.println();

          //start relay 1
          if (readString.indexOf("?r1on") > 0) 
          {
            digitalWrite(relay1, HIGH);             
          }


          if (readString.indexOf("?r1off") > 0) 
          {
            digitalWrite(relay1, LOW );    
          }

          //start
          if (digitalRead(relay1))
          {
            //on
            client.print(F("r1on,"));
          } else {
            //off
            client.print(F("r1off,"));
          }

          //end

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

          //stopping client
          //client.print('A');


          delay(1);

          client.stop();
          Serial.println("client disconnected");


        }


      }


    }


  }


}

Cheers James

the server will refuse connection every now and again

How do you know it refuses the connexion?

If I were you I would get rid of the String class...

Hi,

I just do a HTTP request to the server to return the data and it sometimes times out its quite regular at timing out as well.

if I remove remove String class what would I have to alter in my code?

Would I use CHAR instead? not sure how can you point me in the right direction?

You should use cStrings - ie null terminated char arrays

--> instead of String readString; you would have a char readString[100];
--> instead of readString += c; you would need to maintain an index in the array where to store the character (and a trailing null char) and you need to check you are still within bounds (ie if the array can hold 100 char, as you need to keep one for the trailing '\0' you can only store 99 elements - from index 0 to 98)
--> instead of readString.indexOf() you need to use the cString [url=http://www.cplusplus.com/reference/cstring/strstr/]strstr()[/url] function (see what exists in stdlib.h or string.h)

the String class can poke holes in your memory and after a while there is no memory left for allocation when you do readString += c;... When you see erratic behavior on a program, that could be a cause... Not saying this is your problem, but could be

Hi,

Thanks for the reply i'll look into that possibly over my skill level at the moment.

Do you know of any example sketches that do not use string for the Ethernet web server?

I posted a while back a small tutorial in french to demonstrate how this could work.. may be with the help of google translate (if you don't speak french) you can make sense of it? (or just have a look at the code)

Hi J-M-L,

Thank you very much for that.

I have just tested the code that I posted using my internal IP address and it does not timeout

I checked my router firewall but this is off.

Do you know of any reason this might happen?