TCPClient example from Documentation not working

Hey guys, I am new to retrieving information from the web, and am trying to run the code from the TCP example from Documentation:


// EXAMPLE USAGE

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();
    for(;;);
  }
}```

I dont know what is wrong but I dont see anything on my Serial. When I take out the if statement if(client.available())
Then the code that was once under the if statement is run, but the information retrieved is still not shown. 
    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();
    for(;;);
  }
}

Well i can´t really help you, but i can say that the code works fine for me !

what do you see on your serial after you run this code?

1 Like

I get the html code of the site. It looks like

1 Like

I think i might have found the problem.

It's not an issue with the documentation. The TCPClient was called twice.

If he had done it that way he would not be able to flash the core due Error´s.

/google.cpp:2:11: error: redefinition of ‘TCPClient client’

/google.cpp:1:11: error: ‘TCPClient client’ previously declared here
make: *** [/google.o] Error 1

Error: Could not compile. Please review your code.

I compared your code and his that was the only difference i noticed :wink:

If we can find the problem, that will help fix the docs if the examples isn’t working.

Thanks!

1 Like

oops, I had no idea that I copied that twice. In my original code, the initialization is done only once!

in my serial, it says “connection failed. disconnecting.” I don’t know what is wrong :frowning:
Also, when I flash this code, my Spark is unable to flash itself again. I don’t know why that is. I have to hard reset it for it to work again! ughh!

Maybe this should help :wink:

1 Like

Yes! Thank you so much! It works fine after that fix! <3

1 Like