-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266InternalManager.ino
132 lines (122 loc) · 3.49 KB
/
ESP8266InternalManager.ino
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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <ArduinoJson.h>
String ssid = "";
String password = "";
ESP8266WebServer server ( 80 );
byte finderPacketBuffer[128];
WiFiUDP finderUdp;
int connectCount = 0;
bool needConenct = false;
unsigned long apShutDownTime = 6000000L;
unsigned long lastConnectTime = 0;
unsigned long retryConnectTime = 120000;
void readTimerProfile();
void updateNTP();
void readSSIDJson();
void setup() {
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
Serial.begin ( 115200 );
Serial.println("");
SPIFFS.begin();
// 设置内网
IPAddress softLocal(192,168,128,1);
IPAddress softGateway(192,168,128,1);
IPAddress softSubnet(255,255,255,0);
WiFi.softAPConfig(softLocal, softGateway, softSubnet);
String apName = ("ESP8266_"+(String)ESP.getChipId());
const char *softAPName = apName.c_str();
WiFi.softAP(softAPName, "adminadmin");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.print("softAPName: ");
Serial.println(apName);
// 从spifs读取wifi设置并连接wifi
Serial.println("");
Serial.println("=====Read SSID Info=====");
readSSIDJson();
Serial.println("=====Read SSID Info=====");
Serial.println("begin wifi");
Serial.print("ssid:");
Serial.println(ssid.c_str());
Serial.print("passwd:");
Serial.println(password.c_str());
if(ssid != "") {
WiFi.begin ( ssid.c_str(), password.c_str() );
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 1000 );
Serial.print ( "." );
//showWifiStatue(WiFi.status());
//Serial.println(WiFi.status());
if(connectCount > 30) {
Serial.println( "Connect fail!" );
break;
}
connectCount += 1;
}
}
lastConnectTime = millis();
if(WiFi.status() == WL_CONNECTED) {
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
connectCount = 0;
}
finderUdp.begin(8266);
server.on ("/", handleEvent);
server.on("/io", HTTP_GET, handlePin);
server.on("/wifi_access", HTTP_POST, handleWifiAccess);
server.onNotFound ( handleNotFound );
server.begin();
Serial.println("Start Success.");
digitalWrite(2, HIGH);
}
void loop() {
// 更新内网查询udp
handleUdpFinder();
// wifi重连
if(WiFi.status() != WL_CONNECTED || needConenct == true) {
if(millis() - lastConnectTime > retryConnectTime) {
Serial.println( "Try reCconnect" );
connectCount = 0;
Serial.print("ssid:");
Serial.println(ssid.c_str());
Serial.print("passwd:");
Serial.println(password.c_str());
WiFi.begin ( ssid.c_str(), password.c_str() );
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
connectCount += 1;
if(connectCount > 30) {
Serial.println( F("Connect fail! Wait next time") );
break;
}
Serial.print ( "." );
}
if(WiFi.status() == WL_CONNECTED) {
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
}
lastConnectTime = millis();
needConenct = false;
}
}
if(apShutDownTime != 0) {
if(millis() > apShutDownTime) {
WiFi.softAPdisconnect();
apShutDownTime = 0;
Serial.println(F("AP Shut Down"));
}
}
// 路由更新
server.handleClient();
}