Spark Core Http Client Library

Here’s the code:

#include "application.h"
#include "HttpClient/HttpClient.h"

/**
* Declaring the variables.
*/

HttpClient http;

IPAddress remoteIP(192, 168, 178, 47);

char resultstr[64];
int nextTime = 0;
//Headers currently need to be set at init, useful for API keys etc.
 http_header_t headers[] = {
  { "Content-Type", "text/plain" },
  { "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 > millis()) {
        return;
    }
    
    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.ip = remoteIP;
    request.port = 8080;
    request.path = "/rest/items/landing_light/";

    // The library also supports sending a body with your request:
    //request.body = "{\"key\":\"value\"}";
    sprintf(resultstr, "ON");
    request.body = resultstr;

    http.post(request, response, headers);

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

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

    nextTime = millis() + 10000;
}

It is now working if I ping the spark. Although it takes maybe half a minute of pinging before I can get the connection to work. Sometimes I get flashing cyans and then blinking red. while the requests are still sent out. So I suppose it has something to do with interference with the cloud connectivity…

1 Like