Connection with Tcp Server

Hello,

I am using Arduino Uno with ethernet shield W5100 to connect with my Tcp Server. Currently I my server is sending message to Arduino in every 60 seconds to check state. After connection with internet it's working fine, but after several minutes server can't send information to Arduino. Below is my code. Where is problem? I have diffrent server connected with Tcp Server and it's working fine.

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

String id = "id";
String state = "State/";

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

char server[] = "my-server-server.net";

EthernetClient client;

String readString = "";

void setup()
{
  Ethernet.begin(mac);
  Serial.begin(9600);
  Serial.println("Connecting...");

  if (client.connect(server, 1337))
  {
    Serial.println("Arduino connected with TCP server");
    char deviceConnected[25];
    deviceId.toCharArray(deviceConnected, 25);
    client.write(deviceConnected);
    delay(1000);
  }
  else
  {
    Serial.println("Connection failed.");
    delay(1000);
    setup();
  }
}

void loop()
{
    while (client.connected())
    {
      while (client.available())
      {
        char c = client.read();
        readString += c;

        if (c == '/')
        {
          if (readString == state)
          {
            Serial.println("working");
            client.write("working");
            Serial.println(client);
          }
          Serial.println(readString);
          readString = "";
        }
      }
    }

    Serial.println();
    Serial.println("Disconnecting.");
    client.stop();
    delay(1000);
    Serial.println("Disconnected.");
    setup();
}

It's a bad idea to call setup() from inside of setup(). That could be the cause of your problem.

What sort of output are you seeing in the Serial Monitor before it gets to the "can't send information" state?

I got this info in Serial monitor:

Connecting...
Arduino connected with TCP server
working
1
State/

Then several times I am getting info:

working
1
State/

Then server can not connect with Arduino and Arduino don't show more information and don't print:

working
1
State/

And I removed all calls to setup(). It's the same result. Arduino is connected with TcpServer, receive several packages and then is frozen. It's not disconnected but not receive new data too.

So it never once gets to "Disconnecting.", "Disconnected." the whole time?

My advice is to add in some additional Serial.println statements to get more information about what's happening.

Piotrek50501:
And I removed all calls to setup().

You can call setup() from loop. It's just a bad idea to call setup from inside setup because you could easily have a situation where that happens over and over again until you run out of memory and crash the processor. You could accomplish the same thing safely with a while loop.

Rather than calling setup from loop, I would probably move the code that needs to be run again after a disconnect to a separate function and then call that function from both setup and loop. That way you don't unnecessarily run the code that only ever needs to be called once. For example, there is absolutely no reason you would need to run Serial.begin(9600) multiple times in your code.

don't use String. it fragments the memory with small pieces until there is no space for a next small piece

I will try today evening.
So how can I use without String?

I am using String to get information from:

char c = client.read();

And I am adding new sign to string to readString

And then I am comparing two strings:
String state = "State/"; with String readString = "";

How can I do it without String?

Piotrek50501:
I will try today evening.
So how can I use without String?

I am using String to get information from:

char c = client.read();

And I am adding new sign to string to readString

And then I am comparing two strings:
String state = "State/"; with String readString = "";

How can I do it without String?

it depends what do you want to do with the string.

I would like to compare the string of characters which I get from the server. And if I will have state = "String/" and readString = "String/" then I would like to execute code from

if (readString == state)

I don't know how to do it in the case without String. So How can I add next sign to readString variable when this variable isn't as String?

I found the problem. Problem is with my Tcp Server. I am hosting my server on mydevil.net and several minutes ago I was trying connect my arduino with local server. With local server is working correctly.

So I have to change server. Where can I host my server?

Piotrek50501:
I don't know how to do it in the case without String.

If the two are strings (not Strings), you can use strcmp or strncmp:
http://www.cplusplus.com/reference/cstring/strcmp/

Piotrek50501:
I found the problem. Problem is with my Tcp Server. I am hosting my server on mydevil.net and several minutes ago I was trying connect my arduino with local server. With local server is working correctly.

So I have to change server. Where can I host my server?

does the server use https? copy the response bytes to Serial Monitor to see the error.

I am getting only:

Connecting...
Arduino connected with TCP server
working
Status/

working
Status/

working
Status/

working
Status/

working
Status/

working
Status/

and this part is showing always and then the app is frozen (Arduino). Always I receive "working" and "Status/" 6 times.

Server isn't with https. He is http.

I got in server log:
App 94077 output: Catched error for TCP server:
App 94077 output: ERR.STACK------------------------
App 94077 output: Error: read ETIMEDOUT
App 94077 output: at _errnoException (util.js:992:11)
App 94077 output: at TCP.onread (net.js:618:25)
App 94077 output: ---------------------------------
App 94077 output: Socket closed

port 1337? it could be blocked on firewall in router?

Now I changed router, I added in Router Settings for "Access Control" in firewall tab "Allow" for all. The result is the same. So is strange that I got always six times:

working
Status/

In router log I had nothing.

first make the sketch normal. don't call setup() from loop().

you want always reconnect, or you want hold the socket open?

char buff[50];
int l = client.readBytesUntil('/', buff, sizeof(buff));
buff[l] = 0;
if (strcmp(buff, "Status") == 0) {
  Serial.println(buff);
  cllient.print(buff); // echo
  client.flush();
}

I want hold the socket open.

At this moment I'm using below code:

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

int value = 0;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

 IPAddress server(xxx,xxx,xxx,xx); // here I have server IP

EthernetClient client;

void setup()
{
  Ethernet.begin(mac);
  Serial.begin(9600);
  Serial.println("Connecting...");

  if (client.connect(server, 1337))
  {
    Serial.println("Arduino connected with TCP server");
    delay(1000);
  }
  else
  {
    Serial.println("Connection failed.");
    delay(1000);
    Serial.println("I will try again...");
    delay(2000);
  }
}

void loop()
{
if (client.available()) {
    char c = client.read();
    Serial.print(c);
    if (c == '/')
    {
      value += 1;
      Serial.println(value);
    }
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

And the problem is the same.

I noticed that connection between Arduino and server have always around 6 minutes. But I don't know why.

Piotrek50501:
I noticed that connection between Arduino and server have always around 6 minutes. But I don't know why.

reconnect if it is not connected

I was trying through last 2 hours with my code and with the local server. It's working correctly. But with external hosting is a problem.

I think that at this moment it isn't important my code for Arduino. I know that I have to make it better, but now I would like to resolve the problem with the connection between my hosting and Arduino.

Maybe my hosting has a lot of problems? Maybe I have to change hosting company and try to do it there?

Could you recommend me any hosting company to testing my server with connected Arduino?