@@ -5,7 +5,7 @@
|
|||||||
#define NUKI_HUB_VERSION "9.06"
|
#define NUKI_HUB_VERSION "9.06"
|
||||||
#define NUKI_HUB_VERSION_INT (uint32_t)906
|
#define NUKI_HUB_VERSION_INT (uint32_t)906
|
||||||
#define NUKI_HUB_BUILD "unknownbuildnr"
|
#define NUKI_HUB_BUILD "unknownbuildnr"
|
||||||
#define NUKI_HUB_DATE "2024-12-26"
|
#define NUKI_HUB_DATE "2024-12-27"
|
||||||
|
|
||||||
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
|
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
|
||||||
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"
|
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"
|
||||||
|
|||||||
@@ -637,6 +637,11 @@ bool NukiNetwork::reconnect()
|
|||||||
{
|
{
|
||||||
Log->println(F("MQTT Broker not configured, aborting connection attempt."));
|
Log->println(F("MQTT Broker not configured, aborting connection attempt."));
|
||||||
_nextReconnect = espMillis() + 5000;
|
_nextReconnect = espMillis() + 5000;
|
||||||
|
|
||||||
|
if(_device->isConnected())
|
||||||
|
{
|
||||||
|
_lastConnectedTs = espMillis();
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ const String WifiDevice::deviceName() const
|
|||||||
void WifiDevice::initialize()
|
void WifiDevice::initialize()
|
||||||
{
|
{
|
||||||
ssid = _preferences->getString(preference_wifi_ssid, "");
|
ssid = _preferences->getString(preference_wifi_ssid, "");
|
||||||
|
ssid.trim();
|
||||||
pass = _preferences->getString(preference_wifi_pass, "");
|
pass = _preferences->getString(preference_wifi_pass, "");
|
||||||
|
pass.trim();
|
||||||
WiFi.setHostname(_hostname.c_str());
|
WiFi.setHostname(_hostname.c_str());
|
||||||
|
|
||||||
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
|
||||||
@@ -29,39 +31,30 @@ void WifiDevice::initialize()
|
|||||||
onWifiEvent(event, info);
|
onWifiEvent(event, info);
|
||||||
});
|
});
|
||||||
|
|
||||||
ssid.trim();
|
|
||||||
pass.trim();
|
|
||||||
|
|
||||||
if(isWifiConfigured())
|
if(isWifiConfigured())
|
||||||
{
|
{
|
||||||
Log->println(String("Attempting to connect to saved SSID ") + String(ssid));
|
Log->println(String("Attempting to connect to saved SSID ") + String(ssid));
|
||||||
_connectOnScanDone = true;
|
|
||||||
_openAP = false;
|
_openAP = false;
|
||||||
scan(false, true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if(!_preferences->getBool(preference_wifi_converted, false))
|
|
||||||
{
|
|
||||||
_connectOnScanDone = false;
|
|
||||||
_openAP = false;
|
|
||||||
_convertOldWiFi = true;
|
|
||||||
scan(false, true);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log->println("No SSID or Wifi password saved, opening AP");
|
Log->println("No SSID or Wifi password saved, opening AP");
|
||||||
_connectOnScanDone = false;
|
|
||||||
_openAP = true;
|
_openAP = true;
|
||||||
|
}
|
||||||
|
|
||||||
scan(false, true);
|
scan(false, true);
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiDevice::scan(bool passive, bool async)
|
void WifiDevice::scan(bool passive, bool async)
|
||||||
{
|
{
|
||||||
if(!_connecting)
|
if (!_openAP)
|
||||||
{
|
{
|
||||||
|
WiFi.disconnect(true);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
WiFi.scanDelete();
|
WiFi.scanDelete();
|
||||||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
||||||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
||||||
@@ -82,13 +75,13 @@ void WifiDevice::scan(bool passive, bool async)
|
|||||||
{
|
{
|
||||||
WiFi.scanNetworks(async);
|
WiFi.scanNetworks(async);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiDevice::openAP()
|
void WifiDevice::openAP()
|
||||||
{
|
{
|
||||||
if(_startAP)
|
if(_startAP)
|
||||||
{
|
{
|
||||||
|
Log->println("Starting AP with SSID NukiHub and Password NukiHubESP32");
|
||||||
_startAP = false;
|
_startAP = false;
|
||||||
WiFi.mode(WIFI_AP);
|
WiFi.mode(WIFI_AP);
|
||||||
delay(500);
|
delay(500);
|
||||||
@@ -104,7 +97,6 @@ void WifiDevice::openAP()
|
|||||||
|
|
||||||
bool WifiDevice::connect()
|
bool WifiDevice::connect()
|
||||||
{
|
{
|
||||||
bool ret = false;
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.setHostname(_hostname.c_str());
|
WiFi.setHostname(_hostname.c_str());
|
||||||
delay(500);
|
delay(500);
|
||||||
@@ -151,66 +143,43 @@ bool WifiDevice::connect()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_connecting = true;
|
|
||||||
esp_wifi_scan_stop();
|
|
||||||
|
|
||||||
if(!_ipConfiguration->dhcpEnabled())
|
if(!_ipConfiguration->dhcpEnabled())
|
||||||
{
|
{
|
||||||
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFi.begin(ssid, pass);
|
WiFi.begin(ssid, pass);
|
||||||
auto status = WiFi.waitForConnectResult(10000);
|
|
||||||
|
|
||||||
switch (status)
|
Log->print("WiFi connecting");
|
||||||
|
int loop = 0;
|
||||||
|
while(!isConnected() && loop < 150)
|
||||||
{
|
{
|
||||||
case WL_CONNECTED:
|
Log->print(".");
|
||||||
Log->println("WiFi connected");
|
delay(100);
|
||||||
break;
|
loop++;
|
||||||
case WL_NO_SSID_AVAIL:
|
|
||||||
Log->println("WiFi SSID not available");
|
|
||||||
break;
|
|
||||||
case WL_CONNECT_FAILED:
|
|
||||||
Log->println("WiFi connection failed");
|
|
||||||
break;
|
|
||||||
case WL_IDLE_STATUS:
|
|
||||||
Log->println("WiFi changing status");
|
|
||||||
break;
|
|
||||||
case WL_DISCONNECTED:
|
|
||||||
Log->println("WiFi disconnected");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Log->println("WiFi timeout");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
Log->println("");
|
||||||
|
|
||||||
if (status != WL_CONNECTED)
|
if (!isConnected())
|
||||||
{
|
{
|
||||||
Log->println("Retrying");
|
Log->println("Failed to connect within 15 seconds");
|
||||||
_connectOnScanDone = true;
|
|
||||||
_openAP = false;
|
|
||||||
scan(false, true);
|
|
||||||
_connecting = false;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(!_preferences->getBool(preference_wifi_converted, false))
|
|
||||||
{
|
|
||||||
_preferences->putBool(preference_wifi_converted, true);
|
|
||||||
}
|
|
||||||
_connecting = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000))
|
if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000))
|
||||||
{
|
{
|
||||||
|
Log->println("Restart on disconnect watchdog triggered, rebooting");
|
||||||
|
delay(100);
|
||||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||||
_connecting = false;
|
}
|
||||||
return false;
|
else
|
||||||
|
{
|
||||||
|
Log->println("Retrying WiFi connection");
|
||||||
|
scan(false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiDevice::isWifiConfigured() const
|
bool WifiDevice::isWifiConfigured() const
|
||||||
@@ -228,16 +197,7 @@ void WifiDevice::reconfigure()
|
|||||||
|
|
||||||
bool WifiDevice::isConnected()
|
bool WifiDevice::isConnected()
|
||||||
{
|
{
|
||||||
if (WiFi.status() != WL_CONNECTED)
|
return WiFi.isConnected();
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!_hasIP)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiDevice::onConnected()
|
void WifiDevice::onConnected()
|
||||||
@@ -255,56 +215,7 @@ void WifiDevice::onDisconnected()
|
|||||||
_connected = false;
|
_connected = false;
|
||||||
|
|
||||||
Log->println("Wi-Fi disconnected");
|
Log->println("Wi-Fi disconnected");
|
||||||
|
connect();
|
||||||
//QUICK RECONNECT
|
|
||||||
_connecting = true;
|
|
||||||
|
|
||||||
if(!_ipConfiguration->dhcpEnabled())
|
|
||||||
{
|
|
||||||
WiFi.config(_ipConfiguration->ipAddress(), _ipConfiguration->dnsServer(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet());
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.begin(ssid, pass);
|
|
||||||
|
|
||||||
int loop = 0;
|
|
||||||
|
|
||||||
while(!isConnected() && loop < 200)
|
|
||||||
{
|
|
||||||
loop++;
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
_connecting = false;
|
|
||||||
//END QUICK RECONNECT
|
|
||||||
|
|
||||||
if(!isConnected())
|
|
||||||
{
|
|
||||||
if(_preferences->getBool(preference_restart_on_disconnect, false) && (espMillis() > 60000))
|
|
||||||
{
|
|
||||||
Log->println("Restart on disconnect watchdog triggered, rebooting");
|
|
||||||
delay(100);
|
|
||||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.disconnect(true);
|
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
WiFi.disconnect();
|
|
||||||
delay(500);
|
|
||||||
|
|
||||||
wifi_mode_t wifiMode;
|
|
||||||
esp_wifi_get_mode(&wifiMode);
|
|
||||||
|
|
||||||
while (wifiMode != WIFI_MODE_STA || WiFi.status() == WL_CONNECTED)
|
|
||||||
{
|
|
||||||
delay(500);
|
|
||||||
Log->println("Waiting for WiFi mode change or disconnection.");
|
|
||||||
esp_wifi_get_mode(&wifiMode);
|
|
||||||
}
|
|
||||||
|
|
||||||
_connectOnScanDone = true;
|
|
||||||
_openAP = false;
|
|
||||||
scan(false, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t WifiDevice::signalStrength()
|
int8_t WifiDevice::signalStrength()
|
||||||
@@ -329,29 +240,14 @@ bool WifiDevice::isApOpen()
|
|||||||
|
|
||||||
void WifiDevice::onWifiEvent(const WiFiEvent_t &event, const WiFiEventInfo_t &info)
|
void WifiDevice::onWifiEvent(const WiFiEvent_t &event, const WiFiEventInfo_t &info)
|
||||||
{
|
{
|
||||||
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED || event == ARDUINO_EVENT_WIFI_STA_STOP)
|
Log->printf("[WiFi-event] event: %d\n", event);
|
||||||
{
|
|
||||||
if(!_openAP && !_connecting && _connected)
|
switch (event) {
|
||||||
{
|
case ARDUINO_EVENT_WIFI_READY:
|
||||||
onDisconnected();
|
Log->println("WiFi interface ready");
|
||||||
_hasIP = false;
|
break;
|
||||||
}
|
case ARDUINO_EVENT_WIFI_SCAN_DONE:
|
||||||
}
|
Log->println("Completed scan for access points");
|
||||||
else if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP)
|
|
||||||
{
|
|
||||||
_hasIP = true;
|
|
||||||
}
|
|
||||||
else if(event == ARDUINO_EVENT_WIFI_STA_LOST_IP)
|
|
||||||
{
|
|
||||||
_hasIP = false;
|
|
||||||
}
|
|
||||||
else if(event == ARDUINO_EVENT_WIFI_STA_CONNECTED)
|
|
||||||
{
|
|
||||||
onConnected();
|
|
||||||
}
|
|
||||||
else if(event == ARDUINO_EVENT_WIFI_SCAN_DONE)
|
|
||||||
{
|
|
||||||
Log->println("Wi-Fi scan done");
|
|
||||||
_foundNetworks = WiFi.scanComplete();
|
_foundNetworks = WiFi.scanComplete();
|
||||||
|
|
||||||
for (int i = 0; i < _foundNetworks; i++)
|
for (int i = 0; i < _foundNetworks; i++)
|
||||||
@@ -367,65 +263,84 @@ void WifiDevice::onWifiEvent(const WiFiEvent_t &event, const WiFiEventInfo_t &in
|
|||||||
{
|
{
|
||||||
openAP();
|
openAP();
|
||||||
}
|
}
|
||||||
else if(_convertOldWiFi)
|
else if (_foundNetworks > 0 || _preferences->getBool(preference_find_best_rssi, false))
|
||||||
{
|
|
||||||
Log->println("Trying to convert old WiFi settings");
|
|
||||||
_convertOldWiFi = false;
|
|
||||||
_preferences->putBool(preference_wifi_converted, true);
|
|
||||||
|
|
||||||
wifi_config_t wifi_cfg;
|
|
||||||
if(esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) != ESP_OK)
|
|
||||||
{
|
|
||||||
Log->println("Failed to get Wi-Fi configuration in RAM");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) != ESP_OK)
|
|
||||||
{
|
|
||||||
Log->println("Failed to set storage Wi-Fi");
|
|
||||||
}
|
|
||||||
|
|
||||||
String tempSSID = String(reinterpret_cast<const char*>(wifi_cfg.sta.ssid));
|
|
||||||
String tempPass = String(reinterpret_cast<const char*>(wifi_cfg.sta.password));
|
|
||||||
tempSSID.trim();
|
|
||||||
tempPass.trim();
|
|
||||||
bool found = false;
|
|
||||||
|
|
||||||
for (int i = 0; i < _foundNetworks; i++)
|
|
||||||
{
|
|
||||||
if(tempSSID.length() > 0 && tempSSID == WiFi.SSID(i) && tempPass.length() > 0)
|
|
||||||
{
|
|
||||||
_preferences->putString(preference_wifi_ssid, tempSSID);
|
|
||||||
_preferences->putString(preference_wifi_pass, tempPass);
|
|
||||||
Log->println("Succesfully converted old WiFi settings");
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.disconnect(true, true);
|
|
||||||
|
|
||||||
if(found)
|
|
||||||
{
|
|
||||||
Log->println(String("Attempting to connect to saved SSID ") + String(ssid));
|
|
||||||
_connectOnScanDone = true;
|
|
||||||
_openAP = false;
|
|
||||||
scan(false, true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
restartEsp(RestartReason::ReconfigureWifi);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ((_connectOnScanDone && _foundNetworks > 0) || _preferences->getBool(preference_find_best_rssi, false))
|
|
||||||
{
|
{
|
||||||
|
esp_wifi_scan_stop();
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
else if (_connectOnScanDone)
|
else
|
||||||
{
|
{
|
||||||
Log->println("No networks found, restarting scan");
|
Log->println("No networks found, restarting scan");
|
||||||
scan(false, true);
|
scan(false, true);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_START:
|
||||||
|
Log->println("WiFi client started");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_STOP:
|
||||||
|
Log->println("WiFi clients stopped");
|
||||||
|
if(!_openAP)
|
||||||
|
{
|
||||||
|
onDisconnected();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
|
Log->println("Connected to access point");
|
||||||
|
if(!_openAP)
|
||||||
|
{
|
||||||
|
onConnected();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||||
|
Log->println("Disconnected from WiFi access point");
|
||||||
|
if(!_openAP)
|
||||||
|
{
|
||||||
|
onDisconnected();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
|
||||||
|
Log->println("Authentication mode of access point has changed");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
|
Log->print("Obtained IP address: ");
|
||||||
|
Log->println(WiFi.localIP());
|
||||||
|
if(!_openAP)
|
||||||
|
{
|
||||||
|
onConnected();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||||
|
Log->println("Lost IP address and IP address is reset to 0");
|
||||||
|
if(!_openAP)
|
||||||
|
{
|
||||||
|
onDisconnected();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_START:
|
||||||
|
Log->println("WiFi access point started");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_STOP:
|
||||||
|
Log->println("WiFi access point stopped");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
|
||||||
|
Log->println("Client connected");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
|
||||||
|
Log->println("Client disconnected");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
|
||||||
|
Log->println("Assigned IP address to client");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
|
||||||
|
Log->println("Received probe request");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_AP_GOT_IP6:
|
||||||
|
Log->println("AP IPv6 is preferred");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
||||||
|
Log->println("STA IPv6 is preferred");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,11 +41,7 @@ private:
|
|||||||
String pass;
|
String pass;
|
||||||
|
|
||||||
int _foundNetworks = 0;
|
int _foundNetworks = 0;
|
||||||
bool _connectOnScanDone = false;
|
|
||||||
bool _connecting = false;
|
|
||||||
bool _openAP = false;
|
bool _openAP = false;
|
||||||
bool _startAP = true;
|
bool _startAP = true;
|
||||||
bool _convertOldWiFi = false;
|
|
||||||
bool _connected = false;
|
bool _connected = false;
|
||||||
bool _hasIP = false;
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user