2020-09-19 17:24:34 +00:00
|
|
|
bool accessPoint = false;
|
|
|
|
bool stationMode = false;
|
|
|
|
|
|
|
|
uint32_t stationModeStart = 0;
|
|
|
|
uint32_t apButtonStart = 0;
|
2020-09-20 09:08:20 +00:00
|
|
|
bool mDNSActive = false;
|
2020-09-19 17:24:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
void startAccessPoint();
|
|
|
|
void startStationMode();
|
2020-09-20 09:08:20 +00:00
|
|
|
void startmDNS();
|
2020-09-19 17:24:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
void initWiFi()
|
|
|
|
{
|
|
|
|
WiFi.disconnect();
|
|
|
|
WiFi.softAPdisconnect();
|
2020-09-20 09:08:20 +00:00
|
|
|
if (mDNSActive)
|
|
|
|
{
|
|
|
|
MDNS.close();
|
|
|
|
mDNSActive = false;
|
|
|
|
}
|
2020-09-19 17:24:34 +00:00
|
|
|
|
|
|
|
accessPoint = connectionSettings->flag(AccessPoint);
|
|
|
|
stationMode = connectionSettings->flag(StationMode) && connectionSettings->ssid() != nullptr;
|
|
|
|
|
|
|
|
WiFi.mode(accessPoint && stationMode ? WIFI_AP_STA :
|
|
|
|
accessPoint ? WIFI_AP :
|
|
|
|
stationMode ? WIFI_STA :
|
|
|
|
WIFI_OFF);
|
|
|
|
|
|
|
|
if (accessPoint)
|
|
|
|
startAccessPoint();
|
|
|
|
|
|
|
|
if (stationMode)
|
|
|
|
startStationMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void updateWiFi()
|
|
|
|
{
|
|
|
|
if (stationModeStart > 0)
|
|
|
|
{
|
|
|
|
bool isConnected = WiFi.status() == WL_CONNECTED;
|
|
|
|
|
|
|
|
if (isConnected)
|
|
|
|
{
|
|
|
|
_d("WiFi :: connected, IP address: ");
|
|
|
|
_dln(WiFi.localIP());
|
|
|
|
|
|
|
|
stationModeStart = 0;
|
2020-09-20 09:08:20 +00:00
|
|
|
startmDNS();
|
2020-09-19 17:24:34 +00:00
|
|
|
}
|
|
|
|
else if (stationMode && accessPoint &&
|
|
|
|
currentTime - stationModeStart >= StationModeTimeout)
|
|
|
|
{
|
|
|
|
_dln("WiFi :: unable to connect, switching off station mode, status:");
|
|
|
|
_dln(WiFi.status());
|
|
|
|
|
|
|
|
#ifdef SerialDebug
|
|
|
|
WiFi.printDiag(Serial);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Connecting to access point is taking too long and is blocking
|
|
|
|
// the access point mode, stop trying
|
|
|
|
stationMode = false;
|
|
|
|
WiFi.disconnect();
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!accessPoint)
|
|
|
|
{
|
|
|
|
if (digitalRead(systemSettings->pinAPButton()) == LOW)
|
|
|
|
{
|
|
|
|
if (apButtonStart == 0)
|
|
|
|
apButtonStart = currentTime;
|
|
|
|
else if (currentTime - apButtonStart >= APButtonHoldTime)
|
|
|
|
{
|
|
|
|
connectionSettings->flag(AccessPoint, true);
|
|
|
|
connectionSettings->write();
|
|
|
|
|
|
|
|
startAccessPoint();
|
|
|
|
apButtonStart = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (apButtonStart > 0)
|
|
|
|
apButtonStart = 0;
|
|
|
|
}
|
2020-09-20 09:08:20 +00:00
|
|
|
|
|
|
|
if (mDNSActive)
|
|
|
|
MDNS.update();
|
2020-09-19 17:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void startAccessPoint()
|
|
|
|
{
|
|
|
|
_dln("WiFi :: starting access point");
|
|
|
|
String ssidString = DefaultAPSSIDPrefix + String(ESP.getChipId(), HEX);
|
|
|
|
if (WiFi.softAP((const char *)ssidString.c_str()))
|
|
|
|
{
|
|
|
|
_d("WiFi :: IP address: ");
|
|
|
|
_dln(WiFi.softAPIP());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_d("WiFi :: failed to start soft access point");
|
2020-09-20 09:08:20 +00:00
|
|
|
|
|
|
|
startmDNS();
|
2020-09-19 17:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void startStationMode()
|
|
|
|
{
|
|
|
|
_d("WiFi :: starting station mode to: ");
|
|
|
|
_dln(connectionSettings->ssid());
|
|
|
|
|
|
|
|
stationModeStart = currentTime;
|
|
|
|
|
|
|
|
if (connectionSettings->hostname() != nullptr)
|
|
|
|
WiFi.hostname(connectionSettings->hostname());
|
|
|
|
|
|
|
|
if (WiFi.begin(connectionSettings->ssid(), connectionSettings->password()))
|
|
|
|
{
|
|
|
|
if (connectionSettings->flag(DHCP))
|
|
|
|
// I've had the same issue as described here with config(0, 0, 0):
|
|
|
|
// https://stackoverflow.com/questions/40069654/how-to-clear-static-ip-configuration-and-start-dhcp
|
|
|
|
wifi_station_dhcpc_start();
|
|
|
|
else
|
|
|
|
WiFi.config(connectionSettings->ip(), connectionSettings->gateway(), connectionSettings->subnetMask());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_d("WiFi :: failed to start station mode");
|
|
|
|
}
|
2020-09-20 09:08:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
void startmDNS()
|
|
|
|
{
|
|
|
|
char* hostname = connectionSettings->hostname();
|
|
|
|
if (hostname == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!MDNS.begin(hostname))
|
|
|
|
_dln("WiFi :: mDNS setup failed");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mDNSActive = true;
|
|
|
|
_d("WiFi :: mDNS responder started for hostname "); _d(hostname); _dln(".local");
|
|
|
|
}
|
|
|
|
}
|