Skip to content

Commit

Permalink
set captive
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt committed Oct 24, 2024
1 parent f60a468 commit e5e2c83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AsyncEspFsWebserver
version=1.0.7
version=1.0.8
author=Tolentino Cotesta <[email protected]>
maintainer=Tolentino Cotesta <[email protected]>
sentence=ESPAsyncWebserver with steroids
Expand Down
7 changes: 3 additions & 4 deletions src/AsyncFsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ bool AsyncFsWebServer::startCaptivePortal(const char* ssid, const char* pass, co
else
m_captiveRun = WiFi.softAP(ssid);

#if ESP_FS_WS_SETUP_HTM
if (!m_captiveRun) {
log_error("Captive portal failed to start: WiFi.softAP failed!");
return false;
Expand All @@ -151,7 +150,7 @@ bool AsyncFsWebServer::startCaptivePortal(const char* ssid, const char* pass, co
m_captive = new CaptiveRequestHandler(redirectTargetURL);
addHandler(m_captive).setFilter(ON_AP_FILTER); //only when requested from AP
log_info("Captive portal started. Redirecting all requests to %s", redirectTargetURL);
#endif

return m_captiveRun;
}

Expand Down Expand Up @@ -584,7 +583,7 @@ void AsyncFsWebServer::update_first(AsyncWebServerRequest *request, String file
}
}

IPAddress AsyncFsWebServer::startWiFi(uint32_t timeout, CallbackF fn, bool skipAP ) {
IPAddress AsyncFsWebServer::startWiFi(uint32_t timeout, CallbackF fn, bool skipAP) {
// Check if we need to config wifi connection
IPAddress local_ip, subnet, gateway;

Expand Down Expand Up @@ -674,7 +673,7 @@ IPAddress AsyncFsWebServer::startWiFi(uint32_t timeout, CallbackF fn, bool skipA
}

// No connection, start AP and then captive portal
startCaptivePortal(m_apSSID.c_str(), m_apPsk.c_str(), "/setup");
startCaptivePortal(m_apSSID.c_str(), m_apPsk.c_str(), m_captiveUrl.c_str());
ip = m_captiveIp;
}
return ip;
Expand Down
28 changes: 18 additions & 10 deletions src/AsyncFsWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#define ESP_FS_WS_CONFIG_FILE ESP_FS_WS_CONFIG_FOLDER "/config.json"
#include "setup_htm.h"
#include "SetupConfig.hpp"
#include "CaptivePortal.hpp"
#endif

#define ARDUINOJSON_USE_LONG_LONG 1
Expand All @@ -55,6 +54,7 @@
#else
#define JSON_DOC(x) DynamicJsonDocument doc((size_t)x)
#endif
#include "CaptivePortal.hpp"

#define LIB_URL "https://github.com/cotestatnt/async-esp-fs-webserver/"
#define MIN_F -3.4028235E+38
Expand Down Expand Up @@ -113,6 +113,7 @@ class AsyncFsWebServer : public AsyncWebServer
char* m_pageUser = nullptr;
char* m_pagePswd = nullptr;
String m_host = "esphost";
String m_captiveUrl = "/setup";

uint16_t m_port;
uint32_t m_timeout = 10000;
Expand Down Expand Up @@ -192,11 +193,18 @@ class AsyncFsWebServer : public AsyncWebServer
/*
Start WiFi connection, if fails to in AP mode (backward compatibility)
*/
IPAddress startWiFi(uint32_t timeout, const char *apSSID, const char *apPsw, CallbackF fn=nullptr) {
inline IPAddress startWiFi(uint32_t timeout, const char *apSSID, const char *apPsw, CallbackF fn=nullptr) {
setAP(apSSID, apPsw, m_captiveIp);
return startWiFi(timeout, fn);
}

/*
* Set captive portal endpoint
*/
inline void setCaptiveUrl(const String& url) {
m_captiveUrl = url;
}

/*
* Redirect to captive portal if we got a request for another domain.
*/
Expand All @@ -217,7 +225,7 @@ class AsyncFsWebServer : public AsyncWebServer
/*
* Need to be run in loop to handle DNS requests
*/
void updateDNS() {
inline void updateDNS() {
m_dnsServer->processNextRequest();
}

Expand All @@ -226,21 +234,21 @@ class AsyncFsWebServer : public AsyncWebServer
* This it is necessary due to the different implementation of
* libraries for the filesystem (LittleFS, FFat, SPIFFS etc etc)
*/
void setFsInfoCallback(FsInfoCallbackF fsCallback) {
inline void setFsInfoCallback(FsInfoCallbackF fsCallback) {
getFsInfo = fsCallback;
}

/*
* Set current firmware version (shown in /setup webpage)
*/
void setFirmwareVersion(char* version) {
inline void setFirmwareVersion(char* version) {
strlcpy(m_version, version, sizeof(m_version));
}

/*
* Set hostmane
*/
void setHostname(const char * host) {
inline void setHostname(const char * host) {
m_host = host;
}

Expand All @@ -252,14 +260,14 @@ class AsyncFsWebServer : public AsyncWebServer
/*
* Get status of captive portal
*/
bool getCaptivePortal() {
inline bool getCaptivePortal() {
return m_captiveRun;
}

/*
* Set Access Point SSID and password
*/
void setAP(const char *ssid, const char *psk, IPAddress ip = IPAddress(192,168,4,1)) {
inline void setAP(const char *ssid, const char *psk, IPAddress ip = IPAddress(192,168,4,1)) {
m_apSSID = ssid;
m_apPsk = psk;
m_captiveIp = ip;
Expand All @@ -272,15 +280,15 @@ class AsyncFsWebServer : public AsyncWebServer
/*
* Get reference to current config.json file
*/
File getConfigFile(const char* mode) {
inline File getConfigFile(const char* mode) {
File file = m_filesystem->open(ESP_FS_WS_CONFIG_FILE, mode);
return file;
}

/*
* Get complete path of config.json file
*/
const char* getConfiFileName() {
inline const char* getConfiFileName() {
return ESP_FS_WS_CONFIG_FILE;
}

Expand Down

0 comments on commit e5e2c83

Please sign in to comment.