Skip to content

Instantly share code, notes, and snippets.

@towynlin
Created September 10, 2014 18:32
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 towynlin/272d424f4f64d51d0323 to your computer and use it in GitHub Desktop.
Save towynlin/272d424f4f64d51d0323 to your computer and use it in GitHub Desktop.
Using a Spark Core, gather testing data by making an HTTP POST request every 10 seconds
#include "application.h"
TCPClient client;
const byte SERVER[] = { 192, 168, 0, 100 }; // <-- Change to your server
const int PORT = 4567;
const char REQ_FORMAT[] = "POST / HTTP/1.0\r\n"
"Host: example.com\r\n"
"Content-Length: %d\r\n"
"\r\n%s";
const int MAX_REQ_SIZE = 256;
const char BODY_FORMAT[] = "my_data=%s\r\n";
const int MAX_BODY_SIZE = 64;
void setup() {
Serial.begin(115200);
}
void post_it() {
static int attempts = 0;
static int failures = 0;
static int long_connects = 0;
static int long_totals = 0;
attempts++;
system_tick_t diff;
system_tick_t started_at = millis();
Serial.println("\nconnecting...");
if (client.connect(SERVER, PORT)) {
Serial.print("connected ");
diff = millis() - started_at;
Serial.println(diff);
if (100 < diff) long_connects++;
char body[MAX_BODY_SIZE];
int body_size = snprintf(body, MAX_BODY_SIZE, BODY_FORMAT, "dudit");
char req[MAX_REQ_SIZE];
int req_size = snprintf(req, MAX_REQ_SIZE, REQ_FORMAT, body_size, body);
int sent_size = client.print(req);
if (sent_size != req_size) {
failures++;
Serial.print(" **** !!!! did not send expected size - req_size: ");
Serial.print(req_size);
Serial.print(", sent_size: ");
Serial.println(sent_size);
}
Serial.print("waiting for connection to close ");
Serial.println(millis() - started_at);
while (client.connected()) {
int bytes_avail = client.available();
if (bytes_avail) {
uint8_t buf[bytes_avail];
client.read(buf, bytes_avail);
Serial.print((const char*)buf);
}
}
Serial.println("\r\ndisconnecting ");
client.stop();
} else {
failures++;
Serial.println("failed to connect ");
}
diff = millis() - started_at;
Serial.println(diff);
if (700 < diff) long_totals++;
// report
Serial.print("Attempts: ");
Serial.print(attempts);
Serial.print(", long connects: ");
Serial.print(long_connects);
Serial.print(", long totals: ");
Serial.print(long_totals);
Serial.print(", failures: ");
Serial.print(failures);
Serial.print("\r\n\n\n");
}
void loop() {
static system_tick_t last = millis();
if (millis() - last > 10000) {
post_it();
last = millis();
}
}
@towynlin
Copy link
Author

Been running this for a while, as of right now:

Attempts: 154, long connects: 7, long totals: 13, failures: 1

The one failure was a failure to connect the socket in the first place, which can easily be retried.

@towynlin
Copy link
Author

Attempts: 631, long connects: 27, long totals: 45, failures: 8

@towynlin
Copy link
Author

Hmm... looks like my local test died after this

Attempts: 653, long connects: 28, long totals: 46, failures: 8



connecting...
connected 69
waiting for connection to close 73

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment