From f9e954b3d809b99020157c9dbeb8be28f4de680e Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Mar 2018 15:33:52 +0100 Subject: [PATCH] make httpclient retry --- src/main.cpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0121a17..1aaa2d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,25 +174,30 @@ void ICACHE_FLASH_ATTR sendValues() { char buffer[500]; array.printTo(buffer, sizeof(buffer)); - HTTPClient httpclient; - char url[100]; - sprintf(url, "http://%s/boxes/%s/data", server, SENSEBOX_ID); + int httpCode = 0; + for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) { - httpclient.begin(url); - httpclient.addHeader("Content-Type", "application/json"); + HTTPClient httpclient; + char url[100]; + sprintf(url, "http://%s/boxes/%s/data", server, SENSEBOX_ID); - int httpCode = httpclient.POST(buffer); + httpclient.begin(url); + httpclient.addHeader("Content-Type", "application/json"); - if (httpCode > 0) { - if (httpCode == HTTP_CODE_CREATED) { - httpclient.writeToStream(&Serial); - Serial.println(); - } else { - Serial.printf("[HTTP] POST... failed, error: %s\n", httpclient.errorToString(httpCode).c_str()); + httpCode = httpclient.POST(buffer); + + if (httpCode > 0) { + if (httpCode == HTTP_CODE_CREATED) { + httpclient.writeToStream(&Serial); + Serial.println(); + } else { + Serial.printf("[HTTP] POST... failed, error: %s\n", httpclient.errorToString(httpCode).c_str()); + } } - } - httpclient.end(); + httpclient.end(); + + } WiFi.disconnect(); WiFi.mode(WIFI_OFF);