Can't Re-Establish TCPClient Connection

Hi, everyone!

I’m working on a Spark Core project in which I’m trying to have a core connect to a local server’s socket, send one byte to identify itself, and receive 3 bytes at a time for the purpose of setting some RGB LEDs. The server sends these bytes in a group, but these groups will come anywhere from 1 second to fifteen minutes in between.

I’ve ironed out the initial server connection; the core powers on, connects to the socket, and happily receives data, displaying that data as color values on attached RGB LEDs.

The problem is coming back from a dropped connection. This first version of error-checking failed to reconnect to our local server socket, but we could still flash it using the Spark Cloud:

void attemptConnection(){
    
    if (client.connect(server, 7890)) 
  {
    rainbow(5);
    client.write(29);
    digitalWrite(D7, HIGH); //Onboard LED turn on
  } 
}

void loop() {

   (Other code)
 
    if (!client.connected()) {
        //client.stop();
        //setup();
        attemptConnection();
    }
}

Assuming that perhaps a dropped wifi connection wouldn’t trigger a client.connected() issue, I tried a higher-level approach. Unfortunately, when it drops it sticks the core into a green-flash mode:

if (!Spark.connected()){
    client.stop();
    Spark.disconnect();
    Spark.connect();
    attemptConnection();
}

I wouldn’t be surprised if there’s something simple I’m missing… Any idea what it is?

There are currently lots of problems with the cores network stack (or maybe just one very sneaky problem beneath it all) that make it unsuitable for any kind of reliable production use (in my experience). UDP & TCP both have issues. It may stem from problems with the Texas Instruments CC3000 chip used for WiFi. I know a lot of clever people are working on fixing it, so hopefully we’ll have fixes soon. :smile:

1 Like

Hi @bandgeek​80001,

There have been a bunch of threads on TCPClient / TCPServer, and some awesome community members have sent in pull requests that should improve those libraries. We’re reviewing those patches now, and we’re hoping to roll them out soon. It’s also possible this could be a problem with cleaning up used sockets, since the CC3000 can only keep 4-8 open at a time (I think).

Thanks,
David