Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "PubSubClient.h"
#include "Arduino.h"

#ifdef ESP32
#include <WiFi.h>
#endif

PubSubClient::PubSubClient() {
this->_state = MQTT_DISCONNECTED;
this->_client = NULL;
Expand Down Expand Up @@ -182,14 +186,23 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
if (!connected()) {
int result = 0;


if(_client->connected()) {
result = 1;
} else {
if (domain != NULL) {
#ifdef ESP32
WiFiClient* wfc = (WiFiClient*)_client;
result = wfc->connect(this->domain, this->port, ESP32_CONNECTION_TIMEOUT);
#else
result = _client->connect(this->domain, this->port);
#endif
} else {
#ifdef ESP32
WiFiClient* wfc = (WiFiClient*)_client;
result = wfc->connect(this->ip, this->port, ESP32_CONNECTION_TIMEOUT);
#else
result = _client->connect(this->ip, this->port);
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/PubSubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
#define MQTT_MAX_PACKET_SIZE 256
#endif

// ESP32_CONNECTION_TIMEOUT : Specific case for ESP32, we need to manually provide timeout as default (-1) leads to WDT reset (after 5 seconds).
// By default (4500 milliseconds) 4,5 seconds to avoid reaching 5s default watchdog reset time.
// This is multiplied in WiFiClient.cpp (part of arduino-esp32) by 1000 inside [WiFiClient::connect] method.
// ESP8266 Arduino framework in contrast has fixed 5000ms timeout. No need to define it manually here.
#ifndef ESP32_CONNECTION_TIMEOUT
#define ESP32_CONNECTION_TIMEOUT 4500
#endif

// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 15
Expand Down