-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWiFiManager.cpp
174 lines (132 loc) · 4.11 KB
/
WiFiManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "WiFiManager.h"
#include "userBoardDefines.h"
#ifdef M5STACK
#include <M5StickCPlus.h>
#endif
#ifdef GENERIC_ESP32
#include <Arduino.h>
#endif
#include "WiFi.h"
#include "WiFiType.h"
#include "blockClockTypes.h"
#include "esp_smartconfig.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "powerManager.h"
#include "prefsManager.h"
#include "screen.h"
const int WIFI_CONNECTION_TIMEOUT = 200;
void WiFiManager::initWiFi() {
WiFi.mode(WIFI_AP_STA);
if (dontHaveWiFiDataInPrefs()) {
initWiFiSmartConfig();
delay(3000);
ESP.restart();
}
SsidPasswd ssidPasswd = getPrefsSsidPasswd();
screen->drawStringPush("Connecting to: " + truncateString(ssidPasswd.ssid),
10, 50, 1);
screen->drawStringPush("Please Stand By", 10, 60, 1);
WiFi.mode(WIFI_STA);
WiFi.begin(ssidPasswd.ssid.c_str(), ssidPasswd.passwd.c_str());
int count = 0;
while (waitingWiFiConnection(WiFi.status(), count)) {
count++;
delay(100);
}
screen->clearHalfScreen();
if (connectionFailed(WiFi.status())) {
screen->drawStringPush(
"Failed to connect to: " + truncateString(ssidPasswd.ssid), 10, 60, 1);
screen->drawStringPush("Press main button to wipe WiFi data", 10, 70, 1);
screen->drawStringPush("Or press other button to restart", 10, 80, 1);
while (true) {
#ifdef M5STACK
M5.update();
if (M5.BtnA.wasPressed()) {
screen->drawStringPush("Wiping WiFi data and restarting", 10, 90, 1);
wipeWiFiData();
delay(4000);
ESP.restart();
}
if (M5.BtnB.wasPressed() || M5.Axp.GetBtnPress()) {
ESP.restart();
}
#endif
#ifdef GENERIC_ESP32
if (digitalRead(BUTTON1PIN) == 0) {
screen->drawStringPush("Wiping WiFi data and restarting", 10, 90, 1);
wipeWiFiData();
delay(4000);
ESP.restart();
}
#endif
delay(1000);
}
}
screen->drawStringPush(
"Successfuly connected to: " + truncateString(ssidPasswd.ssid), 10, 60,
1);
setWiFiMaxPowerSave();
}
void WiFiManager::initWiFiSmartConfig() {
WiFi.beginSmartConfig(SC_TYPE_ESPTOUCH);
screen->drawStringPush("Waiting for SmartConfig", 10, 30, 1);
while (!WiFi.smartConfigDone()) {
delay(500);
}
screen->drawStringPush("Smartconfig received", 10, 40, 1);
screen->drawStringPush("Trying to connect", 10, 50, 1);
int count = 0;
while (waitingWiFiConnection(WiFi.status(), count)) {
count++;
delay(100);
}
screen->drawStringPush("Connected to: " + WiFi.SSID(), 10, 60, 1);
screen->drawStringPush("Saving WiFi data", 10, 70, 1);
String ssid = getSsidPasswd("SSID");
String password = getSsidPasswd("PASS");
saveWiFiDataInStorage(ssid, password);
screen->drawStringPush("Restarting", 10, 80, 1);
delay(500);
}
bool WiFiManager::waitingWiFiConnection(wl_status_t status, int count) {
if ((status != WL_CONNECTED && status != WL_CONNECT_FAILED &&
status != WL_NO_SSID_AVAIL) &&
count <= WIFI_CONNECTION_TIMEOUT) {
return true;
}
return false;
}
bool WiFiManager::connectionFailed(wl_status_t status) {
if (status == WL_NO_SSID_AVAIL || status == WL_IDLE_STATUS ||
status == WL_CONNECT_FAILED || status == WL_DISCONNECTED) {
return true;
}
return false;
}
String WiFiManager::getSsidPasswd(String ssidPasswd) {
ssidPasswd.toUpperCase();
wifi_config_t conf;
esp_wifi_get_config(WIFI_IF_STA, &conf);
if (ssidPasswd == "SSID") {
return String(reinterpret_cast<const char*>(conf.sta.ssid));
}
if (ssidPasswd == "PASS") {
return String(reinterpret_cast<const char*>(conf.sta.password));
}
}
bool WiFiManager::isWiFiConnected() { return WiFi.status() == WL_CONNECTED; }
WiFiData WiFiManager::getWiFiData() {
WiFiData wifiData;
wifi_ap_record_t wifiApInformation;
esp_err_t err = esp_wifi_sta_get_ap_info(&wifiApInformation);
Serial.println(esp_err_to_name(err));
if (err != ESP_OK) {
Serial.println(esp_err_to_name(err));
}
wifiData.connected = isWiFiConnected();
wifiData.SignalStrength = wifiApInformation.rssi;
wifiData.SSID = String(reinterpret_cast<char*>(wifiApInformation.ssid));
return wifiData;
}