Created
May 27, 2019 12:09
-
-
Save alistairjevans/61b19f11eb78e88c2b06b1d5cc2d3161 to your computer and use it in GitHub Desktop.
Sensor data buffered request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sendData(unsigned long timestamp, double speed) | |
{ | |
int success = 1; | |
char request[256]; | |
char body[64]; | |
if(!client.connected()) | |
{ | |
success = client.connect(server, 5000); | |
} | |
if(success) | |
{ | |
// Empty the buffer | |
// (I still don't really care about the response) | |
while(client.read() != -1); | |
// Clear the request data | |
memset(request, 0, 256); | |
// Clear the body data | |
memset(body, 0, 64); | |
sprintf(body, "milliseconds=%lu&speed=", timestamp); | |
dtostrf(speed, 2, 3, &body[strlen(body)]); | |
char* currentPos = request; | |
// I'm using sprintf for the fixed length strings here | |
// to make it easier to read. | |
currentPos += sprintf(currentPos, "POST /data/providereading HTTP/1.1\r\n"); | |
currentPos += sprintf(currentPos, "Host: %s:%d\r\n", server, 5000); | |
currentPos += sprintf(currentPos, "Connection: keep-alive\r\n"); | |
currentPos += sprintf(currentPos, "Content-Length: %d\r\n", strlen(body)); | |
currentPos += sprintf(currentPos, "Content-Type: application/x-www-form-urlencoded\r\n"); | |
currentPos += sprintf(currentPos, "\r\n"); | |
strcpy(currentPos, body); | |
// Send the entire request | |
client.print(request); | |
// Force the wifi module to send the packet now | |
// rather than buffering any more data. | |
client.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment