Hello guys, I’m trying to send post/get to a URL as soon as the spark core starts up. I need to be this as soon as possible. I have noticed that If I don’t insert a delay of 5 seconds before the request is done, it is actually never executed. No idea why.
I have tried both the HTTPClient library and done my own with no library just for the sake of trying. Both do not work. The example that comes with HTTPClient only works after the first request. I’m pasting the code of the example, with a small modification that will execute the request only once. If you add the while(!Serial.available()) SPARK_WLAN_Loop(); to the setup the request is indeed done.
Because of this you won’t be able to see the request output on time by serial, If you want to see the request response after it has been done, connect to the serial and press a key and insert this code right after loop() opens:
if (Serial.available()) {
Serial.read();
Serial.println(response.body)
}
unsigned int nextTime = 0; // Next time to contact the server
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;
void setup() {
Serial.begin(9600);
}
void loop() {
if (nextTime) {
return;
}
Serial.println();
Serial.println("Application>\tStart of Loop.");
// Request path and body can be set at runtime or at setup.
request.hostname = "www.timeapi.org";
request.port = 80;
request.path = "/utc/now";
// 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);
nextTime = millis() + 10000;
}
I have tried the SEMI_AUTOMATIC mode as well, and noticed that if I enable Wifi only and make the request 10 seconds later it’s not actually done. I need to enable Wifi, connect to the cloud, wait 5 seconds and then it’s done.
Any ideas? and thanks in advance.
I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy