Problem with Ethernet shield

Hello,

I'm making website controlled lights. I made basic script which turn on and off led light(pin4)
I have arduino and ethernet shield from ebay.I have troubles after conenct it to power(ethernet is always connected)
It works 5-10minutes sometims one minute. aFter this time it no detect change in file on my site.
Code:

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 0, 110 };
    byte dnss[] = { 8, 8, 8, 8 };

    EthernetClient client;
    char server[] = "kaska3er.ugu.pl";
    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    String location = "/siema.txt HTTP/1.0";



    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
      Ethernet.begin(mac, ip, dnss);
      Serial.begin(9600);
      pinMode(6, OUTPUT);
    }

    void loop(){
      String pageValue = connectAndRead(); //connect to the server and read the output
      Serial.println(pageValue); //print out the findings.
      if(pageValue == "wlacz")
      {
        digitalWrite(6, HIGH);
      }
      else if(pageValue == "wylacz")
      {
        digitalWrite(6, LOW);
      }
      delay(5000); //wait 5 seconds before connecting again
    }

    String connectAndRead(){
      //connect to the server

      Serial.println("connecting...");

      //port 80 is typical of a www page
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
        client.println("Host: kaska3er.ugu.pl");
        client.println();

        //Connected - Read the page
        return readPage(); //go and read the output

      }else{
        return "connection failed";
      }

    }

    String readPage(){
      //read the page, and capture & return everything between '<' and '>'

      stringPos = 0;
      memset( &inString, 0, 32 ); //clear inString memory

      while(true){

        if (client.available()) {
          char c = client.read();

          if (c == '<' ) { //'<' is our begining character
            startRead = true; //Ready to start reading the part
          }else if(startRead){

            if(c != '>'){ //'>' is our ending character
              inString[stringPos] = c;
              stringPos ++;
            }else{
              //got what we need here! We can disconnect now
              startRead = false;
              client.stop();
              client.flush();
              Serial.println("disconnecting.");
              return inString;

            }

          }
        }

      }

    }

Sometimes it can work 5-6h.But I need it works 24/7/365.
If it not work only RX led blink sometimes.
But if it work RX and TX blinks fast every 5 sec(delay(5000)

Please help me fast and sorry for my english

The use of String class confused me.
The 'location' is a String, a function returns a String, and you use an index [] of a String.

Is it possible to remove all String and use simple char buffer[] arrays ?
The return value of a function can be an integer (for example an error value).

NEWCONTENT!!!

I change code to this:

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 0, 110 };
    byte dnss[] = { 8, 8, 8, 8 };

    EthernetClient client;
    char outputred[32];

    char server[] = "kaska3er.ugu.pl";
    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    char location[] = "/siema.txt HTTP/1.0";



    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
      Ethernet.begin(mac, ip, dnss);
      Serial.begin(9600);
      pinMode(6, OUTPUT);
    }

    void loop(){
      char *pageValue = connectAndRead(); //connect to the server and read the output
      Serial.println(pageValue); //print out the findings.
      if(strcmp(pageValue, "wlacz") == 0)
      {
        digitalWrite(6, HIGH);
      }
      else if(strcmp(pageValue, "wylacz") == 0)
      {
        digitalWrite(6, LOW);
      }
      delay(5000); //wait 5 seconds before connecting again
    }

    char *connectAndRead(){
      //connect to the server

      Serial.println("connecting...");

      //port 80 is typical of a www page
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
        client.println("Host: kaska3er.ugu.pl");
        client.println();

        //Connected - Read the page
          return readPage(); //go and read the output


      }else{
           return "connectfailed";
      }

    }

    char *readPage(){
      //read the page, and capture & return everything between '<' and '>'

      stringPos = 0;
      memset( &inString, 0, 32 ); //clear inString memory

      while(true){

        if (client.available()) {
          char c = client.read();

          if (c == '<' ) { //'<' is our begining character
            startRead = true; //Ready to start reading the part
          }else if(startRead){

            if(c != '>'){ //'>' is our ending character
              inString[stringPos] = c;
              stringPos ++;
            }else{
              //got what we need here! We can disconnect now
              startRead = false;
              client.stop();
              client.flush();
              Serial.println("disconnecting.");
              return inString;

            }

          }
        }

      }

    }

I changed Strings to chars. It compiles good but i didn't test it.

There is still a mix between String and char buffer[] array.

The 'pageValue' is a char buffer, but you put a String into it from connectAndRead().
Also the readPage() returns a String class, but in that function you return a pointer to a global char buffer.

I think you have mixed two examples, one with the String class and one with char buffer[] array.
Ofcourse, it is possible to use the String class, it makes a smaller an better to read code. But in your sketch with the mix of them, I suggest to use good old char buffer[] arrays.

I connected it and it work very well. I write a post on the morning because i leave it powered for a night.
Sorry for my bad code because i'm 13 years old :smiley:

Can I remove delay(5000) in void loop or do it smaller 1-2sec?

Ok I have a mistake. It works very well for 20min but after it it stop works.
I connect it to PC and I read serial output.
Serial:

connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected
disconnecting.
wlacz
connecting...
connected

It freezes after connect if serial output give me truth.
Please help me fast

I debugged it deeper and i check it freezes in loop;

 while(true){
        Serial.println("checking123");
        if (client.available()) {

If it freezes it spam serial message "checking123" so client.available is false.
Please help me

Sorry, I don't know yet.
Do you use the newest Arduino IDE version 1.0.5 or 1.5.7 BETA ?

I could be a missing connection due to ethernet or a router. At least make something so it can get out of the endless while-loop when the client is no longer available.

I'm not sure about your code, but I noticed that you do a flush after a stop. I think you should flush first and stop after that.

I'm using arduino 1.0.6

Full code:

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 0, 110 };
    byte dnss[] = { 8, 8, 8, 8 };

    EthernetClient client;
    char outputred[32];

    char server[] = "kaska3er.ugu.pl";
    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    char location[] = "/siema.txt HTTP/1.0";



    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
      Ethernet.begin(mac, ip, dnss);
      Serial.begin(9600);
      pinMode(6, OUTPUT);
    }

    void loop(){
      char *pageValue = connectAndRead(); //connect to the server and read the output
      Serial.println(pageValue); //print out the findings.
      if(strcmp(pageValue, "wlacz") == 0)
      {
        digitalWrite(6, HIGH);
      }
      else if(strcmp(pageValue, "wylacz") == 0)
      {
        digitalWrite(6, LOW);
      }
      delay(5000); //wait 5 seconds before connecting again
    }

    char *connectAndRead(){
      //connect to the server

      Serial.println("connecting...");

      //port 80 is typical of a www page
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
        client.println("Host: kaska3er.ugu.pl");
        client.println();

        //Connected - Read the page
          return readPage(); //go and read the output


      }else{
           return "connectfailed";
      }

    }

    char *readPage(){
      //read the page, and capture & return everything between '<' and '>'
      stringPos = 0;
      memset( &inString, 0, 32 ); //clear inString memory
      while(true){
        if (client.available()) {
          char c = client.read();
          if (c == '<' ) { //'<' is our begining character
            startRead = true; //Ready to start reading the part
          }else if(startRead){

            if(c != '>'){ //'>' is our ending character
              inString[stringPos] = c;
              stringPos ++;
            }else{
              //got what we need here! We can disconnect now
              startRead = false;
              client.flush();
              client.stop();
              Serial.println("disconnecting.");
              return inString;

            }

          }
        }

      }

    }

My internet connection is good i use Facebook,skype at the same time without problem.
I'm testing now option with first flush then stop

This line : memset( &inString, 0, 32 );
should be : memset (inString, 0, 32);
since inString is now a normal buffer.

I leave it powered for night and it isn't working now(8:20).I connect it now and change memset function and leave it with open serialmonitor.

There is one more thing....
You do this: return "connectfailed";
I think that is not good programming. That string is locally inside the function, and yet, you bring a pointer to it outside that function. I think it is better to either copy that string or use a global string.

I prefer this:

// global strings.
char msgConnectionFailed[] = "connectfailed";
char server[] = ...
char location[] = ...

...
char *readPage(){
  ...
    return (msgConnectionFailed);

I tested it with changed memset and it work only for 15 minutes.Error is in the same code(client.available).
Now i'm testing with changed return "" to return (msgConenctionFailed)
Code actually looks that:

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 0, 110 };
    byte dnss[] = { 8, 8, 8, 8 };

    EthernetClient client;
    char outputred[32];
    char msgConnectionFailed[] = "connectfailed";
    char server[] = "kaska3er.ugu.pl";
    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    char location[] = "/siema.txt HTTP/1.0";



    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
      Ethernet.begin(mac, ip, dnss);
      Serial.begin(9600);
      pinMode(6, OUTPUT);
    }

    void loop(){
      char *pageValue = connectAndRead(); //connect to the server and read the output
      Serial.println(pageValue); //print out the findings.
      if(strcmp(pageValue, "wlacz") == 0)
      {
        digitalWrite(6, HIGH);
      }
      else if(strcmp(pageValue, "wylacz") == 0)
      {
        digitalWrite(6, LOW);
      }
      delay(5000); //wait 5 seconds before connecting again
    }

    char *connectAndRead(){
      //connect to the server

      Serial.println("connecting...");

      //port 80 is typical of a www page
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
        client.println("Host: kaska3er.ugu.pl");
        client.println();

        //Connected - Read the page
          return readPage(); //go and read the output


      }else{
           return (msgConnectionFailed);
      }

    }

    char *readPage(){
      //read the page, and capture & return everything between '<' and '>'
      stringPos = 0;
      memset(inString, 0, 32 ); //clear inString memory
      while(true){
        Serial.println("poczatek petli");
        if (client.available()) {
          char c = client.read();
          if (c == '<' ) { //'<' is our begining character
            startRead = true; //Ready to start reading the part
          }else if(startRead){

            if(c != '>'){ //'>' is our ending character
              inString[stringPos] = c;
              stringPos ++;
            }else{
              //got what we need here! We can disconnect now
              startRead = false;
              client.flush();
              client.stop();
              Serial.println("disconnecting.");
              return inString;

            }

          }
        }

      }

    }

The problem might be in the code flow, but I can't test it myself at the moment. Sorry.

Did you make something to get out of that while-loop, if the client is not available ?

I've added code in readPage function. It return error message if client is not available. Now i'm testing it.

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 0, 110 };
    byte dnss[] = { 8, 8, 8, 8 };

    EthernetClient client;
    char outputred[32];
    char clienterr[] = "error";
    char msgConnectionFailed[] = "connectfailed";
    char server[] = "kaska3er.ugu.pl";
    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    char location[] = "/siema.txt HTTP/1.0";



    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
      Ethernet.begin(mac, ip, dnss);
      Serial.begin(9600);
      pinMode(6, OUTPUT);
    }

    void loop(){
      char *pageValue = connectAndRead(); //connect to the server and read the output
      Serial.println(pageValue); //print out the findings.
      if(strcmp(pageValue, "wlacz") == 0)
      {
        digitalWrite(6, HIGH);
      }
      else if(strcmp(pageValue, "wylacz") == 0)
      {
        digitalWrite(6, LOW);
      }
      delay(5000); //wait 5 seconds before connecting again
    }

    char *connectAndRead(){
      //connect to the server

      Serial.println("connecting...");

      //port 80 is typical of a www page
      if (client.connect(server, 80)) {
        Serial.println("connected");
        client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
        client.println("Host: kaska3er.ugu.pl");
        client.println();

        //Connected - Read the page
          return readPage(); //go and read the output


      }else{
           return (msgConnectionFailed);
      }

    }

    char *readPage()
    {
        //read the page, and capture & return everything between '<' and '>'
        stringPos = 0;
        memset(inString, 0, 32 ); //clear inString memory
        while(true)
        {
          Serial.println("poczatek petli");
          if (client.available()) 
          {
            char c = client.read();
            if (c == '<' ) 
            { //'<' is our begining character
              startRead = true; //Ready to start reading the part
            }
            else if(startRead)
            {
              if(c != '>')
              { //'>' is our ending character
                inString[stringPos] = c;
                stringPos ++;
              }
              else
              {
                //got what we need here! We can disconnect now
                startRead = false;
                client.flush();
                client.stop();
                Serial.println("disconnecting.");
                return inString;
              }
            }
          }
          else
          {
            return (clienterr);
          }
  
        }

    }

I run it but i have problem. Monitor get spammed by messages like this:

poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
poczatek petli
disconnecting.
wlacz
connecting...
connected
poczatek petli
error
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed
connecting...
connectfailed

poczatekpetli means message before if(client.available

You print the message "poczatek petli" for every single character that is received.
Perhaps you can print only the character that was received, so you can see what is received.

"poczatek petli" is before if(client.available:

stringPos = 0;
      memset(inString, 0, 32 ); //clear inString memory
      while(true){
Serial.println("poczatek petli");
        if (client.available()) {

I don't know why i can't connect and why it doesn't work :frowning:
Please help me

You print "poczatek petli" for every single character that is received.
Perhaps that causes a timeout for the ethernet.
According to the messages in the serial monitor, I think it works for the first time, but fails after that.

So what can I do?
I need it working 24/7 without errors because this is home automation system.
My internet connection is slow but stable