Tcpclient.connect issue [SOLVED]

Hi,

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[] = "www.google.com";
char url[] = "search?q=unicorn";
void setup() {
    
      Spark.variable("datavalue", &datacloud, INT);
Serial.begin(9600);
  datacloud = 10;
}



//call repeatedly
void loop() {

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



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

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

        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
          client.read();  //flush data
          count++;
        }
        client.flush();  //for safety

        //client.flush();
        delay(400);
        client.stop();
        Serial.print("sent and closed-bytes: ");
        Serial.println(count);
    return 1;
     }
    else {
        if(datacloud != 20) datacloud = 30;
        client.flush();
        client.stop();
        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.

2 Likes

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 google.com 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);
Serial.begin(9600);
  //datacloud = 10;
}

void loop() {


    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "http://py-scis.appspot.com";
    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(response.status);

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

    delay(10000);
}

The code works just fine if I change it to google.com, 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 "py-scis.appspot.com" 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