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!