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

View File

@@ -47,6 +47,7 @@ void APSettingsService::manageAP() {
void APSettingsService::startAP() {
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
esp_wifi_set_bandwidth(ESP_IF_WIFI_AP, WIFI_BW_HT20);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
if (!_dnsServer) {
IPAddress apIp = WiFi.softAPIP();

View File

@@ -101,6 +101,7 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
case SYSTEM_EVENT_ETH_GOT_IP:
case SYSTEM_EVENT_GOT_IP6:
if (_state.enabled) {
// emsesp::EMSESP::logger().info(F("Network connection found, starting MQTT client"));
onConfigUpdated();

View File

@@ -19,12 +19,6 @@ NetworkSettingsService::NetworkSettingsService(AsyncWebServer * server, FS * fs,
WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
#if defined(EMSESP_WIFI_TWEAK)
// www.esp32.com/viewtopic.php?t=12055
esp_wifi_set_bandwidth(ESP_IF_WIFI_STA, WIFI_BW_HT20);
esp_wifi_set_bandwidth(ESP_IF_WIFI_AP, WIFI_BW_HT20);
#endif
WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, _1));
addUpdateHandler([&](const String & originId) { reconfigureWiFiConnection(); }, false);
@@ -68,6 +62,19 @@ void NetworkSettingsService::manageSTA() {
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname
// www.esp32.com/viewtopic.php?t=12055
read([&](NetworkSettings & networkSettings) {
if (networkSettings.bandwidth20) {
esp_wifi_set_bandwidth(ESP_IF_WIFI_STA, WIFI_BW_HT20);
} else {
esp_wifi_set_bandwidth(ESP_IF_WIFI_STA, WIFI_BW_HT40);
}
esp_wifi_set_max_tx_power(networkSettings.tx_power * 4);
if (networkSettings.nosleep) {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
}
});
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
}
}

View File

@@ -7,9 +7,7 @@
#include <JsonUtils.h>
#ifndef EMSESP_STANDALONE
#if defined(EMSESP_WIFI_TWEAK)
#include <esp_wifi.h>
#endif
#include <ETH.h>
#endif
@@ -36,6 +34,10 @@ class NetworkSettings {
String password;
String hostname;
bool staticIPConfig;
bool enableIPv6;
bool bandwidth20;
int8_t tx_power;
bool nosleep;
// optional configuration for static IP address
IPAddress localIP;
@@ -50,6 +52,10 @@ class NetworkSettings {
root["password"] = settings.password;
root["hostname"] = settings.hostname;
root["static_ip_config"] = settings.staticIPConfig;
root["enableIPv6"] = settings.enableIPv6;
root["bandwidth20"] = settings.bandwidth20;
root["tx_power"] = settings.tx_power;
root["nosleep"] = settings.nosleep;
// extended settings
JsonUtils::writeIP(root, "local_ip", settings.localIP);
@@ -64,6 +70,10 @@ class NetworkSettings {
settings.password = root["password"] | FACTORY_WIFI_PASSWORD;
settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
settings.staticIPConfig = root["static_ip_config"] | false;
settings.enableIPv6 = root["enableIPv6"] | false;
settings.bandwidth20 = root["bandwidth20"] | false;
settings.tx_power = root["tx_power"] | 20;
settings.nosleep = root["nosleep"] | false;
// extended settings
JsonUtils::readIP(root, "local_ip", settings.localIP);