Browse Source

Thingspeak: tweak SecureClient connection (#2144)

- continue #2140 , use the correct implementation for http requests not confuse code readers with our parsing
- fix data sender data duplication, run build test
- add note that this is actually really RAM heavy, some connection failures are not easily distinguishable from any code errors and are simply OOM.

also
- fix arduinoota prototype error when building without it (... ino2cpp, again)
- add comment about 160mhz into the platformio.ini
mcspr-patch-1
Max Prokhorov 4 years ago
committed by GitHub
parent
commit
fd50e95e9e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 40 deletions
  1. +3
    -0
      code/espurna/config/general.h
  2. +1
    -1
      code/espurna/ota.h
  3. +2
    -0
      code/espurna/thingspeak.h
  4. +26
    -39
      code/espurna/thinkspeak.ino
  5. +4
    -0
      code/platformio.ini

+ 3
- 0
code/espurna/config/general.h View File

@ -1420,6 +1420,9 @@
// so you should compile it with WEB_SUPPORT to 0. // so you should compile it with WEB_SUPPORT to 0.
// When THINGSPEAK_USE_ASYNC is 1, requires EspAsyncTCP to be built with ASYNC_TCP_SSL_ENABLED=1 and ESP8266 Arduino Core >= 2.4.0. // When THINGSPEAK_USE_ASYNC is 1, requires EspAsyncTCP to be built with ASYNC_TCP_SSL_ENABLED=1 and ESP8266 Arduino Core >= 2.4.0.
// When THINGSPEAK_USE_ASYNC is 0, requires Arduino Core >= 2.6.0 and SECURE_CLIENT_BEARSSL // When THINGSPEAK_USE_ASYNC is 0, requires Arduino Core >= 2.6.0 and SECURE_CLIENT_BEARSSL
//
// WARNING: Thingspeak servers do not support MFLN right now, connection requires at least 30KB of free RAM.
// Also see MQTT comments above.
#ifndef THINGSPEAK_USE_SSL #ifndef THINGSPEAK_USE_SSL
#define THINGSPEAK_USE_SSL 0 // Use secure connection #define THINGSPEAK_USE_SSL 0 // Use secure connection


+ 1
- 1
code/espurna/ota.h View File

@ -7,10 +7,10 @@ OTA MODULE
#pragma once #pragma once
#include <Updater.h> #include <Updater.h>
#include <ArduinoOTA.h>
#if OTA_ARDUINOOTA_SUPPORT #if OTA_ARDUINOOTA_SUPPORT
#include <ArduinoOTA.h>
void arduinoOtaSetup(); void arduinoOtaSetup();
#endif // OTA_ARDUINOOTA_SUPPORT == 1 #endif // OTA_ARDUINOOTA_SUPPORT == 1


+ 2
- 0
code/espurna/thingspeak.h View File

@ -12,6 +12,8 @@ Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
#if THINGSPEAK_USE_ASYNC #if THINGSPEAK_USE_ASYNC
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#else
#include <ESP8266HTTPClient.h>
#endif #endif
constexpr const size_t tspkDataBufferSize = 256; constexpr const size_t tspkDataBufferSize = 256;


+ 26
- 39
code/espurna/thinkspeak.ino View File

@ -205,13 +205,7 @@ void _tspkInitClient(const String& _url) {
unsigned int code = (p) ? atoi(&p[4]) : 0; unsigned int code = (p) ? atoi(&p[4]) : 0;
DEBUG_MSG_P(PSTR("[THINGSPEAK] Response value: %u\n"), code); DEBUG_MSG_P(PSTR("[THINGSPEAK] Response value: %u\n"), code);
if ((0 == code) && _tspk_tries) {
_tspk_flush = true;
DEBUG_MSG_P(PSTR("[THINGSPEAK] Re-enqueuing %u more time(s)\n"), _tspk_tries);
} else {
_tspkClearQueue();
}
_tspkRetry(code);
client->close(true); client->close(true);
_tspk_client_state = tspk_state_t::NONE; _tspk_client_state = tspk_state_t::NONE;
@ -293,45 +287,29 @@ SecureClientConfig _tspk_sc_config {
#endif // THINGSPEAK_USE_SSL && SECURE_CLIENT_BEARSSL #endif // THINGSPEAK_USE_SSL && SECURE_CLIENT_BEARSSL
void _tspkPost(WiFiClient* client, const URL& url) {
if (!client->connect(url.host.c_str(), url.port)) {
DEBUG_MSG_P(PSTR("[THINGSPEAK] Connection failed\n"));
return;
}
void _tspkPost(WiFiClient& client, const URL& url, bool https) {
DEBUG_MSG_P(PSTR("[THINGSPEAK] Connected to %s:%u\n"), url.host.c_str(), url.port);
DEBUG_MSG_P(PSTR("[THINGSPEAK] POST %s?%s\n"), url.path.c_str(), _tspk_data.c_str()); DEBUG_MSG_P(PSTR("[THINGSPEAK] POST %s?%s\n"), url.path.c_str(), _tspk_data.c_str());
char headers[strlen_P(THINGSPEAK_REQUEST_TEMPLATE) + url.path.length() + url.host.length() + 1];
snprintf_P(headers, sizeof(headers),
THINGSPEAK_REQUEST_TEMPLATE,
url.path.c_str(),
url.host.c_str(),
_tspk_data.length()
);
client->print(headers);
client->print(_tspk_data);
nice_delay(100);
HTTPClient http;
http.begin(client, url.host, url.port, url.path, https);
const auto response = client->readString();
int pos = response.indexOf("\r\n\r\n");
http.addHeader("User-agent", "ESPurna");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
unsigned int code = (pos > 0) ? response.substring(pos + 4).toInt() : 0;
DEBUG_MSG_P(PSTR("[THINGSPEAK] Response value: %u\n"), code);
const auto http_code = http.POST(_tspk_data);
int value = 0;
client->stop();
_tspk_last_flush = millis();
if ((0 == code) && _tspk_tries) {
_tspk_flush = true;
DEBUG_MSG_P(PSTR("[THINGSPEAK] Re-enqueuing %u more time(s)\n"), _tspk_tries);
if (http_code == 200) {
value = http.getString().toInt();
DEBUG_MSG_P(PSTR("[THINGSPEAK] Response value: %u\n"), value);
} else { } else {
_tspkClearQueue();
DEBUG_MSG_P(PSTR("[THINGSPEAK] Response HTTP code: %d\n"), http_code);
} }
_tspkRetry(value);
_tspk_data = "";
} }
void _tspkPost(const String& address) { void _tspkPost(const String& address) {
@ -351,14 +329,14 @@ void _tspkPost(const String& address) {
return; return;
} }
_tspkPost(&client->get(), url);
_tspkPost(client->get(), url, true);
return; return;
} }
#endif #endif
if (url.protocol == "http") { if (url.protocol == "http") {
auto client = std::make_unique<WiFiClient>(); auto client = std::make_unique<WiFiClient>();
_tspkPost(client.get(), url);
_tspkPost(*client.get(), url, false);
return; return;
} }
@ -385,6 +363,15 @@ void _tspkClearQueue() {
} }
} }
void _tspkRetry(int code) {
if ((0 == code) && _tspk_tries) {
_tspk_flush = true;
DEBUG_MSG_P(PSTR("[THINGSPEAK] Re-enqueuing %u more time(s)\n"), _tspk_tries);
} else {
_tspkClearQueue();
}
}
void _tspkFlush() { void _tspkFlush() {
if (!_tspk_flush) return; if (!_tspk_flush) return;


+ 4
- 0
code/platformio.ini View File

@ -61,6 +61,9 @@ debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP
# -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY = v2 Lower Memory # -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY = v2 Lower Memory
# -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH = v2 Higher Bandwidth # -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH = v2 Higher Bandwidth
# #
# BearSSL performance:
# When building with -DSECURE_CLIENT=SECURE_CLIENT_BEARSSL, please add `board_build.f_cpu = 160000000` to the environment configuration
#
# BearSSL ciphers: # BearSSL ciphers:
# When building on core >= 2.5, you can add the build flag -DBEARSSL_SSL_BASIC in order to build BearSSL with a limited set of ciphers: # When building on core >= 2.5, you can add the build flag -DBEARSSL_SSL_BASIC in order to build BearSSL with a limited set of ciphers:
# TLS_RSA_WITH_AES_128_CBC_SHA256 / AES128-SHA256 # TLS_RSA_WITH_AES_128_CBC_SHA256 / AES128-SHA256
@ -285,6 +288,7 @@ src_build_flags =
[env:nodemcu-lolin-secure-client] [env:nodemcu-lolin-secure-client]
platform = ${common.platform_latest} platform = ${common.platform_latest}
board = ${common.board_4m} board = ${common.board_4m}
board_build.f_cpu = 160000000
build_flags = build_flags =
${common.build_flags_4m1m} ${common.build_flags_4m1m}
-DDEBUG_FAUXMO=Serial -DDEBUG_FAUXMO=Serial


Loading…
Cancel
Save