-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.h
155 lines (141 loc) · 3.94 KB
/
wifi.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
// ******************************
// by Lemmy
// ******************************
#define WIFI_SERIAL Serial1
#define WRESET 14
#define IPDEBUG 0
String SSID = "SSID";
String PASS = "PASS";
bool ret;
bool wifi_connected = false;
// **************************************************
void wifi_buffclr(void) {
while ( WIFI_SERIAL.available() > 0 ) {
char c = WIFI_SERIAL.read();
}
}
// **************************************************
boolean already_connected() {
WIFI_SERIAL.println("AT+CWJAP_CUR?");
delay(1);
if (WIFI_SERIAL.find(SSID)) ret = true;
else ret = false;
wifi_buffclr();
return ret;
}
// **************************************************
boolean wifi_softreset() {
WIFI_SERIAL.println("AT+RST");
delay(1);
if (WIFI_SERIAL.find("ready")) ret = true;
else ret = false;
wifi_buffclr();
return ret;
}
// **************************************************
void wifi_hardreset() {
digitalWrite(WRESET, LOW);
delay(1);
digitalWrite(WRESET, HIGH);
}
// **************************************************
boolean wifi_multicon() {
WIFI_SERIAL.println("AT+CIPMUX=1");
delay(1);
if (WIFI_SERIAL.find("OK")) ret = true;
else ret = false;
wifi_buffclr();
return ret;
}
// **************************************************
boolean wifi_startserv80() {
WIFI_SERIAL.println("AT+CIPSERVER=1,80");
delay(1);
if (WIFI_SERIAL.find("OK")) ret = true;
else ret = false;
wifi_buffclr();
return ret;
}
// **************************************************
boolean wifi_cwmode3() {
WIFI_SERIAL.println("AT+CWMODE_CUR?");
if (WIFI_SERIAL.find("+CWMODE_CUR:3")) ret = true;
else
{
WIFI_SERIAL.println("AT+CWMODE_CUR=3");
delay(1);
if (WIFI_SERIAL.find("OK")) ret = true;
else ret = false;
}
wifi_buffclr();
return ret;
}
// **************************************************
boolean wifi_connect() {
String cmd = "AT+CWJAP_CUR=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
WIFI_SERIAL.println(cmd);
delay(5000);
if (WIFI_SERIAL.find("WIFI CONNECTED") and WIFI_SERIAL.find("WIFI GOT IP") and WIFI_SERIAL.find("OK")) ret = true;
else {
ret = false;
delay(10000);
}
wifi_buffclr();
return ret;
}
// **************************************************
String wifi_getlink() {
if (IPDEBUG == 1 ) DEBUG_SERIAL.println("wifi_getlink");
WIFI_SERIAL.println("AT+CIFSR");
delay(1);
if (WIFI_SERIAL.find("APIP,\"")) {
String ip = WIFI_SERIAL.readString();
String apip = ip.substring(0, ip.indexOf("\""));
String staip = ip.substring(ip.indexOf("STAIP,\"") + 7);
staip = staip.substring(0, staip.indexOf("\""));
if (IPDEBUG == 1 ) {
DEBUG_SERIAL.println("apip : " + apip);
DEBUG_SERIAL.println("staip : " + staip);
}
if (!staip.equals("0.0.0.0")) return "http://" + staip;
else return "http://" + apip;
}
return "IP err";
}
// **************************************************
void wifi_init() {
DEBUG_SERIAL.println("ESP8266 init");
wifi_hardreset();
delay(2000); // wait for boot
wifi_buffclr();
if (!already_connected()) {
if (!wifi_softreset()) DEBUG_SERIAL.println("wifi_softreset failed");
if (!wifi_cwmode3()) DEBUG_SERIAL.println("wifi_cwmode3 failed");
for (int i = 0; i < 2; i++) { // attempt 2 times to connect to wifi
if (wifi_connect()) {
wifi_connected = true;
break;
}
else {
wifi_connected = false;
DEBUG_SERIAL.println("wifi_connect failed");
wifi_hardreset();
delay(2000); // wait for boot
if (!wifi_softreset()) DEBUG_SERIAL.println("wifi_softreset failed");
if (!wifi_cwmode3()) DEBUG_SERIAL.println("wifi_cwmode3 failed");
}
}
}
else wifi_connected = true;
if (!wifi_connected) delay(2000);
wifi_buffclr();
if (!wifi_multicon()) DEBUG_SERIAL.println("wifi_multicon failed");
delay(100);
if (!wifi_startserv80()) DEBUG_SERIAL.println("wifi_startserver80 failed");
delay(250);
wifi_buffclr();
}