introduce captive portal, tidy up ap management code
This commit is contained in:
parent
beb3ff9a62
commit
82adad4746
@ -8,20 +8,53 @@ APSettingsService::~APSettingsService() {}
|
|||||||
void APSettingsService::loop() {
|
void APSettingsService::loop() {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
if (_manageAtMillis <= now){
|
if (_manageAtMillis <= now){
|
||||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
manageAP();
|
||||||
if (_provisionMode == AP_MODE_ALWAYS || (_provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED)) {
|
|
||||||
if (currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA){
|
|
||||||
Serial.println("Starting software access point");
|
|
||||||
WiFi.softAP(_ssid.c_str(), _password.c_str());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA){
|
|
||||||
Serial.println("Stopping software access point");
|
|
||||||
WiFi.softAPdisconnect(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_manageAtMillis = now + MANAGE_NETWORK_DELAY;
|
_manageAtMillis = now + MANAGE_NETWORK_DELAY;
|
||||||
}
|
}
|
||||||
|
handleDNS();
|
||||||
|
}
|
||||||
|
|
||||||
|
void APSettingsService::manageAP() {
|
||||||
|
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||||
|
if (_provisionMode == AP_MODE_ALWAYS || (_provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED)) {
|
||||||
|
if (currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA) {
|
||||||
|
startAP();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA) {
|
||||||
|
stopAP();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void APSettingsService::startAP() {
|
||||||
|
Serial.println("Starting software access point");
|
||||||
|
WiFi.softAP(_ssid.c_str(), _password.c_str());
|
||||||
|
|
||||||
|
if (!_dnsServer) {
|
||||||
|
IPAddress apIp = WiFi.softAPIP();
|
||||||
|
Serial.print("Starting captive portal on ");
|
||||||
|
Serial.println(apIp);
|
||||||
|
_dnsServer = new DNSServer;
|
||||||
|
_dnsServer->start(DNS_PORT, "*", apIp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void APSettingsService::stopAP() {
|
||||||
|
if (_dnsServer) {
|
||||||
|
Serial.println("Stopping captive portal");
|
||||||
|
_dnsServer->stop();
|
||||||
|
delete _dnsServer;
|
||||||
|
_dnsServer = NULL;
|
||||||
|
}
|
||||||
|
Serial.println("Stopping software access point");
|
||||||
|
WiFi.softAPdisconnect(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void APSettingsService::handleDNS() {
|
||||||
|
if (_dnsServer) {
|
||||||
|
_dnsServer->processNextRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::readFromJsonObject(JsonObject& root) {
|
void APSettingsService::readFromJsonObject(JsonObject& root) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef APSettingsConfig_h
|
#ifndef APSettingsConfig_h
|
||||||
#define APSettingsConfig_h
|
#define APSettingsConfig_h
|
||||||
|
|
||||||
|
#include <DNSServer.h>
|
||||||
#include <IPAddress.h>
|
#include <IPAddress.h>
|
||||||
#include <SettingsService.h>
|
#include <SettingsService.h>
|
||||||
|
|
||||||
@ -10,6 +11,8 @@
|
|||||||
#define AP_MODE_DISCONNECTED 1
|
#define AP_MODE_DISCONNECTED 1
|
||||||
#define AP_MODE_NEVER 2
|
#define AP_MODE_NEVER 2
|
||||||
|
|
||||||
|
#define DNS_PORT 53
|
||||||
|
|
||||||
#define AP_DEFAULT_SSID "ssid"
|
#define AP_DEFAULT_SSID "ssid"
|
||||||
#define AP_DEFAULT_PASSWORD "password"
|
#define AP_DEFAULT_PASSWORD "password"
|
||||||
|
|
||||||
@ -33,11 +36,22 @@ class APSettingsService : public SettingsService {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
// access point settings
|
||||||
int _provisionMode;
|
int _provisionMode;
|
||||||
String _ssid;
|
String _ssid;
|
||||||
String _password;
|
String _password;
|
||||||
|
|
||||||
|
// for the mangement delay loop
|
||||||
unsigned long _manageAtMillis;
|
unsigned long _manageAtMillis;
|
||||||
|
|
||||||
|
// for the captive portal
|
||||||
|
DNSServer *_dnsServer;
|
||||||
|
|
||||||
|
void manageAP();
|
||||||
|
void startAP();
|
||||||
|
void stopAP();
|
||||||
|
void handleDNS();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // end APSettingsConfig_h
|
#endif // end APSettingsConfig_h
|
||||||
|
Loading…
Reference in New Issue
Block a user