TinyMaterialDesign
Loading...
Searching...
No Matches
WifiEsp32.h
Go to the documentation of this file.
1#pragma once
2/**
3 * @file WifiEsp32.h
4 * @brief The WiFi of the ESP32 for WifiLoginPage. Not part of
5 * TinyMaterialDesign.h: include it in ESP32 sketches.
6 */
7#include <Preferences.h>
8#include <WiFi.h>
9
10#include "TinyMaterialDesign/Wifi/WifiControl.h"
11
12namespace tinymd {
13
14/**
15 * @brief WifiControl for the ESP32: asynchronous scan and connection, the
16 * credentials are stored in the flash (Preferences, namespace "wifi") when a
17 * connection works, and used by begin().
18 *
19 * Esp32Wifi wifi;
20 * void setup() {
21 * wifi.begin(); // connects with the stored credentials
22 * loginPage.setWifi(&wifi);
23 * }
24 */
25class Esp32Wifi : public WifiControl {
26 public:
27 /// Time after which a connection attempt counts as failed.
28 uint32_t connectTimeoutMs = 20000;
29
30 /// Station mode; connects with the stored credentials (if any).
31 void begin() {
32 WiFi.persistent(false); // the credentials are stored here, not by the SDK
33 WiFi.mode(WIFI_STA);
34 Preferences p;
35 p.begin("wifi", true);
36 savedSsid_ = p.getString("ssid", "").c_str();
37 savedPassword_ = p.getString("password", "").c_str();
38 p.end();
39 if (!savedSsid_.empty()) start(savedSsid_.c_str(), savedPassword_.c_str(), false);
40 }
41
42 bool startScan() override {
43 // a connection attempt blocks the scan: stop it
44 if (connecting_ && WiFi.status() != WL_CONNECTED) {
45 WiFi.disconnect();
46 connecting_ = false;
47 }
48 return WiFi.scanNetworks(true /* async */, false /* hidden */) != WIFI_SCAN_FAILED;
49 }
50
51 bool scanDone(std::vector<WifiNetwork>& out) override {
52 const int n = WiFi.scanComplete();
53 if (n == WIFI_SCAN_RUNNING) return false;
54 out.clear();
55 for (int i = 0; i < n; ++i) {
56 WifiNetwork net;
57 net.ssid = WiFi.SSID(i).c_str();
58 net.rssi = WiFi.RSSI(i);
59 net.secure = WiFi.encryptionType(i) != WIFI_AUTH_OPEN;
60 out.push_back(net);
61 }
62 if (n >= 0) WiFi.scanDelete();
63 normalizeNetworks(out);
64 return true;
65 }
66
67 void connect(const char* ssid, const char* password) override { start(ssid, password, true); }
68
69 void disconnect() override {
70 WiFi.disconnect();
71 connecting_ = false;
72 failed_ = false;
73 }
74
75 State state() override {
76 const wl_status_t s = WiFi.status();
77 if (s == WL_CONNECTED) {
78 if (connecting_ && store_) save();
79 connecting_ = false;
80 failed_ = false;
81 return State::kConnected;
82 }
83 if (connecting_) {
84 const bool timeout = millis() - startMs_ > connectTimeoutMs;
85 if (s == WL_CONNECT_FAILED || s == WL_NO_SSID_AVAIL || timeout) {
86 connecting_ = false;
87 failed_ = true;
88 WiFi.disconnect();
89 }
90 }
91 return connecting_ ? State::kConnecting : failed_ ? State::kFailed : State::kDisconnected;
92 }
93
94 std::string ssid() override {
95 if (WiFi.status() == WL_CONNECTED) return WiFi.SSID().c_str();
96 return ssid_;
97 }
98
99 std::string ip() override { return WiFi.status() == WL_CONNECTED ? WiFi.localIP().toString().c_str() : ""; }
100
101 bool hasCredentials() override { return !savedSsid_.empty(); }
102
103 void forget() override {
104 Preferences p;
105 p.begin("wifi", false);
106 p.clear();
107 p.end();
108 savedSsid_.clear();
109 savedPassword_.clear();
110 }
111
112 private:
113 std::string ssid_, password_, savedSsid_, savedPassword_;
114 bool connecting_ = false;
115 bool failed_ = false;
116 bool store_ = false;
117 uint32_t startMs_ = 0;
118
119 void start(const char* ssid, const char* password, bool store) {
120 ssid_ = ssid != nullptr ? ssid : "";
121 password_ = password != nullptr ? password : "";
122 store_ = store;
123 WiFi.disconnect();
124 WiFi.begin(ssid_.c_str(), password_.empty() ? nullptr : password_.c_str());
125 connecting_ = true;
126 failed_ = false;
127 startMs_ = millis();
128 }
129
130 void save() {
131 store_ = false;
132 if (ssid_ == savedSsid_ && password_ == savedPassword_) return;
133 Preferences p;
134 p.begin("wifi", false);
135 p.putString("ssid", ssid_.c_str());
136 p.putString("password", password_.c_str());
137 p.end();
138 savedSsid_ = ssid_;
139 savedPassword_ = password_;
140 }
141};
142
143} // namespace tinymd
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