let WifiManager handle the reconnect
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define nuki_hub_version "4.3"
|
#define nuki_hub_version "4.4"
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include "WifiDevice.h"
|
#include "WifiDevice.h"
|
||||||
#include "WiFiManager.h"
|
|
||||||
#include "../PreferencesKeys.h"
|
#include "../PreferencesKeys.h"
|
||||||
|
|
||||||
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||||
@@ -42,14 +41,13 @@ void WifiDevice::initialize()
|
|||||||
{
|
{
|
||||||
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
||||||
|
|
||||||
WiFiManager wm;
|
|
||||||
|
|
||||||
std::vector<const char *> wm_menu;
|
std::vector<const char *> wm_menu;
|
||||||
wm_menu.push_back("wifi");
|
wm_menu.push_back("wifi");
|
||||||
wm_menu.push_back("exit");
|
wm_menu.push_back("exit");
|
||||||
wm.setShowInfoUpdate(false);
|
_wm.setShowInfoUpdate(false);
|
||||||
wm.setMenu(wm_menu);
|
_wm.setMenu(wm_menu);
|
||||||
wm.setHostname(_hostname);
|
_wm.setHostname(_hostname);
|
||||||
|
|
||||||
bool res = false;
|
bool res = false;
|
||||||
|
|
||||||
@@ -57,11 +55,11 @@ void WifiDevice::initialize()
|
|||||||
{
|
{
|
||||||
Serial.println(F("Opening WiFi configuration portal."));
|
Serial.println(F("Opening WiFi configuration portal."));
|
||||||
_cookie.clear();
|
_cookie.clear();
|
||||||
res = wm.startConfigPortal();
|
res = _wm.startConfigPortal();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
res = wm.autoConnect(); // password protected ap
|
res = _wm.autoConnect(); // password protected ap
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!res) {
|
if(!res) {
|
||||||
@@ -74,9 +72,6 @@ void WifiDevice::initialize()
|
|||||||
Serial.println(WiFi.localIP().toString());
|
Serial.println(WiFi.localIP().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
_wifiSSID = wm.getWiFiSSID();
|
|
||||||
_wifiPSK = wm.getWiFiPass();
|
|
||||||
|
|
||||||
_mqttClient->setBufferSize(_mqttMaxBufferSize);
|
_mqttClient->setBufferSize(_mqttMaxBufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,15 +101,7 @@ bool WifiDevice::isConnected()
|
|||||||
|
|
||||||
bool WifiDevice::reconnect()
|
bool WifiDevice::reconnect()
|
||||||
{
|
{
|
||||||
WiFi.disconnect();
|
delay(3000);
|
||||||
WiFi.begin(_wifiSSID.c_str(), _wifiPSK.c_str());
|
|
||||||
|
|
||||||
unsigned long timeout = millis() + 5000;
|
|
||||||
while(!isConnected() && millis() < timeout)
|
|
||||||
{
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
return isConnected();
|
return isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "../SpiffsCookie.h"
|
#include "../SpiffsCookie.h"
|
||||||
|
#include "WiFiManager.h"
|
||||||
|
|
||||||
class WifiDevice : public NetworkDevice
|
class WifiDevice : public NetworkDevice
|
||||||
{
|
{
|
||||||
@@ -23,14 +24,12 @@ public:
|
|||||||
virtual PubSubClient *mqttClient();
|
virtual PubSubClient *mqttClient();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
WiFiManager _wm;
|
||||||
WiFiClient* _wifiClient = nullptr;
|
WiFiClient* _wifiClient = nullptr;
|
||||||
WiFiClientSecure* _wifiClientSecure = nullptr;
|
WiFiClientSecure* _wifiClientSecure = nullptr;
|
||||||
PubSubClient* _mqttClient = nullptr;
|
PubSubClient* _mqttClient = nullptr;
|
||||||
SpiffsCookie _cookie;
|
SpiffsCookie _cookie;
|
||||||
|
|
||||||
String _wifiSSID = "";
|
|
||||||
String _wifiPSK = "";
|
|
||||||
|
|
||||||
char _ca[TLS_CA_MAX_SIZE];
|
char _ca[TLS_CA_MAX_SIZE];
|
||||||
char _cert[TLS_CERT_MAX_SIZE];
|
char _cert[TLS_CERT_MAX_SIZE];
|
||||||
char _key[TLS_KEY_MAX_SIZE];
|
char _key[TLS_KEY_MAX_SIZE];
|
||||||
|
|||||||
Reference in New Issue
Block a user