Tcpclient.connect issue [SOLVED]


I’m having trouble with TCPclient.connect, I’ve searched the forums for working TCPclient.connect code, and some users posted code that said worked for them:

I’ve tried the same code:

TCPClient client;
char server[] = "";
char url[] = "search?q=unicorn";
void setup() {
      Spark.variable("datavalue", &datacloud, INT);
  datacloud = 10;

//call repeatedly
void loop() {

//String coreID = getCoreID();
//int level = rand() % 100 + 1; //random number between 0 and 100
    int retval = getrequest();

int getrequest(){
client.connect(server, 80);

if (client.connected()) {
        datacloud = 20;
        client.print("GET ");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println("Connection: close");

        unsigned int count = 0;
        unsigned long lastTime = millis();
        while( client.available()==0 && millis()-lastTime<10000) { //ten second timeout
          }  //do nothing
        lastTime = millis();
        while( client.available() && millis()-lastTime<10000 ) {  //ten seconds
;  //flush data
        client.flush();  //for safety

        Serial.print("sent and closed-bytes: ");
    return 1;
    else {
        if(datacloud != 20) datacloud = 30;
        Serial.println("Not connected");
        return 0;

Sorry if the formatting is a bit messy. When I check on the datacloud variable, it is always 30. Which means that client.connect is NEVER returning true :< how can I fix this? I need it to connect to a server that I’m hosting, but I thought I’d stick with getting it to connect in this instance first since it would be easy to alter it afterwards.

Thanks in advance for any assistance!

I would check your DNS first using this great tool

client.print is very slow and causes a few problems, so client.write is a much better way of doing things.

There is also a library for this type of connection too, adds some nice features. I updated it to use the client.write method a while ago so its nice and quick, you can fine the .h and .cpp files here have a look at the HTTPclient library on the web IDE for example and how to use it.


Thanks for the help!

I updated my cc3000 based off a post I saw in the DNS tool link you posted and that allowed me to get a connection to using the code in my first post. However, I still can’t connect to my own server, or other small server (based off of google app engine). I am using your http code at the moment (from the other link you mentioned), here is what I’ve got:

HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL

http_request_t request;
http_response_t response;

//call once
void setup() {
      Spark.variable("datavalue", &datacloud, INT);
  //datacloud = 10;

void loop() {

    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "";
    request.port = 80;
    request.path = "/containerpost";

    // The library also supports sending a body with your request:
    //request.body = "{\"key\":\"value\"}";

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");

    Serial.print("Application>\tHTTP Response Body: ");


The code works just fine if I change it to, but I’m using google’s appengine to host a website and I need it to connect to the server mentioned in the above code. I don’t think it could involve a DNS issue since I updated the CC3000. What are your thoughts? Maybe I’m not doing the request the right way?

Thanks :slight_smile:

I would try with just “” instead, leaving off the “http://” part. This is a host with many names and the web server on it needs to know which hostname you meant to connect to so the Host: field is critical.

1 Like

hey bko! that worked like a charm ^.^ thanks so much for the help :slight_smile:

1 Like