TCPClient failing to connect

Hi

I am trying to get a Photon to post data to my local server every 60 sec. The failure is in the TCPClient connect which every now and then returns false and the only way to recover is to reset the hardware. Has any one managed to get a photon reliably posting data ??. This is surely a rather common requirement of an internet connected device but I cannot get it reliably working.

@martinhoyle, you’ll need to post some code and describe your context a bit more if you want help!

ok
I call the following routine every 60 secs with a string to send. When it fails to connect the only option is to reset the device. It fails every 5 or 6 attempts. If I change the system mode to Manual it seems better.

byte buf[500];
int send_to_web(String s,bool flag)
        {
            
            String header;
            String request;
            String reply;
            String rc;
            TCPClient client;
            
            request = s;
            request.concat("\r\n") ;
            int len = request.length();
            String length =  String(len);
            if (flag == false)
            {
                header=head;
            }
            else
            {
                header=head2;
            }
            header.concat(length);
            header.concat("\r\n\n") ;
            header.concat(request);
            Serial.println(header);
          
            if (client.connect(server,80))
            {
              //Serial.print("connect");
                //client.print(header);
                header.getBytes(buf, strlen(header)) ;
                client.write( buf, strlen(header) );

                //client.print(request);
                while (client.connected())
                {
                    while (client.available())
                    {
                        char c = client.read();
                        rc=String(c);
                        reply.concat(rc);

                    }
                }
                
                
                client.flush();  //for safety
               
                client.stop();
                Serial.print(reply+ "\r\n");
                if ( reply.indexOf(done) >0 )
                    return 0 ;
                else
                {
                    Serial.print("No Done reply");
                    return -1 ;
                }
           }
           else
           {
             client.stop();
             Serial.print("Failed to connect");
             numberOfnotconnects++;
             System.reset();
             return -1;
           }

            return 0 ;
        }

@martinhoyle, I highly recommend NOT using Arduino String vars since they can cause memory fragmentation. Instead, use C strings (char arrays).

Also, you call TCPClient client; every time send_to_web() is called. This is the most likely cause of your trouble and you may want to move that to a global definition. :grinning:

1 Like

I initially had the client definition as a global definition but with the same result. I will try removing the String vars. Thanks for that

@martinhoyle, I would move the TCPClient to global regardless.

1 Like

If you are running AUTOMATIC how long could the while() actually keep running?
Try putting a Particle.process() in there and/or add a timeout.

1 Like

I have moved TCPClient to global and removed all String vars. Things are working much better now I only get about 1 failure every 2 hours ( I am posting every 60 Secs.). So it looks like the String vars was the problem perhaps there needs to be a note in the documentation that they are not recommended for use.

2 Likes