Good design from the ground up — how I/O library design can make or break IoT solutions

    POST /upload/device/0242c41f0116/sensor HTTP/1.1
Host: www.iot.example.com
Connection: close
User-Agent: cool-iot/1.3.1312
Accept: application/json
X-SharedSecretAuth: bmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXAsIG5ldmVyIGdvbm5hIGxldCB5b3UgZG93bi4uLgo
Content-Length: 28

{"t":1623906557,"mV":4.16}
  • the device identifier in the request line
  • the timestamp and sensor reading value in the body JSON
  • possibly the authentication token
int http_send_request(http_conn *conn, const uint8_t *buf, uint16_t sz);
int do_upload(http_conn *conn, const char *device_id, uint32_t t, double mv)
{
// Caution: assumes t and mv to be within well-defined value range
static const char *fmt =
"POST /upload/device/%s/sensor HTTP/1.1\r\n"
"Host: www.iot.example.com\r\n"
"Connection: close\r\n"
"User-Agent: cool-iot/1.3.1312\r\n"
"Accept: application/json\r\n"
"X-SharedSecretAuth: bmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXAsIG5ldmVyIGdvbm5hIGxldCB5b3UgZG93bi4uLgo\r\n"
"Content-Length: 28\r\n"
"\r\n"
"{\"t\":%u,\"mV\":%.2f}"; // in flash

// Too big for our stack, so dynamically allocate space
char *buffer = 0;
int n = asprintf(&buffer, fmt, device_id, t, mv);
if (n < 0)
return n;

// Here n is ~300
n = http_send_request(conn, buffer, n);

free(buffer);
return n;
}
int http_send_request(http_conn *conn, iovec *iov);
int do_upload(http_conn *conn, const char *device_id, uint32_t t, double mv)
{
// Caution: assumes t and mv to be within well-defined value range
static const char req_a[] = "POST /upload/device/"; // in flash
static const char req_b[] = "/sensor HTTP/1.1\rn"
"Host: www.iot.example.com\r\n"
"Connection: close\r\n"
"User-Agent: cool-iot/1.3.1312\r\n"
"Accept: application/json\r\n"
"X-SharedSecretAuth: bmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXAsIG5ldmVyIGdvbm5hIGxldCB5b3UgZG93bi4uLgo\r\n"
"Content-Length: 28\r\n"
"\r\n"; // in flash
static const char body_fmt[] = "{\"t\":%u,\"mV\":%.2f}"; // in flash
char body[29]; // in RAM (on stack)
snprintf(body, sizeof(body), body_fmt, t, mv);
// Set up a gather-read iovec, interleaving flash and RAM contents
iovec iov[] = {
{ req_a, sizeof(req_a) -1 },
{ device_id, strlen(device_id) },
{ req_b, sizeof(req_b) -1 },
{ body, 28 },
{ 0, 0 },
}; // in RAM (on stack)
return http_send_request(conn, iov);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
DiUS

DiUS

We specialise in using emerging tech to solve difficult problems, get new ideas to market & disrupt business models.