mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-04 02:48:13 +10:00
feat: refine portal flow and examples
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
WiFiManager portal;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
portal.setConfigPortalTimeout(180);
|
||||
|
||||
portal.autoConnect("WiFiManager Basic", "example-pass");
|
||||
|
||||
Serial.println("Connected. Run your normal application here.");
|
||||
}
|
||||
|
||||
void loop() { portal.process(); }
|
||||
@@ -0,0 +1,12 @@
|
||||
# Basic Portal
|
||||
|
||||
This is the smallest useful WiFiManager application. It first tries the credentials the ESP platform already knows. If it cannot connect, it opens an access point named **WiFiManager Basic** with password **example-pass**.
|
||||
|
||||
1. Build and flash the selected `esp8266` or `esp32` environment.
|
||||
2. Connect a phone or computer to **WiFiManager Basic**.
|
||||
3. Open `http://192.168.4.1/` if your captive-portal helper does not open it automatically.
|
||||
4. Select a network and save it. The board joins that network and the next reboot reconnects without opening the portal.
|
||||
|
||||
The access-point password is only an example. Choose a unique, Wi-Fi-valid password for a real product.
|
||||
|
||||
See the shared [example guide](../README.md) and [getting started](../../docs/GETTING_STARTED.md).
|
||||
@@ -0,0 +1,26 @@
|
||||
[platformio]
|
||||
default_envs = esp8266
|
||||
src_dir = .
|
||||
|
||||
[common]
|
||||
framework = arduino
|
||||
lib_ldf_mode = deep+
|
||||
lib_deps =
|
||||
WiFiManager=symlink://../..
|
||||
|
||||
[env:esp8266]
|
||||
extends = common
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
platform_packages =
|
||||
platformio/framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git#521ae60a89e64bb0d1eb7a0b7addf620ced5cad3
|
||||
|
||||
[env:esp32]
|
||||
extends = common
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.05/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
-std=gnu++14
|
||||
-DSOC_WIFI_SUPPORTED=1
|
||||
-I${platformio.packages_dir}/framework-arduinoespressif32/libraries/Network/src
|
||||
@@ -6,7 +6,7 @@ WiFiManager wifi;
|
||||
namespace {
|
||||
const char kPortalTitle[] PROGMEM = "Set up Temperature Monitor";
|
||||
const char kPortalIdentity[] PROGMEM = "Example Devices";
|
||||
const char kPortalIntro[] PROGMEM = "Connect this device to Wi-Fi.";
|
||||
const char kPortalTagline[] PROGMEM = "Reliable setup for connected devices.";
|
||||
const char kPortalLogoAlt[] PROGMEM = "Example Devices";
|
||||
const char kExampleLogo[] PROGMEM = "<svg viewBox='0 0 64 64' aria-hidden='true'><circle cx='32' cy='32' r='28' fill='#347a45'/></svg>";
|
||||
const char kPage[] PROGMEM = "#f4f7f3";
|
||||
@@ -18,7 +18,7 @@ const char kAccentText[] PROGMEM = "#ffffff";
|
||||
const WiFiManagerPortalConfig kPortalUI = {
|
||||
WiFiManagerPortalText::progmem(kPortalTitle),
|
||||
WiFiManagerPortalText::progmem(kPortalIdentity),
|
||||
WiFiManagerPortalText::progmem(kPortalIntro),
|
||||
WiFiManagerPortalText::progmem(kPortalTagline),
|
||||
WiFiManagerPortalAsset::svgFromProgmem(kExampleLogo),
|
||||
WiFiManagerPortalText::progmem(kPortalLogoAlt),
|
||||
{
|
||||
@@ -42,9 +42,7 @@ void setup() {
|
||||
Serial.println("Portal UI configuration was rejected");
|
||||
}
|
||||
wifi.setConfigPortalTimeout(180);
|
||||
if (!wifi.autoConnect("Temperature Monitor")) {
|
||||
ESP.restart();
|
||||
}
|
||||
wifi.autoConnect("Temperature Monitor");
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
void loop() { wifi.process(); }
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Branded Portal
|
||||
|
||||
This example uses the supported `WiFiManagerPortalConfig` presentation API to give the built-in portal a product name, company identity, tagline, inline SVG mark, and semantic colour tokens.
|
||||
|
||||
Flash the `esp8266` or `esp32` environment, join **Temperature Monitor**, and open `http://192.168.4.1/`. The visual changes come from static firmware data; WiFiManager still owns the portal routes, forms, validation, and captive-network behaviour.
|
||||
|
||||
Use only trusted compiled SVG data. Keep the backing strings static for the lifetime of the firmware, then call `setPortalConfig()` before opening a portal.
|
||||
|
||||
See [Portal UI](../../docs/PORTAL_UI.md) and the shared [example guide](../README.md).
|
||||
@@ -0,0 +1,26 @@
|
||||
[platformio]
|
||||
default_envs = esp8266
|
||||
src_dir = .
|
||||
|
||||
[common]
|
||||
framework = arduino
|
||||
lib_ldf_mode = deep+
|
||||
lib_deps =
|
||||
WiFiManager=symlink://../..
|
||||
|
||||
[env:esp8266]
|
||||
extends = common
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
platform_packages =
|
||||
platformio/framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git#521ae60a89e64bb0d1eb7a0b7addf620ced5cad3
|
||||
|
||||
[env:esp32]
|
||||
extends = common
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.05/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
-std=gnu++14
|
||||
-DSOC_WIFI_SUPPORTED=1
|
||||
-I${platformio.packages_dir}/framework-arduinoespressif32/libraries/Network/src
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
WiFiManager portal;
|
||||
WiFiManagerParameter brokerHost("broker_host", "MQTT broker", "mqtt.local", 40);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
portal.portalAddParameter(&brokerHost);
|
||||
portal.portalAddInfoSection({
|
||||
"device", "Example device",
|
||||
{{"firmware", "Firmware", "1.0.0"}, {"sensor", "Sensor", "Ready"}},
|
||||
});
|
||||
portal.portalAddHomeCard({
|
||||
"hint", "What this example adds", PortalHomeCardKind::Callout,
|
||||
"A normal text setting, a status section, and a home-page callout.", {},
|
||||
});
|
||||
|
||||
portal.setSaveParamsCallback([](WiFiManager::WiFiManagerRequestArgs) {
|
||||
Serial.print("MQTT broker selected: ");
|
||||
Serial.println(brokerHost.getValue());
|
||||
});
|
||||
portal.setConfigPortalTimeout(180);
|
||||
|
||||
portal.autoConnect("WiFiManager Content", "example-pass");
|
||||
}
|
||||
|
||||
void loop() { portal.process(); }
|
||||
@@ -0,0 +1,13 @@
|
||||
# Custom Portal Content
|
||||
|
||||
This example keeps WiFiManager’s portal navigation, validation, and captive behaviour, while adding three application-owned pieces of content:
|
||||
|
||||
- an editable MQTT broker host field;
|
||||
- a compact device-information section;
|
||||
- a callout on the portal overview.
|
||||
|
||||
It opens **WiFiManager Content** with password **example-pass** until it has working station credentials. Save the form, then inspect serial output to see the selected broker value.
|
||||
|
||||
The parameter object is global because WiFiManager reads it for the lifetime of the portal. In a real application, copy the value into that application’s own validated persistent configuration inside the save callback.
|
||||
|
||||
See [Portal UI](../../docs/PORTAL_UI.md) and the shared [example guide](../README.md).
|
||||
@@ -0,0 +1,26 @@
|
||||
[platformio]
|
||||
default_envs = esp8266
|
||||
src_dir = .
|
||||
|
||||
[common]
|
||||
framework = arduino
|
||||
lib_ldf_mode = deep+
|
||||
lib_deps =
|
||||
WiFiManager=symlink://../..
|
||||
|
||||
[env:esp8266]
|
||||
extends = common
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
platform_packages =
|
||||
platformio/framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git#521ae60a89e64bb0d1eb7a0b7addf620ced5cad3
|
||||
|
||||
[env:esp32]
|
||||
extends = common
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.05/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
-std=gnu++14
|
||||
-DSOC_WIFI_SUPPORTED=1
|
||||
-I${platformio.packages_dir}/framework-arduinoespressif32/libraries/Network/src
|
||||
@@ -0,0 +1,22 @@
|
||||
# WiFiManager examples
|
||||
|
||||
Every directory below is a standalone PlatformIO project. Build it from the repository root or the example directory:
|
||||
|
||||
```bash
|
||||
pio run -d examples/BasicPortal -e esp8266
|
||||
pio run -d examples/BasicPortal -e esp8266 -t upload
|
||||
pio device monitor -d examples/BasicPortal -e esp8266
|
||||
```
|
||||
|
||||
Choose `esp32` for an ESP32 development board. The examples use the checked-out WiFiManager source, so they are also useful while developing this fork.
|
||||
|
||||
| Example | Start here when you want to… |
|
||||
| --- | --- |
|
||||
| [Basic Portal](BasicPortal/) | provision one board through the normal saved-network-or-portal flow |
|
||||
| [Custom Portal Content](CustomPortalContent/) | add application settings and useful status cards to the built-in portal |
|
||||
| [Branded Portal](BrandedPortal/) | change the portal’s identity, logo, and semantic visual theme |
|
||||
| [Station Profiles](StationProfiles/) | remember a primary Wi-Fi network and one fallback in application-owned EEPROM storage |
|
||||
|
||||
The portal examples intentionally begin with no station credentials. On first boot, connect to the access point printed on serial and open `http://192.168.4.1/`.
|
||||
|
||||
Back to the [project overview](../README.md).
|
||||
@@ -0,0 +1,9 @@
|
||||
# Station Profiles
|
||||
|
||||
This example gives WiFiManager a small application-owned EEPROM store. The portal accepts a required primary Wi-Fi network and an optional fallback, verifies a submitted network before saving it, then remembers the last successful choice across restarts.
|
||||
|
||||
On a blank board, connect to **WiFiManager Profiles** with password **example-pass** and open `http://192.168.4.1/`. Enter a primary network and, if useful, one fallback. After a successful connection, restart the board to confirm that it tries the saved profiles before reopening the portal.
|
||||
|
||||
`StoredProfiles` is intentionally simple so the ownership boundary is visible. A production application should add its own record versioning and integrity protection around the application’s complete configuration; WiFiManager only owns network-selection policy.
|
||||
|
||||
See [Station profiles](../../docs/STATION_PROFILES.md) and the shared [example guide](../README.md).
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
namespace {
|
||||
constexpr uint32_t kStoreMagic = 0x574D5031; // "WMP1"
|
||||
|
||||
struct StoredProfiles {
|
||||
uint32_t magic;
|
||||
WiFiManagerStationProfiles profiles;
|
||||
};
|
||||
|
||||
class EepromProfileStore final : public WiFiManagerStationProfileStore {
|
||||
public:
|
||||
bool begin() {
|
||||
#if defined(ESP32)
|
||||
return EEPROM.begin(sizeof(StoredProfiles));
|
||||
#else
|
||||
EEPROM.begin(sizeof(StoredProfiles));
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool load(WiFiManagerStationProfiles& profiles) override {
|
||||
StoredProfiles stored{};
|
||||
EEPROM.get(0, stored);
|
||||
if (stored.magic != kStoreMagic) {
|
||||
return false;
|
||||
}
|
||||
profiles = stored.profiles;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool save(const WiFiManagerStationProfiles& profiles) override {
|
||||
EEPROM.put(0, StoredProfiles{kStoreMagic, profiles});
|
||||
return EEPROM.commit();
|
||||
}
|
||||
|
||||
bool clear() override {
|
||||
EEPROM.put(0, StoredProfiles{});
|
||||
return EEPROM.commit();
|
||||
}
|
||||
};
|
||||
|
||||
EepromProfileStore profileStore;
|
||||
WiFiManager portal;
|
||||
} // namespace
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
if (!profileStore.begin()) {
|
||||
Serial.println("Could not initialise EEPROM profile storage");
|
||||
return;
|
||||
}
|
||||
|
||||
portal.setStationProfileStore(&profileStore);
|
||||
portal.setStationRecoveryInterval(30000);
|
||||
portal.startStationConnection("WiFiManager Profiles", "example-pass");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
portal.process();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
[platformio]
|
||||
default_envs = esp8266
|
||||
src_dir = .
|
||||
|
||||
[common]
|
||||
framework = arduino
|
||||
lib_ldf_mode = deep+
|
||||
lib_deps =
|
||||
WiFiManager=symlink://../..
|
||||
|
||||
[env:esp8266]
|
||||
extends = common
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
platform_packages =
|
||||
platformio/framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git#521ae60a89e64bb0d1eb7a0b7addf620ced5cad3
|
||||
|
||||
[env:esp32]
|
||||
extends = common
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.05/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
-std=gnu++14
|
||||
-DSOC_WIFI_SUPPORTED=1
|
||||
-I${platformio.packages_dir}/framework-arduinoespressif32/libraries/Network/src
|
||||
Reference in New Issue
Block a user