forked from bilibilifmk/wifi_link_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_link_tool.h
332 lines (280 loc) · 7.82 KB
/
wifi_link_tool.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
wifi link tool 配网库
by:发明控
版本v1.0.2
测试环境 sdk版本:2.5.0 arduino版本1.8.8
项目地址:https://github.com/bilibilifmk/wifi_link_tool 项目成员:发明控 狗腿
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <FS.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
const char *AP_name = "wifi_link_tool"; //修改后即不支持微信配网
/////////////////////////////////////////////////////不建议修改部分//////////////////////////////////////////////////////////////////
String Hostname = "ESP8266";
String url = Hostname + ".com";
int Signal_filtering = -200;
const byte DNS_PORT = 53;
String WiFi_State;
#define WiFi_State_Addr 0
bool wxscan=true;
int rstb=0;
int stateled=2;
IPAddress apIP(6, 6, 6, 6);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
String responseHTML =" <html><head><title>正在跳转</title></head><body><script language='javascript'>document.location = '/'</script></body></html>";
//心跳服务
void pant(){
MDNS.update();
dnsServer.processNextRequest();
webServer.handleClient();
}
void torest()
{
WiFi.disconnect(true);
delay(100);
EEPROM.write(WiFi_State_Addr, 0);
EEPROM.commit();
delay(300);
Serial.print("重置成功!正在重启。");
ESP.restart();
}
int link()
{
int a=0;
if(WiFi_State=="1")
{
a=1;
}
return a;
}
void wwwroot()
{
if (WiFi_State == "1")
{
File file = SPIFFS.open("/index.html", "r");
webServer.streamFile(file, "text/html");
file.close();
}
else if (WiFi_State == "0")
{
File file = SPIFFS.open("/config.html", "r");
webServer.streamFile(file, "text/html");
file.close();
}
}
void wifiConfig()
{
if (webServer.hasArg("ssid") && webServer.hasArg("password") && WiFi_State == "0")
{
int ssid_len = webServer.arg("ssid").length();
int password_len = webServer.arg("password").length();
if ((ssid_len > 0) && (ssid_len < 33) && (password_len > 7) && (password_len < 65))
{
String ssid_str = webServer.arg("ssid");
String password_str = webServer.arg("password");
const char *ssid = ssid_str.c_str();
const char *password = password_str.c_str();
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
WiFi.begin(ssid, password);
Serial.print("Connenting");
unsigned long millis_time = millis();
while ((WiFi.status() != WL_CONNECTED) && (millis() - millis_time < 8000))
{
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED)
{
digitalWrite(stateled, HIGH);
Serial.println("");
Serial.println("Connected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("http://");
Serial.println(Hostname);
webServer.send(200, "text/plain", "1");
delay(300);
WiFi_State = "1";
EEPROM.write(WiFi_State_Addr, 1);
EEPROM.commit();
delay(50);
WiFi.softAPdisconnect();
}
else
{
Serial.println("Connenting failed!");
webServer.send(200, "text/plain", "0");
}
}
else
{
Serial.println("Password format error");
webServer.send(200, "text/plain", "Password format error");
}
}
else
{
Serial.println("Request parameter error");
webServer.send(200, "text/plain", "Request parameter error");
}
}
String wifi_type(int typecode)
{
if (typecode == ENC_TYPE_NONE) return "Open";
if (typecode == ENC_TYPE_WEP) return "WEP ";
if (typecode == ENC_TYPE_TKIP) return "WPA ";
if (typecode == ENC_TYPE_CCMP) return "WPA2";
if (typecode == ENC_TYPE_AUTO) return "WPA*";
}
void wifiScan()
{
String req_json = "";
Serial.println("Scan WiFi");
int n = WiFi.scanNetworks();
if (n > 0)
{
int m = 0;
req_json = "{\"req\":[";
for (int i = 0; i < n; i++)
{
if ((int)WiFi.RSSI(i) >= Signal_filtering)
// if (1)
{
m++;
String a="{\"ssid\":\"" + (String)WiFi.SSID(i) + "\"," + "\"encryptionType\":\"" + wifi_type(WiFi.encryptionType(i)) + "\"," + "\"rssi\":" + (int)WiFi.RSSI(i) + "},";
if(a.length()>15)
req_json += a;
}
}
req_json.remove(req_json.length() - 1);
req_json += "]}";
webServer.send(200, "text/json;charset=UTF-8", req_json);
Serial.print("Found ");
Serial.print(m);
Serial.print(" WiFi! >");
Serial.print(Signal_filtering);
Serial.println("dB");
}
}
void opera() {
if(webServer.arg("opera") == "sb"){
webServer.send(200, "text/plain", Hostname);
}
}
void blink() {
Serial.println("Keep 3 seconds to reset!");
bool res_state = true;
unsigned int res_time = millis();
while (millis() - res_time < 3000)
{
if (digitalRead(rstb) != LOW)
{
res_state = false;
Serial.println("Cancel reset!");
break;
}
}
if (res_state == true)
{
WiFi.disconnect(true);
delay(100);
EEPROM.write(WiFi_State_Addr, 0);
EEPROM.commit();
delay(300);
Serial.println("Press the 'rst' button!");
while (1) {
digitalWrite(stateled, LOW);
delay(250);
digitalWrite(stateled, HIGH);
delay(250);
}
Serial.println("重置!");
ESP.restart();
}
}
//加载部分
void load(){
attachInterrupt(digitalPinToInterrupt(rstb), blink, FALLING);
Serial.println("");
EEPROM.begin(4096);
SPIFFS.begin();
WiFi.hostname(Hostname);
pinMode(stateled, OUTPUT);
WiFi_State = EEPROM.read(WiFi_State_Addr);
delay(300);
if (WiFi_State == "1")
{
Serial.println("找到配置!");
Serial.print("链接网络");
unsigned millis_time = millis();
while ((WiFi.status() != WL_CONNECTED) && (millis() - millis_time < 5000))
{
delay(250);
Serial.print(".");
}
if (wxscan){
if (MDNS.begin(Hostname)) { Serial.println("mDNS以启动"); }
MDNS.addService("http", "tcp", 80);
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("IP 地址: ");
Serial.println(WiFi.localIP());
Serial.print("http://");
Serial.println(Hostname);
digitalWrite(stateled, HIGH);
}
else
{
Serial.println("链接失败!");
Serial.println("请尝试重置系统!");
digitalWrite(stateled, LOW);
}
}
else if (WiFi_State == "0")
{
digitalWrite(stateled, LOW);
WiFi.disconnect(true);
Serial.println("");
Serial.print("启动WiFi配置 \n建立AP 名称 --> ");
Serial.println(AP_name);
Serial.print("http://");
Serial.println(url);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(AP_name);
}
else
{
Serial.println("初次启动");
EEPROM.write(WiFi_State_Addr, 0);
EEPROM.commit();
delay(300);
Serial.println("请重置系统!");
while (1) {
digitalWrite(stateled, LOW);
delay(250);
digitalWrite(stateled, HIGH);
delay(250);
}
}
Serial.println("启动http服务");
webServer.on("/", wwwroot);
webServer.on("/wificonfig", wifiConfig);
webServer.on("/wifiscan", wifiScan);
webServer.on("/opera", opera);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
dnsServer.start(DNS_PORT, "*", apIP);
webServer.onNotFound([]() {
webServer.send(200, "text/html", responseHTML);
});
webServer.begin();
}