From 44492f33e836568f43f661713cb8263494991142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 8 Oct 2016 16:26:38 +0200 Subject: [PATCH] make better HTTP request --- main.c | 149 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 65 deletions(-) diff --git a/main.c b/main.c index 399f2ab..d21018e 100644 --- a/main.c +++ b/main.c @@ -81,7 +81,7 @@ int _write(int file, char *ptr, int len) } /** user input command */ -char command[32] = {0}; +static char command[32] = {0}; /** user input command index */ uint8_t command_i = 0; @@ -137,6 +137,79 @@ error: return; } +/** send HTTP data + * @warning blocking until a response has been received + * @param[in] data data to be send + * @param[in] length number of bytes to be sent, set to 0 to use the string length + * @return if data has been sent + */ +static bool http_send(uint8_t* data, size_t length) +{ + if (length==0) { + radio_esp8266_send(data, strlen((char*)data)); // send string data + } else { + radio_esp8266_send(data, length); // send raw data + } + while (!radio_esp8266_activity) { // wait until response has been received + __WFI(); // wait until something happens + } + if (!radio_esp8266_success) { + fprintf(stderr,"could not send data\n"); + return false; + } + return true; +} + +/** end HTTP connection + * @warning blocking until a response has been received + * @return if connection has been closed + */ +static bool http_end(void) +{ + radio_esp8266_close(); // close connection + while (!radio_esp8266_activity) { // wait until response has been received + __WFI(); // wait until something happens + } + return radio_esp8266_success; +} + +/** open HTTP connection and send POST header + * @warning blocking until a response has been received + * @param[in] host host name or IP of HTTP server to connect to + * @param[in] port port number of HTTP server to connect to + * @param[in] length number of bytes to POST + * @return if HTTP POST succeeded + */ +static bool http_post_header(char* host, uint16_t port, size_t length) +{ + char http_line[256] = {0}; // generated lines + radio_esp8266_tcp_open(host, port); // open connection + while (!radio_esp8266_activity) { // wait until response has been received + __WFI(); // wait until something happens + } + if (!radio_esp8266_success) { + fprintf(stderr,"TCP connection failed\n"); + return false; + } + if (!http_send((uint8_t*)"POST /write?db=test HTTP/1.1\r\n", 0)) { // send data + return false; + } + if (snprintf(http_line, LENGTH(http_line), "Content-Length: %u\r\n", length)<0) { // set content length (for measurements) + fprintf(stderr,"could not create line\n"); + return false; + } + if (!http_send((uint8_t*)http_line, 0)) { // send data + return false; + } + if (!http_send((uint8_t*)"Host: influx\r\n", 0)) { // send data + return false; + } + if (!http_send((uint8_t*)"\r\n", 0)) { // send data + return false; + } + return true; +} + /** program entry point * this is the firmware function started by the micro-controller */ @@ -210,71 +283,17 @@ void main(void) // send HTTP POST request printf("making HTTP request: "); - radio_esp8266_tcp_open("192.168.42.3",8086); // open connection - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens + char line[256] = {0}; // measurement line to send + if (snprintf(line, LENGTH(line), "cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000")<0) { + fprintf(stderr,"could not create line\n"); + } else if (!http_post_header("192.168.42.3", 8086, strlen(line))) { // send header + fprintf(stderr,"could not sent HTTP POST header\n"); + } else if (!http_send((uint8_t*)line, 0)) { // send data + fprintf(stderr,"could not send measurement\n"); + } else { + http_end(); // end HTTP request (don't care about the result) + printf("OK\n"); } - if (!radio_esp8266_success) { // could not open port - printf("TCP connection failed\n"); - while (true); // unhandled error. wait for debugging or watchdog - } - char http_line[256+1] = {0}; - int http_length = snprintf(http_line, LENGTH(http_line), "POST /write?db=test HTTP/1.1\r\n"); - if (http_length<=0) { // line creation failed - while (true); // not ideal error handling - } - radio_esp8266_send((uint8_t*)http_line, http_length); // send data - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens - } - if (!radio_esp8266_success) { // could not open port - while (true); // not ideal error handling - } - http_length = snprintf(http_line, LENGTH(http_line), "Content-Length: 74\r\n"); - if (http_length<=0) { // line creation failed - while (true); // not ideal error handling - } - radio_esp8266_send((uint8_t*)http_line, http_length); // send data - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens - } - if (!radio_esp8266_success) { // could not open port - while (true); // not ideal error handling - } - http_length = snprintf(http_line, LENGTH(http_line), "Host: influx\r\n"); - if (http_length<=0) { // line creation failed - while (true); // not ideal error handling - } - radio_esp8266_send((uint8_t*)http_line, http_length); // send data - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens - } - if (!radio_esp8266_success) { // could not open port - while (true); // not ideal error handling - } - http_length = snprintf(http_line, LENGTH(http_line), "\r\n"); - if (http_length<=0) { // line creation failed - while (true); // not ideal error handling - } - radio_esp8266_send((uint8_t*)http_line, http_length); // send data - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens - } - if (!radio_esp8266_success) { // could not open port - while (true); // not ideal error handling - } - http_length = snprintf(http_line, LENGTH(http_line), "cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000"); - if (http_length<=0) { // line creation failed - while (true); // not ideal error handling - } - radio_esp8266_send((uint8_t*)http_line, http_length); // send data - while (!radio_esp8266_activity) { // wait until response has been received - __WFI(); // wait until something happens - } - if (!radio_esp8266_success) { // could not open port - while (true); // not ideal error handling - } - printf("OK\n"); // main loop printf("command input: ready\n");