# Basic tcp example always returns connection failed on serial \[Solved\]

**URL:** <https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355>\
**Category:** Firmware\
**Created:** [June 26, 2014, 8:53am UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355 "2014-06-26T08:53:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tertius](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tertius](https://community.particle.io/u/tertius)\
**Post date:** [June 26, 2014, 8:53am UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/1 "2014-06-26T08:53:52Z")

</div>

Hi,

I have a project that I’m trying to port from using arduino micro + cc3000 to spark core due to lack of memory, and I’m stuck at the tcp example, any help would be much appreciated.

I’m using the basic tcp connection example with 1 modification (the for(;😉 is blocking so I changed it to delay), code bellow, and I can’t get it to work.

on the serial port I always get:

connecting…  
connection failed

disconnecting.

Code:

```
TCPClient client;
byte server[] = { 74, 125, 224, 72 }; // Google
void setup()
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /search?q=unicorn HTTP/1.0");
    client.println("Host: www.google.com");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    delay(5000);
  }
}

```

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [June 26, 2014, 9:18am UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/2 "2014-06-26T09:18:51Z")

</div>

Try the following modification. Instead of delaying for 1 second after beginning a serial connection, just sit in a hard loop (while still pinging the background tasks) and wait for keypress from your serial terminal (ENTER works!). I tested this and found 5 out 5 reboots successful. Without this modification (your original code) I had one of 5 successful.

```cpp
TCPClient client;
byte server[] = { 74, 125, 224, 72 }; // Google
void setup()
{
  Serial.begin(9600);
  //delay(1000);
  while(!Serial.available()) SPARK_WLAN_Loop();

  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /search?q=unicorn HTTP/1.0");
    client.println("Host: www.google.com");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    delay(5000);
  }
}

```

---

<div class="post-metadata">

**Author:** ![tertius](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@tertius](https://community.particle.io/u/tertius)\
**Post date:** [June 26, 2014, 9:24am UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/3 "2014-06-26T09:24:05Z")

</div>

Solved!,

Thank you so much!

---

<div class="post-metadata">

**Author:** ![hassaan22](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hassaan22/32/2865_2.png) [@hassaan22](https://community.particle.io/u/hassaan22)\
**Post date:** [June 26, 2014, 6:35pm UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/4 "2014-06-26T18:35:40Z")

</div>

Oh my God, you are a Lifesaver!!! Thank you so much! Just one question: In the line:

while(!Serial.available()) SPARK\_WLAN\_Loop();

what exactly is going on? I do not understand the SPARK\_WLAN\_LOOP() call or what it does. I understand that the while loop will go on if the serial is not available but how did you come up with the following line, and how does it fix the problem?

Thanks so much. I was stumped on this for over a week!

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [June 26, 2014, 7:07pm UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/5 "2014-06-26T19:07:42Z")

</div>

> [@hassaan22](#):
>
> but how did you come up with the following line  
> `while(!Serial.available()) SPARK_WLAN_Loop();`

This is my standard approach for starting a Serial Connection on a Windows machine. Following this explanation is my Tip's and Tricks post about it. The `SPARK_WLAN_Loop()` is the function that's called that processes all background tasks when your user code is not running. You can think of the Spark Core as more or less having 3 functions that run in a main() function that also loops. Not necessarily in this order, those functions are:

```auto
main() {
  1) setup() - runs only once, then is prevented from running again.
  2) loop() - runs this, then when this ends, #3 is run...
  3) SPARK_WLAN_Loop() - ...then when this ends, #3 is run...
}

```

Every time your user loop() ends, the SPARK\_WLAN\_Loop() runs for 5 to 6 milliseconds to keep your Core connected to the Cloud. Then your user loop() runs again, and so on.

If you have a "blocking" loop within your user code such as the while() loop that will run until it receives a character from the Serial port, you need to still processes your background tasks to keep the Core connected to the Cloud. So we just call that task explicitly instead of letting the system call it for us (because it can't do that right now anyway).

> **[Firmware Tips and Tricks](https://community.particle.io/t/firmware-tips-and-tricks/3649/2)**
>
> \##Pause your application to make a Serial over USB connection## Here is a quick trick that makes opening a serial connection over USB on Windows easy and foolproof. // The following line is for Local Builds, // but works fine in the (Web IDE)...

* * *

> [@hassaan22](#):
>
> and how does it fix the problem?

That's a great question, but unfortunately I don't have a detailed answer for this one... yet 😉

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [June 26, 2014, 8:48pm UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/6 "2014-06-26T20:48:49Z")

</div>

I moved 3 posts to a new topic: [HTTPS Request from Spark Core?](http://community.spark.io/t/https-request-from-spark-core/5376)

---

<div class="post-metadata">

**Author:** ![gauravptalukdar](https://avatars.discourse-cdn.com/v4/letter/g/51bf81/32.png) [@gauravptalukdar](https://community.particle.io/u/gauravptalukdar)\
**Post date:** [July 29, 2014, 11:36am UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/7 "2014-07-29T11:36:26Z")

</div>

Hey @BDub,

> [@BDub](#):
>
> Try the following modification. Instead of delaying for 1 second after beginning a serial connection, just sit in a hard loop (while still pinging the background tasks) and wait for keypress from your serial terminal (ENTER works!)

Here I don't see the necessity of SPARK\_WLAN\_Loop();

My observations:

1. Just used " while(!Serial.available()); " Commented out " SPARK\_WLAN\_Loop() "
2. Wait for the core to get stable connection and then press any key. You get connected.
3. Now start over and press any key while the core still searches for connection. You won't get connected.

Now with SPARK\_WLAN\_Loop() present I get the exact same results. I would like to suggest to wait until the spark gets connected to the network rather than wait for a serial input. That should work I guess. Please correct me if I am on the wrong track.

Note: I tested this communication with cloud disabled. Created a local TCP server in a PC.

Now can anyone help me identify the function which returns when the core is online both with cloud enabled (breathing cyan) and without cloud (breathing green -local network). Also what is the return value.

Thanks  
Gaurav

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [July 29, 2014, 1:00pm UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/8 "2014-07-29T13:00:20Z")

</div>

Hi @gauravptalukdar

You didn’t see any difference because SPARK\_WLAN\_Loop() does almost nothing when the cloud is disabled.

The Spark cloud connection is doc’ed here

[http://docs.spark.io/firmware/#connection-management-spark-connected](http://docs.spark.io/firmware/#connection-management-spark-connected)

It sounds like `Spark.connected()` is what you looking for regarding the cloud connection.

For WiFi there is `WiFi.status()` which can return one of `WIFI_CONNECTING, WIFI_ON, WIFI_OFF`.

This is an area of active development for the Spark team and these methods will be changing in the near future.

---

<div class="post-metadata">

**Author:** ![gauravptalukdar](https://avatars.discourse-cdn.com/v4/letter/g/51bf81/32.png) [@gauravptalukdar](https://community.particle.io/u/gauravptalukdar)\
**Post date:** [July 29, 2014, 1:12pm UTC](https://community.particle.io/t/basic-tcp-example-always-returns-connection-failed-on-serial-solved/5355/9 "2014-07-29T13:12:25Z")

</div>

Thanks @bko for the update.

Anyways, the following code snippet did the trick for me.

```
  while (!client.connect(server, 50000)) // 50000 -listening port
	{
	delay(10000); // Not an absolute necessity
	Serial.println("connecting...");
	}
  Serial.println("connected");

```

I will try out the other functions. Another thing @bko , are there any C libraries available to work with the spark core ??

-Gaurav
