-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.cpp
More file actions
222 lines (180 loc) · 5.79 KB
/
Copy pathNetwork.cpp
File metadata and controls
222 lines (180 loc) · 5.79 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <Arduino.h>
#include "WiFi.h"
#include "Display.h"
#include "UI.h"
#include "Debug.h"
#include "secrets.h"
using namespace Display;
namespace Network
{
static int s_reconnectAttempts = 0;
enum class WifiConnectionStatus {
Connecting,
Failure,
Connected
};
/// @brief Render and update the display with current WiFi connection status.
/// @param state WiFi status
/// @param err Error code if failed to connect
static void uiRenderWiFiConnect(WifiConnectionStatus state, wl_status_t err = (wl_status_t)0) {
tftClearCanvas();
UI::Widgets::uiRenderLabelCentered(gfx_right, "WIFI CONNECT", -60, TFT_WHITE);
switch (state) {
case WifiConnectionStatus::Connecting:
{
UI::Widgets::uiRenderLabelCentered(gfx_right, "CONNECTING TO", 0, TFT_WHITE);
UI::Widgets::uiRenderLabelCentered(gfx_right, secrets::wifi_ssid, 30, TFT_LIGHTGREY);
break;
}
break;
case WifiConnectionStatus::Connected:
{
auto ip_str = WiFi.localIP().toString();
UI::Widgets::uiRenderLabelCentered(gfx_right, "CONNECTED", 0, TFT_WHITE);
UI::Widgets::uiRenderLabelCentered(gfx_right, ip_str.c_str(), 30, TFT_LIGHTGREY);
break;
}
case WifiConnectionStatus::Failure:
{
UI::Widgets::uiRenderLabelCentered(gfx_right, "ERROR", 0, TFT_WHITE);
const char* err_msg = nullptr;
switch (err) {
case WL_NO_SSID_AVAIL:
// Bad SSID or AP not present
err_msg = "No SSID";
break;
case WL_CONNECT_FAILED:
err_msg = "Connect Failed";
break;
case WL_IDLE_STATUS:
err_msg = "Station Idle";
break;
case WL_CONNECTION_LOST:
err_msg = "Connection Lost";
break;
}
if (err_msg != nullptr) {
UI::Widgets::uiRenderLabelCentered(gfx_right, err_msg, 30, TFT_LIGHTGREY);
}
break;
}
}
tftUpdateDisplay();
}
#include "esp_wifi.h"
static void startWiFi()
{
// // Erase prior configuration
WiFi.disconnect(true, true);
// WiFi.mode(WIFI_OFF);
// // Store config in RAM instead of committing to flash
// esp_wifi_set_storage(WIFI_STORAGE_RAM);
// // Workaround to disable persistent config
//wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
// cfg.nvs_enable = 0;
//WiFi.persistent(true);
WiFi.persistent(false); // Store config in RAM
WiFi.setHostname(secrets::device_name);
WiFi.setAutoReconnect(true);
// Enable WiFi Station Mode
if (!WiFi.mode(WIFI_STA)) {
Debug.println("ERROR: Could not enable WiFi STA");
}
// Disable power saving to keep connection stable
WiFi.setSleep(WIFI_PS_NONE);
//WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
auto status = WiFi.begin(secrets::wifi_ssid, secrets::wifi_pw);
if (status == WL_CONNECT_FAILED) {
Debug.println("ERROR: Could not start WiFi");
//WiFi.reconnect();
}
}
void initWiFi()
{
wl_status_t result;
//WiFi.persistent(false);
#if CONFIG_WAIT_FOR_WIFI
while (WiFi.status() != WL_CONNECTED) {
uiRenderWiFiConnect(WifiConnectionStatus::Connecting);
Debug.println();
startWiFi();
Debug.printf("Connecting to SSID: %s\n", secrets::wifi_ssid);
result = (wl_status_t)WiFi.waitForConnectResult();
if (result != WL_CONNECTED) {
uiRenderWiFiConnect(WifiConnectionStatus::Failure, result);
Debug.println("WiFi Diagnostics:");
WiFi.printDiag(Serial);
Debug.println();
delay(1000);
}
}
uiRenderWiFiConnect(WifiConnectionStatus::Connected);
Debug.print("IP address: ");
Debug.println(WiFi.localIP());
delay(1500);
#else
Debug.println("Starting WiFi...");
startWiFi();
//Debug.printf("Connecting to SSID: %s\n", secrets::wifi_ssid);
//WiFi.waitForConnectResult();
#endif
}
static const char* wifi_status_str[] = {
"Idle",
"No SSID",
"Scan Completed",
"Connected",
"Connect Failed",
"Connection Lost",
"Disconnected"
};
void handle() {
static unsigned long t_last = 0;
static wl_status_t last_status = WL_DISCONNECTED;
const unsigned long reconnect_interval_ms = 20000;
auto status = WiFi.status();
if (last_status != status) {
last_status = status;
Debug.write("WiFi: ");
Debug.println(wifi_status_str[(int)status]);
}
#if true
if (!(WiFi.getMode() & WIFI_MODE_STA) ||
(status == WL_NO_SSID_AVAIL) ||
(status == WL_CONNECT_FAILED) ||
(status == WL_CONNECTION_LOST) ||
(status == WL_DISCONNECTED) ||
(status == WL_IDLE_STATUS)) // Connecting
{
// Throttle reconnection attempts
if ((millis() - t_last) > reconnect_interval_ms) {
t_last = millis();
if (status != WL_DISCONNECTED) {
Debug.println("WiFi connection lost, reconnecting...");
}
WiFi.reconnect();
s_reconnectAttempts++;
if (s_reconnectAttempts > 6) {
// After 1 minute, fall back to AP mode
WiFi.disconnect();
WiFi.softAP("LUPA", "espresso");
}
}
}
else
{
s_reconnectAttempts = 0;
}
#endif
}
bool isConnecting() {
if (WiFi.getMode() & WIFI_MODE_STA) {
auto wifiStatus = WiFi.status();
//if (wifiStatus == WL_IDLE_STATUS || wifiStatus >= WL_DISCONNECTED) {
if (wifiStatus == WL_IDLE_STATUS) {
return true;
}
}
return false;
}
}