[solved] TCPClient Will Not Connect

Hello Everyone,

I am brand new to the Spark Core but not to programming or micro controllers in general. I have been messing around with several applications and had a TCPClient working to a Nodejs Server for several weeks. I then started a project for school that required similar but different code. So I started from scratch to practice and learn some more stuff, however, the new code was never able to connect. So i tried going back to old project and now even the code that was working for a few weeks doesn’t seem to work anymore.

I am not able to connect to any server using the TCPClient via IP. I have read through several different posts on the forum and have tried the following:

  1. Particle.process()/1 second delay before client.connect()
  2. alternate for client.flush() ```cpp while (client.available() == false && millis() - startTime < 1000) { // Just wait up to 1000 millis } startTime = millis(); while (client.available() > 0 && millis() - startTime < 1000) { client.read(); } ```
  3. Verified that i can connect to the Cloud and publish variables/functions
  4. I can also flash via the webIDE
  5. I am 100% up to date with patches (or atleast i think i am) Im at patch 1.29
  6. I have also run the following piece of code and verified that i have atleast 3 open sockets everytime i run the code ```cpp for(int i=0; i<7; i++) { int res = get_socket_active_status(i); if (SOCKET_STATUS_ACTIVE == res) { Serial.println("SOCKET IN USE"); } else { Serial.println("WE HAVE A SOCKET"); } } ```
  7. I am able to reach and communicate with the server through a browser, mobile application, and various rest clients. (It is not an HTTP server it is a TCP server, however, it does accept HTTP)

So the code I have reverted to in order to try and get a simple connection is the following:

byte ip[] = {146, 185, 175, 78};
//IPAddress ip(146, 185, 175, 78);
//Also Tried with my local network server ip as follows
//byte ip[] = {10,0,1,12};
//IPAddress ip(10,0,1,12);
int port = 80;//8888,8080,7777 and more

TCPClient c;

void setup() 
{ 
    Serial.begin(9600);
    Serial.println("Setup complete");
    Particle.variable("test", &port, INT);
    delay(1000);
} 


void loop() 
{ 
    c.connect(ip, port);
    if(c.connected())
    {
        Serial.println("Connected");
    }else
    {
        Serial.println("Not Connected");
    }
    delay(1000);
}

I am also running a Spark Core (Not a Photon), Flashing via CLI with the particle tool running on Ubuntu 15.10

If anyone can please help me with this. From the posts I’ve been reading maybe @bko might be able to help. You seem very resourceful when it comes to this.

Thank You in Advanced

Tyler

EDIT: Here is the project code that I am trying to get working.

I ran your code on a Photon and it worked properly. I changed the IP address and the delay at the end of loop so it would only connect every 60 seconds, but other than that it was unchanged and successfully connected. I also ran your code on a Spark Core, and it always printed Not Connected. Unfortunately I didn’t have time to figure out why that is, but I thought I’d provide an extra data point.

1 Like

Every time you call c.connect(), you will get a new socket, so I would expect that this code will run for a few cycles of loop, probably 4 or 5 on a Core, and then start to get 6 second timeouts as the TI CC3000 waits for a socket to timeout and close so it can re-use it. If you don’t call c.stop(), you will get this behavior.

Is that what you are seeing here?

@rickkas7 thank you that helps me know that at least if gets closer to the due date and the spark core is still giving me problems I can just jump to photon and be happy. Thanks.

@bko I did try adding in a c.stop() if the client couldn’t connect, however, this doesn’t change anything.

If you call c.connect() say 5 times, you are using 5 different sockets and there are only 4-5 sockets on a TI CC3000 for TCP. So either way, you should call c.stop() before calling c.connect() again.

Let me try this out later tonight on a core. There is probably a work-around, like waiting for c.connected() to be true or time-out instead of expecting it to connect right away.

Thank You. Also i changed the loop code to this:

void loop() 
{ 
    for(int i=0; i<7; i++) {
      int res = get_socket_active_status(i);
      if (SOCKET_STATUS_ACTIVE == res) {
        Serial.println("SOCKET IN USE");
      } else {
        Serial.println("WE HAVE A SOCKET");
      }
    }

    c.connect(ip, port);
    if(c.connected())
    {
        Serial.println("Connected");
    }else
    {
        Serial.println("Not Connected");
        c.stop();
    }
    delay(1000);
}

I added the c.stop() and the print to see how many sockets are available. When i read the serial monitor i get the following:

Setup Complete
SOCKET IN USE
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
Not Connected
SOCKET IN USE
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
Not Connected
SOCKET IN USE
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
WE HAVE A SOCKET
Not Connected

Also worth noting that the actual connect loop I had started with that doesn’t work was this:

          while(!this->client.connected())
          {
            	Serial.print("Connecting..with(");
            	Serial.print(this->ip);
            	Serial.print(":");
            	Serial.print(this->port);
            	Serial.println(")");
         	   	this->client.connect(this->ip, this->port);
                if(!this->client.connected())
                {
                    Serial.println("Couldn't Connect");
                    this->client.stop();   
                    delay(1000);
                    Particle.process();     
                }
            }

Full Code Here

Thank you, I appreciate the help.

So I did a little more digging and

spark::WiFi.resolve("www.google.com");
spark::WiFi.resolve("www.tgauch.net");

are both working. Just for fun I decided to reset my router (even though I could connect to the cloud and resolve hostnames) and now all issues have been resolved. So something funky was going on with my network. Thank you both for your help and for anyone that comes across this in the future, the code I have last posted (with the while loop) works for me 99% of the time (nothings perfect right?) so if you are having problems. I would recommend checking your network.

Thanks,

Tyler

1 Like