TCP Client and WIFI?

If nothing else, the SPark core keeps me guesing and very challenged :slight_smile:
So, in a previous topic, I had posted a small snippet of code that I was using to create a TCP Client that would connect to a small custom java server. The good news is that when the core is connected to my laptop via USB cable, everything has been working well, core connects to wifi and every second sends a very simple text string that is received in my java server.
So, I removed all statements relating to “serial”, disconnected core from laptop, plugged into the well and voila…nothing happens :frowning:
It boots up, flashes green and eventually breathes the cyan/magenta color so it appears to be starting correctly. I do see the device listed on my router so the wifi is connected but it does not appear to be sending anything or it sends but never receives…not sure which one.
So, I have to say I am somewhat stumped as to where to start debugging :slight_smile: It is power related, WIFI related (Core is physically close to router), firewall related (I turned them off on my laptop to ensure connectivity), etc…

Any suggestions?

Scott

Hi @scottdillon1

The most common way this happens is that you forget to take out the while loop you put in setup that waits for you to press a key and start serial debugging.

If that is not it, post your code and we will help you debug. The code formatting here in the forum is like this:

```cpp
  Code Goes here

Thanks Bko…this is not production code as you will see below but given it worked perfectly before removing the Serial statements…something else very basic must be going wrong…of course…could never be user error :slight_smile: Code below should really close to samples that are provided by core.

TCPClient client;
byte server[] = { 192, 168, 1,132 }; 
void setup()
{
delay(5000); //I noticed a ocuple of other posts that recommended  a small delay before trying to connect
  while (!client.connect(server,6666))
   {
      delay(1000);
  }
}

void loop()
{
   if (client.connected())
  {
    client.println("Ottawa"); //Yes, I live in Canada
    delay(3000);
  }
  if (!client.connected())
  {
    client.stop();
    for(;;);
  }
}

Thanks for any suggestions you have,
Scott

Folks, quick update. I modified the code to include a little function to light an LED once connected to the WIFI and just to verify the code was in fact still running. It is :slight_smile: Tonight, I started the laptop, plugged in the core and it started up and started sending events with no problem. Then I unplugged the core, restarted the java server, replugged and with zero code changes it now does not send any events? Any thoughts?

Thanks for any insight,
Scott

Hi @scottdillon1

A couple of things come to mind in your code.

Calling client.connect is a loop is generally not needed.

You code says, when you discover that the client is not connected for any reason (the server glitched, the router glitched, etc.) call stop and loop forever doing nothing. I don’t think you want to do this. A better approach is to reconnect at that point.

You never read the data from the client. If your server is sending you anything your core firmware will eventually crash since you have to clear the packet buffers in the TI CC3000.

Hi Bko, you are totally right…the funny thing is that I was not even looking at the part of the code until last night. here is the new code…just removed the code you were referring to as I also came to same conclusion…that’s the problem with just copying sample code and not actually using your brain :smile:

while (!client.connect(server,1024))
{
if (client.connected())
{
digitalWrite(led, HIGH); // Turn ON the LED
delay(1000); // Wait for 1000mS = 1 second
digitalWrite(led, LOW); // Turn OFF the LED
delay(1000);
}
else
{
digitalWrite(led, HIGH); // Turn ON the LED
delay(1000); // Wait for 1000mS = 1 second
digitalWrite(led, LOW); // Turn OFF the LED
delay(1000);
digitalWrite(led, HIGH); // Turn ON the LED
delay(1000); // Wait for 1000mS = 1 second
digitalWrite(led, LOW); // Turn OFF the LED
delay(1000);
}
delay(1000);
}
The funny thing is I just flashed a different sample and reflashed my client code and now it’s working…very weird.

Scott

The TI CC3000 has four sockets for TCP or UDP connections, one of which is used by the cloud if you are using it.

The line above has the potential to run you out of sockets but I can't really tell from the way your new code is formatted. :wink:

Thanks for the quick reply…not sure I follow your logic on that one but really want to :smile:
The original intent of the code was to flash the LEDs just to show me that the code was at least trying to connect. For three days now I have been watching the LEDs blink two times continuously :frowning: My assumption is that as soon as the client.connect returns a true, it will continue on to the for loop and start sending me data. If that assumption is true, why would it use more than one socket?

Did not know the board had four sockets…thanks for that input…lots to learn.

Scott
p.s.So far, the spark has returned about 1300 events and counting.