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

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(;:wink: 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);
  }
}

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.

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);
  }
}
4 Likes

Solved!,

Thank you so much!

4 Likes

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!

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:

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).


That’s a great question, but unfortunately I don’t have a detailed answer for this one… yet :wink:

1 Like

I moved 3 posts to a new topic: HTTPS Request from Spark Core?

Hey @BDub,

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

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

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.

1 Like

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