add network options, IPv6 for mqtt

This commit is contained in:
MichaelDvP
2021-07-08 10:17:50 +02:00
parent 3ea53a8012
commit 2d7449aeba
17 changed files with 180 additions and 27 deletions

View File

@@ -12,8 +12,10 @@ AsyncMqttClient::AsyncMqttClient()
, _lastPingRequestTime(0)
, _generatedClientId{0}
, _ip()
, _ipv6()
, _host(nullptr)
, _useIp(false)
, _useIpv6(false)
#if ASYNC_TCP_SSL_ENABLED
, _secure(false)
#endif
@@ -111,16 +113,33 @@ AsyncMqttClient& AsyncMqttClient::setWill(const char* topic, uint8_t qos, bool r
}
AsyncMqttClient& AsyncMqttClient::setServer(IPAddress ip, uint16_t port) {
_useIp = true;
_ip = ip;
_port = port;
_useIp = true;
_useIpv6 = false;
_ip = ip;
_port = port;
return *this;
}
AsyncMqttClient& AsyncMqttClient::setServer(IPv6Address ipv6, uint16_t port) {
_useIpv6 = true;
_useIp = false;
_ipv6 = ipv6;
_port = port;
return *this;
}
AsyncMqttClient& AsyncMqttClient::setServer(const char* host, uint16_t port) {
_useIp = false;
_host = host;
_port = port;
_port = port;
_useIp = false;
_useIpv6 = false;
_host = host;
if (_ipv6.fromString(host)) {
_useIpv6 = true;
_useIp = false;
} else if (_ip.fromString(host)) {
_useIpv6 = false;
_useIp = true;
}
return *this;
}
@@ -698,6 +717,8 @@ void AsyncMqttClient::connect() {
#else
if (_useIp) {
_client.connect(_ip, _port);
} else if (_useIpv6) {
_client.connect(_ipv6, _port);
} else {
_client.connect(_host, _port);
}

View File

@@ -62,6 +62,7 @@ class AsyncMqttClient {
AsyncMqttClient& setCredentials(const char* username, const char* password = nullptr);
AsyncMqttClient& setWill(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0);
AsyncMqttClient& setServer(IPAddress ip, uint16_t port);
AsyncMqttClient& setServer(IPv6Address ipv6, uint16_t port);
AsyncMqttClient& setServer(const char* host, uint16_t port);
#if ASYNC_TCP_SSL_ENABLED
AsyncMqttClient& setSecure(bool secure);
@@ -102,8 +103,10 @@ class AsyncMqttClient {
char _generatedClientId[18 + 1]; // esp8266-abc123 and esp32-abcdef123456
IPAddress _ip;
IPv6Address _ipv6;
const char* _host;
bool _useIp;
bool _useIpv6;
#if ASYNC_TCP_SSL_ENABLED
bool _secure;
#endif