Skip to content

Commit

Permalink
[MQTT-TLS] Improve stability of MQTT connection while serving JSON pages
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Sep 30, 2024
1 parent 695e30e commit 4917ff9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
12 changes: 12 additions & 0 deletions lib/lib_ssl/tls_mini/src/WiFiClientSecureLightBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ void WiFiClientSecure_light::flush(void) {
#endif

#ifdef ESP32

int WiFiClientSecure_light::connect(IPAddress ip, uint16_t port)
{
return connect(ip, port, _timeout);
}

int WiFiClientSecure_light::connect(const char* name, uint16_t port)
{
return connect(name, port, _timeout);
}


int WiFiClientSecure_light::connect(IPAddress ip, uint16_t port, int32_t timeout) {
DEBUG_BSSL("connect(%s,%d)", ip.toString().c_str(), port);
clearLastError();
Expand Down
6 changes: 4 additions & 2 deletions lib/lib_ssl/tls_mini/src/WiFiClientSecureLightBearSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class WiFiClientSecure_light : public NetworkClient {
void allocateBuffers(void);

#ifdef ESP32 // the method to override in ESP32 has timeout argument default #define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000)
int connect(IPAddress ip, uint16_t port, int32_t timeout = 3000) override;
int connect(const char* name, uint16_t port, int32_t timeout = 3000) override;
int connect(IPAddress ip, uint16_t port) override;
int connect(const char* name, uint16_t port) override;
int connect(IPAddress ip, uint16_t port, int32_t timeout);
int connect(const char* name, uint16_t port, int32_t timeout);
#else
int connect(IPAddress ip, uint16_t port) override;
int connect(const char* name, uint16_t port) override;
Expand Down
1 change: 1 addition & 0 deletions lib/pubsubclient/src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ bool PubSubClient::loop_read() {
}

boolean PubSubClient::loop() {
delay(0);
loop_read();
if (connected()) {
unsigned long t = millis();
Expand Down
2 changes: 1 addition & 1 deletion src/ESPEasy-Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ boolean printToWebJSON = false;



unsigned long timermqtt_interval = 250;
unsigned long timermqtt_interval = 100;
unsigned long lastSend = 0;
unsigned long lastWeb = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/src/ESPEasyCore/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ bool MQTTConnect(controllerIndex_t controller_idx)
mqtt.setTimeout(timeout); // in msec as it should be!
# endif // ifdef MUSTFIX_CLIENT_TIMEOUT_IN_SECONDS
MQTTclient.setClient(mqtt);
MQTTclient.setKeepAlive(10);
MQTTclient.setSocketTimeout(timeout);
break;
}

Expand Down Expand Up @@ -357,6 +359,9 @@ bool MQTTConnect(controllerIndex_t controller_idx)
mqtt_tls->setBufferSizes(1024, 1024);
# endif // ifdef ESP8266
MQTTclient.setClient(*mqtt_tls);
MQTTclient.setKeepAlive(10);
MQTTclient.setSocketTimeout(timeout);


if (mqttPort == 1883) {
mqttPort = 8883;
Expand Down
2 changes: 1 addition & 1 deletion src/src/ESPEasyCore/ESPEasy_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void ESPEasy_setup()
}
#endif

timermqtt_interval = 250; // Interval for checking MQTT
timermqtt_interval = 100; // Interval for checking MQTT
timerAwakeFromDeepSleep = millis();
CPluginInit();
#ifndef BUILD_NO_RAM_TRACKER
Expand Down
4 changes: 2 additions & 2 deletions src/src/Helpers/PeriodicalActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ void updateMQTTclient_connected() {
if (!MQTTclient_connected) {
// As suggested here: https://github.com/letscontrolit/ESPEasy/issues/1356
if (timermqtt_interval < 30000) {
timermqtt_interval += 5000;
timermqtt_interval += 500;
}
} else {
timermqtt_interval = 250;
timermqtt_interval = 100;
}
Scheduler.setIntervalTimer(SchedulerIntervalTimer_e::TIMER_MQTT);
scheduleNextMQTTdelayQueue();
Expand Down

0 comments on commit 4917ff9

Please sign in to comment.