3
4
5
6
7#include <Preferences.h>
10#include "TinyMaterialDesign/Wifi/WifiControl.h"
15
16
17
18
19
20
21
22
23
24
32 WiFi.persistent(
false);
35 p.begin(
"wifi",
true);
36 savedSsid_ = p.getString(
"ssid",
"").c_str();
37 savedPassword_ = p.getString(
"password",
"").c_str();
39 if (!savedSsid_.empty()) start(savedSsid_.c_str(), savedPassword_.c_str(),
false);
44 if (connecting_ && WiFi.status() != WL_CONNECTED) {
48 return WiFi.scanNetworks(
true ,
false ) != WIFI_SCAN_FAILED;
51 bool scanDone(std::vector<WifiNetwork>& out)
override {
52 const int n = WiFi.scanComplete();
53 if (n == WIFI_SCAN_RUNNING)
return false;
55 for (
int i = 0; i < n; ++i) {
57 net.ssid = WiFi.SSID(i).c_str();
58 net.rssi = WiFi.RSSI(i);
59 net.secure = WiFi.encryptionType(i) != WIFI_AUTH_OPEN;
62 if (n >= 0) WiFi.scanDelete();
63 normalizeNetworks(out);
67 void connect(
const char* ssid,
const char* password)
override { start(ssid, password,
true); }
76 const wl_status_t s = WiFi.status();
77 if (s == WL_CONNECTED) {
78 if (connecting_ && store_) save();
85 if (s == WL_CONNECT_FAILED || s == WL_NO_SSID_AVAIL || timeout) {
94 std::string
ssid()
override {
95 if (WiFi.status() == WL_CONNECTED)
return WiFi.SSID().c_str();
99 std::string
ip()
override {
return WiFi.status() == WL_CONNECTED ? WiFi.localIP().toString().c_str() :
""; }
105 p.begin(
"wifi",
false);
109 savedPassword_.clear();
113 std::string ssid_, password_, savedSsid_, savedPassword_;
114 bool connecting_ =
false;
115 bool failed_ =
false;
117 uint32_t startMs_ = 0;
119 void start(
const char* ssid,
const char* password,
bool store) {
120 ssid_ = ssid !=
nullptr ? ssid :
"";
121 password_ = password !=
nullptr ? password :
"";
124 WiFi.begin(ssid_.c_str(), password_.empty() ?
nullptr : password_.c_str());
132 if (ssid_ == savedSsid_ && password_ == savedPassword_)
return;
134 p.begin(
"wifi",
false);
135 p.putString(
"ssid", ssid_.c_str());
136 p.putString(
"password", password_.c_str());
139 savedPassword_ = password_;
WifiControl for the ESP32: asynchronous scan and connection, the credentials are stored in the flash ...
Definition: WifiEsp32.h:25
void disconnect() override
Definition: WifiEsp32.h:69
std::string ssid() override
The network that is connected or that is being connected to.
Definition: WifiEsp32.h:94
State state() override
Definition: WifiEsp32.h:75
uint32_t connectTimeoutMs
Time after which a connection attempt counts as failed.
Definition: WifiEsp32.h:28
std::string ip() override
The IP address while connected.
Definition: WifiEsp32.h:99
void connect(const char *ssid, const char *password) override
Connects in the background; the credentials are stored when it works.
Definition: WifiEsp32.h:67
bool startScan() override
Starts a scan in the background.
Definition: WifiEsp32.h:42
bool hasCredentials() override
Credentials are stored (they are used at the start).
Definition: WifiEsp32.h:101
void begin()
Station mode; connects with the stored credentials (if any).
Definition: WifiEsp32.h:31
bool scanDone(std::vector< WifiNetwork > &out) override
Definition: WifiEsp32.h:51
void forget() override
Removes the stored credentials.
Definition: WifiEsp32.h:103
The WiFi of a device as WifiLoginPage uses it: scan, connect and the stored credentials....
Definition: WifiControl.h:50
State
Definition: WifiControl.h:52
Definition: AnchorLayout.h:4
A WiFi network found by a scan.
Definition: WifiControl.h:11