Unable to send multiple get request to webserver (flashes cyan)

Hello Particle community!

I have successfully setup my spark core to send get requests, with the help of some examples that are on this community.
My problem is that if i send more than a single get request to my webserver within 10-20 seconds from each other, the core will only send the first request, and fail to send the rest. The core will start to flash cyan when this happens, and if under heavy stress, even green. It almost seems like it is crashing when I attempt to send more get requests. This might be a problem with my code, or is this normal?

here is the code I used to send the requests :smile:

the setup bit of code

#include "HttpClient/HttpClient.h"

#include <string.h>
#include <math.h>
#define LIB_DOMAIN "requestb.in"

TCPClient myTCP;    

The get request

void sendGetRequest(const char * server, const char * url)
    {
        if (myTCP.connect(server, 80)) 
        {
            myTCP.print("GET ");
            myTCP.print(url);
            myTCP.println(" HTTP/1.1");
            myTCP.println("Connection: close");
            myTCP.print("Host: ");
            myTCP.println(server);
            myTCP.println("Accept: text/html, text/plain");
            myTCP.println();
            myTCP.flush();
        } 
    } 

and then this is called whenever i actually send my request

sendGetRequest("webserver.com", "subdirectory/getrequestdata");

So has this something to do with my code or is the spark core not capable to do multiple request in a (relatively) short timeframe?

I am looking forward to hear your input

Have a great weekend :smiley:

Hi @Freezy

It looks like you are not really using the HTTP client library–it has an example that shows you how to use it. Check on the web IDE or on github directly for details.

From how you describe your LED, I think your code is not reading all of the data that your web server is sending back. If you switch to HttpClient this will be better. Or if you want to have your own functions like sendGetRequest, you should wait for the web server to send the first byte back and the loop reading (and possible discarding) bytes until there are no more.

What happens if you don’t do this is you tie up all the packet buffers in the TI CC3000 and then the cloud cannot connect and your core will retry until it generally reboots.

1 Like

Thanks alot, it makes sense. I will try this and report back later
Cheers!

Yes! thank you! That is indeed a lot smoother, and it works really well!
In the code I got my request.path and request.host name from elsewhere in my code, so that I am able to send appropriate data in the get request url.
I did this with this code

void sendGetRequest(const char * server, const char * url) 

and then called this in the method with the data that I wanted to send

sendGetRequest("someservername.com", "/someURLwithdata");

I use the example from the HttpClient library

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

    // 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.println(response.status);

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

It obviously works when I call sendGetRequest() in my methods. But since I am very new to this, I can’t really figure out how I am going to make request.path() take different input from different methods.

Thanks again :slight_smile:

1 Like

Hi @Freezy

Since the HTTP library uses Arduino String objects and not char arrays, you should be able to just pass in const char* pointers as before and then assign them

  request.hostname = server;
  request.path = url;

There is a problem that sometimes comes up assigning String objects and if that doesn’t compile you can use the append method instead which copies the argument into the String object.

  request.hostname = "";
  request.hostname += server;
  request.url = "";
  request.url += url;

I know that this problem with String assignment is getting fixed in an upcoming release.

Hi Bko
Thanks for your response. Neither of those seems to work. I get a compile error:

Error: Could not compile. Please review your code.

It seems very strange to me , that the error message is not more specific

Here is my code: :smile:

void sendGetRequest(const char * server, const char * url)
{   
    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "";
    request.hostname += server;
    request.port = 80;
    request.url = "";
    request.url += url;
    

    // 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.println(response.status);

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

I just tried again, and for some reason it compiles just fine now :smiley:

Amazing!

Thanks a bunch :slight_smile:

2 Likes