add HTTP example

This commit is contained in:
King Kévin 2016-10-04 11:16:34 +02:00
parent e9489ec910
commit 94d4d5c722
1 changed files with 68 additions and 1 deletions

69
main.c
View File

@ -188,6 +188,73 @@ void main(void)
printf("setup ESP8266 WiFi SoC: ");
radio_esp8266_setup();
printf("OK\n");
// send HTTP POST request
printf("making HTTP request: ");
radio_esp8266_tcp_open("192.168.42.2",8086); // open connection
while (!radio_esp8266_activity) { // wait until response has been received
__WFI(); // wait until something happens
}
if (!radio_esp8266_success) { // could not open port
return; // not ideal error handling
}
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");
@ -279,7 +346,7 @@ void main(void)
ticks_time = rtc_get_counter_val(); // copy time from internal RTC for processing
action = true; // action has been performed
if ((ticks_time%(60))==0) { // one minute passed
printf("uptime %02lu:%02lu:%02lu\n", ticks_time/(60*60), (ticks_time%(60*60))/60, (ticks_time%60)); // display external time
printf("uptime: %02lu:%02lu:%02lu\n", ticks_time/(60*60), (ticks_time%(60*60))/60, (ticks_time%60)); // display external time
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity