Fixes in new API functions

This commit is contained in:
Martin Bauer
2021-07-22 18:38:28 +02:00
parent 06c3015d22
commit fb0b455910
7 changed files with 96 additions and 42 deletions

View File

@@ -1,8 +1,9 @@
#include "WifiManager.h"
void WifiManager::begin(const String &hostname)
void WifiManager::begin(const String &hostname, const String & wifiStaModeName)
{
hostname_ = hostname;
wifiStaModeName_ = wifiStaModeName;
prefs_.begin("st_wifi_manager");
startWifi();
}
@@ -20,19 +21,24 @@ void WifiManager::startWifi()
// station mode
WiFi.mode(WIFI_STA);
WiFi.begin(staSSID.c_str(), staPassword.c_str());
Serial.printf("Starting WiFi station mode to ssid %s, hostname %s\n", staSSID.c_str(), hostname_.c_str());
WiFi.setHostname(hostname_.c_str());
int connectCounter = 0;
bool successful = true;
delay(5000);
while (WiFi.status() != WL_CONNECTED)
{
delay(2000);
Serial.printf("WiFI connection problem %d\n", WiFi.status());
WiFi.begin(staSSID.c_str(), staPassword.c_str());
connectCounter += 1;
if (connectCounter >= 60) // for two minutes no connection
{
successful = false;
break; // fallback to AP mode
}
delay(5000);
}
state_ = STA;
if (successful)
@@ -42,14 +48,17 @@ void WifiManager::startWifi()
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(192, 168, 42, 1), IPAddress(192, 168, 42, 1), IPAddress(255, 255, 255, 0));
WiFi.softAPsetHostname(hostname_.c_str());
if (apPassword.length() > 0)
{
WiFi.softAP(hostname_.c_str(), apPassword.c_str());
Serial.printf("Secured AP mode, name %s\n", wifiStaModeName_.c_str());
WiFi.softAP(wifiStaModeName_.c_str(), apPassword.c_str());
state_ = AP_SECURE;
}
else
{
WiFi.softAP(hostname_.c_str());
Serial.printf("Provisioning AP mode, name %s\n", wifiStaModeName_.c_str());
WiFi.softAP(wifiStaModeName_.c_str());
state_ = AP_PROVISIONING;
}
}
@@ -80,15 +89,14 @@ void WifiManager::resetToApProvisioning()
void WifiManager::iteration()
{
if (state_ == STA && WiFi.status() != WL_CONNECTED) {
if (state_ == STA && WiFi.status() != WL_CONNECTED)
{
startWifi();
Serial.println("Connection lost - Restarting WIFI");
}
}
const char * WifiManager::stateToString(WifiManager::State state)
const char *WifiManager::stateToString(WifiManager::State state)
{
switch (state)
{

View File

@@ -28,7 +28,7 @@ public:
};
WifiManager() : state_(INVALID) {}
void begin(const String &hostname);
void begin(const String &hostname, const String & wifiStaModeName);
void setStaCredentials(const char *wifiName, const char *password);
void setApCredentials(const char *password);
@@ -48,4 +48,5 @@ private:
Preferences prefs_;
State state_;
String hostname_;
String wifiStaModeName_;
};