* Asyncwebserver * Squashed commit of the following: commit 575ef02f593918ec6654c87407a4d11fc17071b8 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:56:11 2024 +0200 merge master commit 35e5adf4ecd80f9829e8801181f35dd2c1d94759 Merge: a2cc7be221adca01Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:41:04 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit a2cc7be2954cbd8767ab8186296c0b14134d1d0b Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:51:50 2024 +0200 update nuki ble commit 20c809f3dca28b29b219d1ff3a183f1981316de5 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:44:46 2024 +0200 backup commit dd41c218efb5270f5efeb734e64dff695920db16 Merge: 153000b5e84b944aAuthor: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:40:03 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit 153000b5b1af7df1fbeb5263df94eb26f689cc0a Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:23:07 2024 +0200 fix linker error commit a93bbfbfc4301e46ff3696a763dd13c6c89efefb Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:27:07 2024 +0200 backup commit f611c75ce8c35f829bcad6cf7e86188f4b3ec331 Merge: f1964917063fbab6Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:24:47 2024 +0200 merge master commit f1964917b4dade3920f1ecdb699c58630199e6da Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 15:17:45 2024 +0200 update platformio.ini commit f448e5e8a7e93be38e09e2ab0b622199a3721af6 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:28:09 2024 +0200 add SPIClass instance for DM9051 commit 1f190e9aa08033535a2eb442a92e6e20409bbda1 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:22:26 2024 +0200 add definitions and constructor for DM9051 commit 726b3602ae91594ee1210ad5b6714f75cc5e42a7 Merge: 50a2eb134af90cbcAuthor: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 10:19:34 2024 +0200 merge master commit 50a2eb136d75d90921f1c6974f18bc107bddc123 Author: technyon <j.o.schuemann@gmx.de> Date: Fri Aug 9 11:52:09 2024 +0200 add comment commit 9437e485cae169efdf8e5a7bf188a1c7e792d1e5 Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 4 08:29:21 2024 +0200 move LAN8720 definitions to seperate file * Remove Core 2 Ethernet library * Custom Ethernet * GPIO and Preferences * H2
217 lines
6.3 KiB
C++
217 lines
6.3 KiB
C++
#include <WiFi.h>
|
|
#include "WifiDevice.h"
|
|
#include "../PreferencesKeys.h"
|
|
#include "../Logger.h"
|
|
#ifndef NUKI_HUB_UPDATER
|
|
#include "../MqttTopics.h"
|
|
#include "espMqttClient.h"
|
|
#endif
|
|
#include "../RestartReason.h"
|
|
|
|
RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17];
|
|
|
|
WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
|
: NetworkDevice(hostname, ipConfiguration),
|
|
_preferences(preferences),
|
|
_wm(preferences->getString(preference_cred_user, "").c_str(), preferences->getString(preference_cred_password, "").c_str())
|
|
{
|
|
_startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0;
|
|
|
|
#ifndef NUKI_HUB_UPDATER
|
|
_restartOnDisconnect = preferences->getBool(preference_restart_on_disconnect, false);
|
|
|
|
size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
|
size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
|
size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
|
|
|
_useEncryption = caLength > 1; // length is 1 when empty
|
|
|
|
if(_useEncryption)
|
|
{
|
|
Log->println(F("MQTT over TLS."));
|
|
Log->println(_ca);
|
|
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
|
_mqttClientSecure->setCACert(_ca);
|
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
|
{
|
|
Log->println(F("MQTT with client certificate."));
|
|
Log->println(_cert);
|
|
Log->println(_key);
|
|
_mqttClientSecure->setCertificate(_cert);
|
|
_mqttClientSecure->setPrivateKey(_key);
|
|
}
|
|
} else
|
|
{
|
|
Log->println(F("MQTT without TLS."));
|
|
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
|
}
|
|
|
|
if(preferences->getBool(preference_mqtt_log_enabled, false) || preferences->getBool(preference_webserial_enabled, false))
|
|
{
|
|
MqttLoggerMode mode;
|
|
|
|
if(preferences->getBool(preference_mqtt_log_enabled, false) && preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
|
|
else if (preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
|
|
else mode = MqttLoggerMode::MqttAndSerial;
|
|
|
|
_path = new char[200];
|
|
memset(_path, 0, sizeof(_path));
|
|
|
|
String pathStr = preferences->getString(preference_mqtt_lock_path);
|
|
pathStr.concat(mqtt_topic_log);
|
|
strcpy(_path, pathStr.c_str());
|
|
Log = new MqttLogger(*getMqttClient(), _path, mode);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
const String WifiDevice::deviceName() const
|
|
{
|
|
return "Built-in Wi-Fi";
|
|
}
|
|
|
|
void WifiDevice::initialize()
|
|
{
|
|
_wifiFallbackDisabled = _preferences->getBool(preference_network_wifi_fallback_disabled, false);
|
|
std::vector<const char *> wm_menu;
|
|
wm_menu.push_back("wifi");
|
|
wm_menu.push_back("exit");
|
|
_wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
|
|
// reduced timeout if ESP is set to restart on disconnect
|
|
_wm.setFindBestRSSI(_preferences->getBool(preference_find_best_rssi));
|
|
_wm.setConnectTimeout(20);
|
|
_wm.setConfigPortalTimeout(_restartOnDisconnect ? 60 * 3 : 60 * 30);
|
|
_wm.setShowInfoUpdate(false);
|
|
_wm.setMenu(wm_menu);
|
|
_wm.setHostname(_hostname);
|
|
|
|
if(!_ipConfiguration->dhcpEnabled())
|
|
{
|
|
_wm.setSTAStaticIPConfig(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
|
|
}
|
|
|
|
_wm.setAPCallback(clearRtcInitVar);
|
|
|
|
bool res = false;
|
|
bool connectedFromPortal = false;
|
|
|
|
if(_startAp)
|
|
{
|
|
Log->println(F("Opening Wi-Fi configuration portal."));
|
|
res = _wm.startConfigPortal();
|
|
connectedFromPortal = true;
|
|
}
|
|
else
|
|
{
|
|
res = _wm.autoConnect(); // password protected ap
|
|
}
|
|
|
|
if(!res)
|
|
{
|
|
esp_wifi_disconnect();
|
|
esp_wifi_stop();
|
|
esp_wifi_deinit();
|
|
|
|
Log->println(F("Failed to connect. Wait for ESP restart."));
|
|
delay(1000);
|
|
restartEsp(RestartReason::WifiInitFailed);
|
|
}
|
|
else {
|
|
Log->print(F("Wi-Fi connected: "));
|
|
Log->println(WiFi.localIP().toString());
|
|
|
|
if(connectedFromPortal)
|
|
{
|
|
Log->println(F("Connected using WifiManager portal. Wait for ESP restart."));
|
|
delay(1000);
|
|
restartEsp(RestartReason::ConfigurationUpdated);
|
|
}
|
|
}
|
|
|
|
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
|
{
|
|
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED)
|
|
{
|
|
onDisconnected();
|
|
}
|
|
else if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP)
|
|
{
|
|
onConnected();
|
|
}
|
|
});
|
|
}
|
|
|
|
void WifiDevice::reconfigure()
|
|
{
|
|
strcpy(WiFiDevice_reconfdetect, "reconfigure_wifi");
|
|
delay(200);
|
|
restartEsp(RestartReason::ReconfigureWifi);
|
|
}
|
|
|
|
bool WifiDevice::supportsEncryption()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool WifiDevice::isConnected()
|
|
{
|
|
return WiFi.isConnected();
|
|
}
|
|
|
|
ReconnectStatus WifiDevice::reconnect(bool force)
|
|
{
|
|
if((!isConnected() || force) && !_isReconnecting)
|
|
{
|
|
_isReconnecting = true;
|
|
WiFi.disconnect();
|
|
int loop = 0;
|
|
|
|
while(isConnected() && loop <20)
|
|
{
|
|
delay(100);
|
|
loop++;
|
|
}
|
|
|
|
_wm.resetScan();
|
|
_wm.autoConnect();
|
|
_isReconnecting = false;
|
|
}
|
|
|
|
if(!isConnected() && _disconnectTs > (esp_timer_get_time() / 1000) - 120000) _wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
|
|
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
|
}
|
|
|
|
void WifiDevice::onConnected()
|
|
{
|
|
_isReconnecting = false;
|
|
_wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
|
|
}
|
|
|
|
void WifiDevice::onDisconnected()
|
|
{
|
|
_disconnectTs = (esp_timer_get_time() / 1000);
|
|
if(_restartOnDisconnect && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
|
_wm.setEnableConfigPortal(false);
|
|
reconnect();
|
|
}
|
|
|
|
int8_t WifiDevice::signalStrength()
|
|
{
|
|
return WiFi.RSSI();
|
|
}
|
|
|
|
String WifiDevice::localIP()
|
|
{
|
|
return WiFi.localIP().toString();
|
|
}
|
|
|
|
String WifiDevice::BSSIDstr()
|
|
{
|
|
return WiFi.BSSIDstr();
|
|
}
|
|
|
|
void WifiDevice::clearRtcInitVar(WiFiManager *)
|
|
{
|
|
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
|
}
|