Really sporadic TCP transfer times

TCPClient TCP;
char rxbuf[512];
void sendGetRequest (const char \*server, const char \*url)
{
   unsigned long s = millis();
   unsigned long e;
   unsigned long count = 1;
   unsigned long entry = s;

   Serial.println ("sendGetRequest:");
   if (TCP.connect (server, 80))
   {
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.print ("GET ");
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.print (url);
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.println ("HTTP/1.0");
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.println ("Connection: close");
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.print ("Host: ");
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.println (server);
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.println ("Accept: text/html, text/plain");
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.println ();
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
      TCP.flush();
      e = millis(); Serial.println ("ET = " + String(e-s) + "(" + String(count++) + ")"); s = e;
   }
   Serial.println ("Total time = " + String(e - entry));
}

void loop (void)
{
   unsigned long response;
   sendGetRequest ("www.random.org", "/integers/?num=1&min=1&max=100&col=6&base=10&format=plain");
   TCP.stop();
   delay(2000);
}

Sometimes this code reports an elapsed time of < 1 second.
Much of the time (easily over 50%) it’s over 15 seconds.

I’m doing this as a test, because I downloaded the HttpClient that someone was kind enough to upload, and I did the same kinds of measurements on that code and I found that it was ALWAYS taking over 15 seconds.

Doing a similar request in a browser takes < 2 seconds, every time.

I’m doing this because I need to do some POSTs to a server, and I need to do them much more frequently than every 15 seconds.

Is there some misunderstanding reflected in this code?

Usually, the println() by itself, just before the flush(), is the one take takes many seconds (call it just under 15 seconds).

I can give a detailed log if it’s necessary.

Thanks!

Hi @mgssnr

I don’t know what is going on for you but I am able to get response times back from that web page under 1 second if I flush the data. Here is my test program:

const char server[] = "www.random.org";
const char url[] = "/integers/?num=1&min=1&max=100&col=6&base=10&format=plain";
TCPClient myTCP;
char publishString[40];

void setup() {

}

void loop() {
    unsigned long tic = millis();
    sendGetRequest(server, url);
    while (!myTCP.available()) {}
    myTCP.stop();
    unsigned long toc = millis();
    sprintf(publishString, "%lu", toc-tic);
    Spark.publish("URLTime",publishString);
    delay(10000); //be nice every 10 seconds
}

void sendGetRequest(const char * server, const char * url)
{
    if (myTCP.connect(server, 80)) {
        //Serial1.print("Connected to Server");
        digitalWrite(D7,HIGH);
        myTCP.print("GET ");
        myTCP.print(url);
        myTCP.println(" HTTP/1.0");
        myTCP.println("Connection: close");
        myTCP.print("Host: ");
        myTCP.println(server);
        myTCP.println("Accept: text/html, text/plain");
        myTCP.println();
        myTCP.flush();
    } 
}

And here is the output of the publish stream–as you can see requests take around 750-1000 milliseconds in the “data” field, when I wait for the first data to come back, but then flush the data instead of reading it as shown in the code above.

event: URLTime
data: {"data":"844","ttl":"60","published_at":"2014-03-17T02:54:09.593Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"824","ttl":"60","published_at":"2014-03-17T02:54:20.420Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"746","ttl":"60","published_at":"2014-03-17T02:54:31.172Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"776","ttl":"60","published_at":"2014-03-17T02:54:41.943Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"892","ttl":"60","published_at":"2014-03-17T02:54:52.851Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"871","ttl":"60","published_at":"2014-03-17T02:55:03.714Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"805","ttl":"60","published_at":"2014-03-17T02:55:14.528Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"838","ttl":"60","published_at":"2014-03-17T02:55:25.367Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"1083","ttl":"60","published_at":"2014-03-17T02:55:36.458Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"930","ttl":"60","published_at":"2014-03-17T02:55:47.392Z","coreid":"50ff73065067545640270287"}
event: URLTime
data: {"data":"820","ttl":"60","published_at":"2014-03-17T02:55:58.224Z","coreid":"50ff73065067545640270287"}

Arduino String objects are really convenient but they can be slow due to dynamic memory allocation. I would not create and destroy many Strings quickly. If you want to go fast, I would recommend C strings and static allocation.