Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
256 lines (219 loc) · 5.27 KB
/
Copy pathnetwork.cpp
File metadata and controls
256 lines (219 loc) · 5.27 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "Arduino.h"
#include "network.h"
#include "serial.h"
#include "config.h"
#include "pins.h"
#include "sdControl.h"
#include <ArduinoOTA.h>
#include "jsonString.h"
#ifdef ESP32
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
IPAddress AP_local_ip(192, 168, 4, 1);
IPAddress AP_gateway(192, 168, 4, 1);
IPAddress AP_subnet(255, 255, 255, 0); // Subnet
const char* AP_SSID = "SD-WIFI-PRO";
int Network::startConnect(String ssid, String psd)
{
if(WiFi.status() == WL_CONNECTED && WiFi.SSID().equals(_ssid.c_str())) {
return 0; // already connected to the AP
}
_ssid = ssid;
_psd = psd;
_doConnect = true;
return 1;
}
int Network::connect(String ssid, String psd)
{
if(_wifiMode == _stamode && WiFi.status() == WL_CONNECTED && WiFi.SSID().equals(ssid.c_str())) {
SERIAL_ECHOLN("Aready connected to the AP");
return 1;
}
// startSoftAP();
wifiConnected = false;
wifiConnecting = true;
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet);
SERIAL_ECHO("Connecting to ");SERIAL_ECHOLN(ssid.c_str());
WiFi.begin(ssid.c_str(), psd.c_str());
// Wait for connection
unsigned int timeout = 0;
while(WiFi.status() != WL_CONNECTED)
{
//blink();
SERIAL_ECHO(".");
timeout++;
if(timeout++ > WIFI_CONNECT_TIMEOUT/100)
{
SERIAL_ECHOLN("");
wifiConnecting = false;
Serial.println("Connect fail, please check your SSID and password");
return 2;
}
else
delay(500);
}
config.save(ssid.c_str(), psd.c_str());
Serial.println("");
Serial.print("Connected to "); Serial.println(config.ssid());
Serial.print("IP address: "); Serial.println(WiFi.localIP());
_stamode = true;
wifiConnected = true;
wifiConnecting = false;
return 3;
}
int Network::start()
{
if(config.load() != 1) { // Not connected before
// Start the AP
startSoftAP();
return 1;
}
wifiConnected = false;
wifiConnecting = true;
WiFi.hostname(AP_SSID);
WiFi.mode(WIFI_STA);
SERIAL_ECHO("Connecting to "); SERIAL_ECHOLN(config.ssid());
WiFi.begin(config.ssid(), config.password());
// Wait for connection
unsigned int timeout = 0;
while(WiFi.status() != WL_CONNECTED)
{
//blink();
SERIAL_ECHO(".");
timeout++;
if(timeout++ > WIFI_CONNECT_TIMEOUT/500)
{
SERIAL_ECHOLN("");
wifiConnecting = false;
SERIAL_ECHOLN("Connect fail, please check your INI file");
config.clear();
startSoftAP();
SERIAL_ECHOLN("Change to AP mode\n. AP SD-WIFI-PRO started");
return 2;
}
else
delay(500);
}
config.save();
SERIAL_ECHOLN("");
SERIAL_ECHO("Connected to "); SERIAL_ECHOLN(config.ssid());
SERIAL_ECHO("IP address: "); SERIAL_ECHOLN(WiFi.localIP());
wifiConnected = true;
wifiConnecting = false;
_stamode = true;
initOTA();
return 3;
}
int Network::status()
{
if(_stamode == false)
return 4;
if(wifiConnected)
return 3; // connected
else {
if(wifiConnecting) return 2; // connecting
else return 1; // fail
}
}
bool Network::isConnected() {
return wifiConnected;
}
bool Network::isConnecting() {
return wifiConnecting;
}
bool Network::isSTAmode() {
return _stamode;
}
void Network::startSoftAP() {
WiFi.hostname(AP_SSID);
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet);
if (WiFi.softAP(AP_SSID))
{
_stamode = false;
Serial.println("SD-WIFI-PRO SoftAP started.");
Serial.print("IP address = ");
Serial.println(WiFi.softAPIP());
Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
// WiFi.beginSmartConfig(); // disables AP mode
// Serial.println("Use EspTouch");
config.clear();
}
else
{
Serial.println("WiFiAP Failed");
delay(1000);
Serial.println("restart now...");
ESP.restart();
}
}
void Network::getWiFiList(String &list) {
list = _wifiList;
_scanComplete = false;
}
void Network::doScan() {
_doScan = true;
}
void Network::scanWiFi() {
Serial.println("Scanning wifi");
Serial.println("--------->");
int n = WiFi.scanNetworks();
Serial.println("scan done");
_wifiList = "{\"type\":\"scan\",\"value\":[";
if (n != 0) {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
if(i) _wifiList += ",";
jsonString js;
js.Var("ssid", WiFi.SSID(i));
js.Var("rssi", WiFi.RSSI(i));
js.Var("type", (WiFi.encryptionType(i) == WIFI_AUTH_OPEN)? "open":"close");
_wifiList += js.Close();
}
}
_wifiList += "]}";
Serial.println(_wifiList);
_scanComplete = true;
}
bool Network::hasScan()
{
return _scanComplete;
}
void Network::initOTA()
{
ArduinoOTA.setHostname(HOSTNAME);
ArduinoOTA.begin();
ArduinoOTA.onStart([]() {
SERIAL_ECHOLN("OTA started");
delay(100);
});
}
void Network::loop()
{
ArduinoOTA.handle();
if(_doConnect) {
connect(_ssid,_psd);
_doConnect = false;
}
if(_doScan) {
scanWiFi();
_doScan = false;
}
if(wifiConnected == false)
{
if( WiFi.smartConfigDone() )
{
Serial.println("EspTouch complete, connected");
config.save( WiFi.SSID().c_str(), WiFi.psk().c_str() );
wifiConnected = true;
wifiConnecting = false;
_stamode = true;
initOTA();
}
}
}
Network network;