Skip to content

Instantly share code, notes, and snippets.

@charleskorn
Created July 14, 2016 09:35
Show Gist options
  • Save charleskorn/7f93a5e9b7026369de854959a620cdd0 to your computer and use it in GitHub Desktop.
Save charleskorn/7f93a5e9b7026369de854959a620cdd0 to your computer and use it in GitHub Desktop.
HTTP sample for Particle
String requestBody = constructRequestBody(time, values);
byte server[] = {10, 242, 127, 30};
TCPClient client;
if (client.connect(server, 8000))
{
Serial.println("Connected.");
String agentId = String(persistentStorage.getAgentId());
String token = persistentStorage.getToken();
client.println("POST /v1/agents/" + agentId + "/data HTTP/1.1");
client.println("User-Agent: weather-thingy-particle");
client.println("Connection: close");
client.println("Authorization: weather-thingy-agent-token " + token);
client.println("Accept: */*");
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(requestBody.length()));
client.println();
client.println(requestBody);
client.println();
client.flush();
Serial.println("Response:");
// TODO Check response indicates success
unsigned long startTime = micros();
while (micros() - startTime < 2 * 1000 * 1000) {
while (client.available()) {
Serial.write(client.read());
}
}
Serial.println();
Serial.println("End of response.");
client.stop();
}
else
{
Serial.println("Connection failed!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment