Created
April 30, 2018 13:08
-
-
Save arihantdaga/fd27e3c991dd90cb65f606105205edc5 to your computer and use it in GitHub Desktop.
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
const char HTTPSERVER_GET_REQUEST_TEMPLATE[] PROGMEM = | |
"GET %s HTTP/1.1\r\n" | |
"Host: %s\r\n" | |
"User-Agent: KIOTDevice\r\n" | |
"Connection: keep-alive\r\n" | |
"Content-Type: application/json\r\n" | |
"\r\n"; | |
void _httpGetRaw(String path) { | |
if (_http_client == NULL) { | |
_http_client = new AsyncClient(); | |
} | |
_http_client->onDisconnect([](void *s, AsyncClient *c) { | |
DEBUG_MSG_P(PSTR("[HTTPSERVER] Disconnected\n")); | |
if(NULL != _http_client){ | |
_http_client->free(); | |
delete _http_client; | |
_http_client = NULL; | |
} | |
}, 0); | |
_http_client->onTimeout([](void *s, AsyncClient *c, uint32_t time) { | |
if(NULL != _http_client){ | |
_http_client->close(true); | |
} | |
}, 0); | |
_http_client->onData([](void * arg, AsyncClient * c, void * response, size_t len) { | |
char * b = (char *) response; | |
b[len] = 0; | |
char * p = strstr((char *)response, "\r\n\r\n"); | |
unsigned int code = (p != NULL) ? atoi(&p[4]) : 0; | |
Serial.println((char *)response); | |
DEBUG_MSG_P(PSTR("[HTTPSERVER] Response value: %d\n"), code); | |
if(NULL != _http_client){ | |
_http_client->close(true); | |
} | |
}, NULL); | |
_http_client->onConnect([](void * arg, AsyncClient * client) { | |
char buffer[strlen_P(HTTPSERVER_GET_REQUEST_TEMPLATE) + strlen(HTTPSERVER_URL) + strlen(HTTPSERVER_HOST) ]; | |
snprintf_P(buffer, sizeof(buffer), | |
HTTPSERVER_GET_REQUEST_TEMPLATE, | |
HTTPSERVER_URL, | |
HTTPSERVER_HOST | |
); | |
Serial.println(buffer); | |
client->write(buffer); | |
}, NULL); | |
bool connected = _http_client->connect(HTTPSERVER_HOST, HTTPSERVER_PORT); | |
if (!connected) { | |
DEBUG_MSG_P(PSTR("[HTTPSERVER] Connection failed\n")); | |
_http_client->close(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment