Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Last active May 27, 2019 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairjevans/c5c4558b1a3006cc75d515a94808543b to your computer and use it in GitHub Desktop.
Save alistairjevans/c5c4558b1a3006cc75d515a94808543b to your computer and use it in GitHub Desktop.
Sending Data over HTTP
#define SERVER "MYSERVER"
#define SERVERPORT 5000
WiFiClient client;
void sendData(unsigned long timestamp, double speed)
{
// Host and port
if(client.connect(SERVER, SERVERPORT))
{
char body[64];
// Clear the array to zeroes.
memset(body, 0, 64);
// Arduino sprintf does not support floats or doubles.
sprintf(body, "milliseconds=%lu&speed=", timestamp);
// Use the dtostrf to append the speed.
dtostrf(speed, 2, 3, &body[strlen(body)]);
int bodyLength = strlen(body);
// Specify the endpoint
client.println("POST /data/providereading HTTP/1.1");
// Write Host: SERVER:SERVERPORT
client.print("Host: ");
client.print(SERVER);
client.print(":");
client.println(SERVERPORT);
// Close the connection after the request
client.println("Connection: close");
// Write the amount of body data
client.print("Content-Length: ");
client.println(bodyLength);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
client.print(body);
// Wait for the response
delay(100);
// Read the response (but we don't care what is in it)
while(client.read() != -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment