TCPClient seems to not work without a delay after handshake

Can someone please explain to me why this simple basic TCPClient code returns -1, is the spark core even capable of making TCPClient / Server connections without being attached at the umbilical to a PC via serial ports, because nothing I try no matter how basic seems to work. URL doesn’t work, IP doesnt work nothing about TCPClient seems to work. I have managed to get it working over serial but thats not what I want.

#include <application.h>

TCPClient client;
int update = 0;
byte server[] = { 74, 125, 224, 72 }; //google


void setup()
{
    
Spark.variable("update", &update, INT);

//
client.connect(server, 80);

if (client.connected()){
   update = 1;
   }
   
   else {
     update = -1;
   }
}

// OR

if (client.connect(server, 80)) {
   update = 1;
   }
   
   else {
     update = -1;
   }
}

void loop()
{
   
}

Actually if it did work with Serial connected, I’d argue your topic title does not actually reflect your experience :wink: (before @Dave edited the topic title :+1:)

Have you tried a short delay before client.connect() and between this and client.connected()?

1 Like

This is (in my experience and opinion) a bug. @ScuffR, there should not be any need to have a delay, but yes that should fix it. I have had two topics with conversation about this in the forum. delay(10000) before client.connect() should fix it.

The reason why Serial works is because of the delay of the Spark waiting for you to connect the Serial port.

Here are the topics if anyone is interested:


Ok, I have sucessfully got it work how I expect it to, but it still requires a Serial.begin(9600) despite not being attached via serial and it also requires SPARK_WLAN_Loop(); being called. I tried adding delays but just couldn’t get it to work, unfortunately the api call times out for the first 30 seconds of the core going live then works as normal.

#include <application.h>

TCPClient client;
int update = 0;
byte server[] = { 74, 125, 224, 72 }; //google

void setup()
{

Serial.begin(9600);    

Spark.variable("update", &update, INT);

SPARK_WLAN_Loop();

if (client.connect(server, 80)) {
update = 1;
}
   else {
     update = -1;
   }
}

void loop()
{
   
}

I can then add to this to do .php page updates

#include <application.h>

TCPClient client;
int update = 0;
char server[] = "";


void setup()
{
    
    Serial.begin(9600);
    
  Spark.variable("update", &update, INT);


SPARK_WLAN_Loop();

   
   if (client.connect(server, 80)){
   update = 1;
   
    client.println("GET /led.php?led=9 HTTP/1.0");
    client.println("Host: URL goes here");
    client.println("Content-Length: 0");
    client.println();
   }
   
   else {
     update = -1;
   }

}

void loop()
{
   
}

Updated the thread title to try and better reflect the bug you’re seeing. :slight_smile:

Thanks!
David

No idea why you seem to need Serial.begin() for client.connect() to work.
There is (I can see) nothing in the implementation of either class that would suggest any dependencies between them.

What kind of delay time have you tested?
Could you try something like

void setup()
{
  unsigned long ms = millis();

  Spark.variable("update", &update, INT);

  while(millis() - ms < 5000)  // play a bit with this number
    SPARK_WLAN_Loop();

  // now try connecting and the rest of setup
  ...
}
1 Like