-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathespManager.cpp
More file actions
122 lines (100 loc) · 1.96 KB
/
espManager.cpp
File metadata and controls
122 lines (100 loc) · 1.96 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
#include <espManager.h>
#ifdef ARDUINO_ESP8266_ESP01
#define LED 1
#elif ARDUINO_ESP8266_NODEMCU
#define LED D4
#endif
WiFiUDP udp;
void espManager::begin()
{
pinMode(LED, OUTPUT);
name = ""; pass = ""; req = "";
WiFi.mode(WIFI_AP_STA);
WiFi.disconnect();
WiFi.softAP("WiFi_Manager", "");
udp.begin(555);
while (WiFi.status() != WL_CONNECTED)
{
if (millis() - lm > 500)
{
GPO ^= 1 << LED;
lm = millis();
}
if (udp.parsePacket() > 0)
{
req = "";
while (udp.available() > 0)
{
char z = udp.read();
req += z;
}
if (req.equals("SCAN") == true)
{
String a = "";
byte x = WiFi.scanNetworks();
a += "@"; a += x; a += ";";
for (byte i = 0; i < x; i++)
{
a += "#";
a += WiFi.SSID(i);
a += ";";
}
udp.beginPacket(udp.remoteIP(), 1200);
udp.print(a);
udp.endPacket();
}
else if (req.equals("CONNECT") == true)
{
int x = 0;
if (pass.length() > 5 && name.length() > 5)
{
WiFi.begin(name.c_str(), pass.c_str());
}
else if (pass.length() < 6 && name.length() > 5)
{
WiFi.begin(name.c_str(), NULL);
}
else if (name.length() < 6 && pass.length() > 5)
{
WiFi.begin(NULL, pass.c_str());
}
while (WiFi.status() != WL_CONNECTED)
{
x++;
GPO ^= 1 << LED;
delay(100);
if (x > 49)
{
break;
}
}
}
else if (req.indexOf("NAME=") > -1)
{
if (req.length() > 5)
{
name = req.substring(5);
}
else
{
name = "";
}
}
else if (req.indexOf("PASS=") > -1)
{
pass = req.substring(5);
}
else
{
udp.beginPacket(udp.remoteIP(), 1200);
udp.print("??" + req);
udp.endPacket();
}
}
yield();
}
udp.stop();
WiFi.softAPdisconnect();
WiFi.mode(WIFI_STA);
digitalWrite(LED, 0);
}