Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
240 lines (204 loc) · 5.41 KB
/
Copy pathnetwork.cpp
File metadata and controls
240 lines (204 loc) · 5.41 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
#include "network.h"
#include "serial.h"
#include "config.h"
#include "pins.h"
#include "sdControl.h"
#include <DNSServer.h>
#include <SPIFFS.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifdef ESP32
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
DNSServer dnsServer;
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";
String IpAddress2String(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
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.print(".");
timeout++;
if(timeout++ > WIFI_CONNECT_TIMEOUT/100) {
Serial.println("");
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());
//String sIp = IpAddress2String(WiFi.localIP());
//config.save_ip(sIp.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(&SPIFFS) != 1) { // Not connected before
// Start the AP
startSoftAP();
return 1;
}
wifiConnected = false;
wifiConnecting = true;
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();
//String sIp = IpAddress2String(WiFi.localIP());
//config.save_ip(sIp.c_str());
SERIAL_ECHOLN("");
SERIAL_ECHO("Connected to "); SERIAL_ECHOLN(config.ssid());
SERIAL_ECHO("IP address: "); SERIAL_ECHOLN(WiFi.localIP());
wifiConnected = true;
wifiConnecting = false;
_stamode = true;
return 3;
}
int Network::status() {
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.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());
config.clear();
}
else
{
Serial.println("WiFiAP Failed");
delay(1000);
Serial.println("restart now...");
ESP.restart();
}
// m_apmode = true;
// WiFi.mode(WIFI_AP_STA);
// WiFi.persistent(false);
// WiFi.softAP(ssid, psk);
// /* Setup the DNS server redirecting all the domains to the AP_local_ip */
// m_dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
// m_dnsServer.start(53, "*", WiFi.softAPIP());
// return WiFi.softAPIP();
}
void Network::getWiFiList(String &list) {
list = _wifiList;
}
void Network::doScan() {
_doScan = true;
}
void Network::scanWiFi() {
Serial.println("Scanning wifi");
Serial.println("--------->");
int n = WiFi.scanNetworks();
Serial.println("scan done");
_wifiList = "[";
if (n != 0) {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
_wifiList += "{\"ssid\":\"";
_wifiList += WiFi.SSID(i);
_wifiList += "\",\"rssi\":\"";
_wifiList += WiFi.RSSI(i);
_wifiList += "\"";
_wifiList += ",\"type\":\"";
_wifiList += (WiFi.encryptionType(i) == WIFI_AUTH_OPEN)? "open":"close";
_wifiList += "\"";
_wifiList += "}";
if(i!=n-1) _wifiList += ",";
}
}
_wifiList += "]";
Serial.println(_wifiList);
}
void Network::loop() {
if(_doConnect) {
connect(_ssid,_psd);
_doConnect = false;
}
if(_doScan) {
scanWiFi();
_doScan = false;
}
}
Network network;