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?
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.
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()
{
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.
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.
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:
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);
}