-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC.ino
508 lines (425 loc) · 11.2 KB
/
AC.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* WiFi Microphone program For ESP8266
* WiFi Configuration File
*
* Author: M. Mahdi K. Kanan
* WiCardTech Engineering Group
*
* https://wicard.net
* https://store.wicard.net
*
* https://wicard.ir
* https://shop.wicard.net
*/
#include "AC.h"
void configPage_AC()
{
int i;
if(Server.arg("aps") != NULL) //Access point settings
{
for(i = EEP_AP_SSID; i < (EEP_AP_SSID+EEP_AP_SSID_LEN); i++)
{
APSsid[i-EEP_AP_SSID] = Server.arg("aps")[i-EEP_AP_SSID];
EEPROM.write(i, APSsid[i-EEP_AP_SSID]);
}
if(Server.arg("app") != NULL)
{
for(i = EEP_AP_PASS; i < (EEP_AP_PASS+EEP_AP_PASS_LEN); i++)
{
APPassword[i-EEP_AP_PASS] = Server.arg("app")[i-EEP_AP_PASS];
EEPROM.write(i, APPassword[i-EEP_AP_PASS]);
}
}
else
{
APPassword[0] = 0;
EEPROM.write(EEP_AP_PASS, 0);
}
if(Server.arg("sli") != NULL)
{
for(i = EEP_SECURE; i < (EEP_SECURE+EEP_SECURE_LEN); i++)
{
if(Server.arg("sli")[i-EEP_SECURE] == 0)
{
secureLink[i-EEP_SECURE] = 0;
EEPROM.write(i, 0);
break;
}
else if((Server.arg("sli")[i-EEP_SECURE] >= '0' && Server.arg("sli")[i-EEP_SECURE] <= '9')
|| (Server.arg("sli")[i-EEP_SECURE] >= 'A' && Server.arg("sli")[i-EEP_SECURE] <= 'Z')
|| (Server.arg("sli")[i-EEP_SECURE] >= 'a' && Server.arg("sli")[i-EEP_SECURE] <= 'z'))
{
secureLink[i-EEP_SECURE] = Server.arg("sli")[i-EEP_SECURE];
EEPROM.write(i, secureLink[i-EEP_SECURE]);
}
else
{
secureLink[0] = 0;
EEPROM.write(EEP_SECURE, 0);
break;
}
}
}
else
{
secureLink[0] = 0;
EEPROM.write(EEP_SECURE, 0);
}
if(Server.arg("hap") != NULL)
i = 1;
else
i = 0;
if(Server.arg("dap") != NULL)
i |= 2;
EEPROM.write(EEP_AC_CONFIG, (i&0xff));
EEPROM.commit();
//const String content = "";
PGM_P content = PSTR("<html><head><title>AutoConnect</title></head><body>Hot Spot Saved! Please re-start the module.</body></html>");
Server.send(200, "text/html", content);
}
else if(Server.arg("sts") != NULL) //station settings
{
for(i = EEP_ST_SSID; i < (EEP_ST_SSID+EEP_ST_SSID_LEN); i++)
{
STSsid[i-EEP_ST_SSID] = Server.arg("sts")[i-EEP_ST_SSID];
EEPROM.write(i, STSsid[i-EEP_ST_SSID]);
}
if(Server.arg("stp") != NULL)
{
for(i = EEP_ST_PASS; i < (EEP_ST_PASS+EEP_ST_PASS_LEN); i++)
{
STPassword[i-EEP_ST_PASS] = Server.arg("stp")[i-EEP_ST_PASS];
EEPROM.write(i, STPassword[i-EEP_ST_PASS]);
}
}
else
{
STPassword[0] = 0;
EEPROM.write(EEP_ST_PASS, 0);
}
EEPROM.commit();
#ifdef LOG_ENABLE
Serial.print("ST SSID:");
Serial.println(STSsid);
Serial.print("ST PASS:");
Serial.println(STPassword);
#endif
//const String content = "
PGM_P content = PSTR("<html><head><meta http-equiv='refresh' content='100; url=/config'><title>Connecting...</title></head><body>Please Wait...</body></html>");
Server.send(200, "text/html", content);
}
else if(Server.arg("b") != NULL && Server.arg("p") != NULL && Server.arg("a") != NULL)//rev 1.2
{
//bitrate
if(Server.arg("b")[0] == '0')
{
EEPROM.write(EEP_WM_BITRATE, 0);
ucWMBitrate = 0;
}
else if(Server.arg("b")[0] == '1')
{
EEPROM.write(EEP_WM_BITRATE, 1);
ucWMBitrate = 1;
}
else //if(Server.arg("b")[0] == '2')
{
EEPROM.write(EEP_WM_BITRATE, 2);
ucWMBitrate = 2;
}
//length
if(Server.arg("p")[0] == '0')
{
EEPROM.write(EEP_WM_PACKETLEN, 0);
ucWMPacketLen = 0;
}
else if(Server.arg("p")[0] == '1')
{
EEPROM.write(EEP_WM_PACKETLEN, 1);
ucWMPacketLen = 1;
}
else if(Server.arg("p")[0] == '2')
{
EEPROM.write(EEP_WM_PACKETLEN, 2);
ucWMPacketLen = 2;
}
else //if(Server.arg("p")[0] == '3')
{
EEPROM.write(EEP_WM_PACKETLEN, 3);
ucWMPacketLen = 3;
}
//amplitude
if(Server.arg("a")[0] == '0')
{
EEPROM.write(EEP_WM_AMP, 0);
ucWMAmp = 0;
}
else if(Server.arg("a")[0] == '1')
{
EEPROM.write(EEP_WM_AMP, 1);
ucWMAmp = 1;
}
else //if(Server.arg("a")[0] == '2')
{
EEPROM.write(EEP_WM_AMP, 2);
ucWMAmp = 2;
}
EEPROM.commit();
PGM_P content = PSTR("<html><head><title>Configuration Saved</title></head><body>Saved.</body></html>");
Server.send(200, "text/html", content);
}
else
{
/* HTBPACHpSIDRA rev 1.3
H: hidden hotspot=1
T: temp hotspot=1
B: bitrate
P: packet len
A: amplitude
C: conntected to SSID [RSSI]*IP*saved SSID*
H: hotspot SSID*
p: hotspot password*
S: secure link*
I: Linked ip*
D: ST MAC addr
R: AP MAC addr
A: available aps*/
String content = "";
if(byte(EEPROM.read(EEP_AC_CONFIG)) & 0x01)
content += 1;
else
content += 0;
if(byte(EEPROM.read(EEP_AC_CONFIG)) & 0x02)
content += 1;
else
content += 0;
content += int(ucWMBitrate);
content += int(ucWMPacketLen);
content += int(ucWMAmp);
if(WiFi.status() == WL_CONNECTED)
{
content += STSsid;
content += " [";
content += WiFi.RSSI();
content += "]*";
content += WiFi.localIP().toString();
content += "*";
}
else
content +="-*-*";
content += STSsid;
content += "*";
content += APSsid;
content += "*";
content += APPassword;
content += "*";
content += secureLink;
content += "*";
byte mac[6];
WiFi.macAddress(mac);
#ifdef LOG_ENABLE
Serial.print("MAC: ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
#endif
//ST Mac
content += String(mac[0],HEX)+":"+String(mac[1],HEX)+":"+String(mac[2],HEX)+":"+String(mac[3],HEX)+":"+String(mac[4],HEX)+":"+String(mac[5],HEX)+"*";
//AP Mac
content += String((mac[0]+2),HEX)+":"+String(mac[1],HEX)+":"+String(mac[2],HEX)+":"+String(mac[3],HEX)+":"+String(mac[4],HEX)+":"+String(mac[5],HEX)+"*";
#ifdef LOG_ENABLE
Serial.println("start scan");
#endif
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
#ifdef LOG_ENABLE
Serial.println("scan done");
#endif
if(n != 0)
{
#ifdef LOG_ENABLE
Serial.print(n);
Serial.println(" networks found");
#endif
for(int i = 0; i < n; ++i)
{
content += "<div class='ap'><b onclick='ap(this.innerHTML);'>" + WiFi.SSID(i) + "</b><b class='rssi'>" + WiFi.RSSI(i) + "</b></div>";
// Print SSID and RSSI for each network found
#ifdef LOG_ENABLE
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.println(")");
delay(10);
#endif
}
}
#ifdef LOG_ENABLE
else
{
Serial.println("no networks found");
}
#endif
Server.send(200, "text/html", content);
}
}
void reset_AC()
{
int i;
#ifdef LOG_ENABLE
Serial.print("EE Reset...");
#endif
STSsid[0] = 0;
STPassword[0] = 0;
APSsid[0] = 'W';
APSsid[1] = 'i';
APSsid[2] = 'C';
APSsid[3] = 'a';
APSsid[4] = 'r';
APSsid[5] = 'd';
APSsid[6] = 'M';
APSsid[7] = 'p';
APSsid[8] = 0;
APPassword[0] = '1';
APPassword[1] = '2';
APPassword[2] = '3';
APPassword[3] = '4';
APPassword[4] = '5';
APPassword[5] = '6';
APPassword[6] = '7';
APPassword[7] = '8';
APPassword[8] = 0;
EEPROM.write(EEP_AC_KEY, MEM_SAVED);
EEPROM.write(EEP_ST_SSID, 0);
EEPROM.write(EEP_ST_PASS, 0);
for(i = EEP_AP_SSID; i < (EEP_AP_SSID+EEP_AP_SSID_LEN); i++)
EEPROM.write(i, APSsid[i-EEP_AP_SSID]);
for(i = EEP_AP_PASS; i < (EEP_AP_PASS+EEP_AP_PASS_LEN); i++)
EEPROM.write(i, APPassword[i-EEP_AP_PASS]);
EEPROM.write(EEP_SECURE, 0);
//EEPROM.write(EEP_AC_CONFIG, 0);
EEPROM.write(EEP_AC_CONFIG, 2);
//Your EEprom init
//rev 1.2
EEPROM.write(EEP_WM_BITRATE, 1);
ucWMBitrate = 1;
EEPROM.write(EEP_WM_PACKETLEN, 1);
ucWMPacketLen = 1;
EEPROM.write(EEP_WM_AMP, 2);
ucWMAmp = 0;
EEPROM.commit();
}
void init_AC()
{
#ifdef LOG_ENABLE
Serial.print("Autoconnect Init!");
#endif
int i;
DisableHotSpot = false;
if(byte(EEPROM.read(EEP_AC_KEY)) == MEM_SAVED)
{
#ifdef LOG_ENABLE
Serial.print("EE OK!");
#endif
for(i = EEP_SECURE; i < (EEP_SECURE+EEP_SECURE_LEN); i++)
secureLink[i-EEP_SECURE] = EEPROM.read(i);
for(i = EEP_ST_SSID; i < (EEP_ST_SSID+EEP_ST_SSID_LEN); i++)
STSsid[i-EEP_ST_SSID] = EEPROM.read(i);
for(i = EEP_ST_PASS; i < (EEP_ST_PASS+EEP_ST_PASS_LEN); i++)
STPassword[i-EEP_ST_PASS] = EEPROM.read(i);
for(i = EEP_AP_SSID; i < (EEP_AP_SSID+EEP_AP_SSID_LEN); i++)
APSsid[i-EEP_AP_SSID] = EEPROM.read(i);
for(i = EEP_AP_PASS; i < (EEP_AP_PASS+EEP_AP_PASS_LEN); i++)
APPassword[i-EEP_AP_PASS] = EEPROM.read(i);
//rev 1.2
ucWMBitrate = EEPROM.read(EEP_WM_BITRATE);
if(ucWMBitrate > 2)
ucWMBitrate = 1;
ucWMPacketLen = EEPROM.read(EEP_WM_PACKETLEN);
if(ucWMPacketLen > 3)
ucWMPacketLen = 1;
ucWMAmp = EEPROM.read(EEP_WM_AMP);
if(ucWMAmp > 2)
ucWMAmp = 0;
if(STSsid[0] != 0)
{
if(byte(EEPROM.read(EEP_AC_CONFIG)) & 0x02)
WiFi.mode(WIFI_STA);
else
WiFi.mode(WIFI_AP_STA);
WiFi.begin(STSsid, STPassword);
#ifdef LOG_ENABLE
Serial.print("Connecting To AP:");
Serial.print(STSsid);
#endif
}
}
else
{
#ifdef LOG_ENABLE
Serial.print("EE Init...");
#endif
reset_AC();
}
if(byte(EEPROM.read(EEP_AC_CONFIG)) & 0x01)
WiFi.softAP(APSsid, APPassword, 1, true);
else
WiFi.softAP(APSsid, APPassword);
#ifdef LOG_ENABLE
Serial.print("Autoconnect Init Done!");
#endif
}
void handle_AC()
{
if(WiFi.status() != WL_CONNECTED)
Delay10Sec();
if(WiFi.status() != WL_CONNECTED)
{
delay(200);
WiFi.mode(WIFI_AP);
delay(200);
if(STSsid[0] != 0)
{
#ifdef LOG_ENABLE
Serial.println("Reconnecting");
#endif
Delay10Sec();
Delay10Sec();
Delay10Sec();
//Delay10Sec();
//Delay10Sec();
//Delay10Sec();
#ifdef LOG_ENABLE
Serial.print("Connecting To AP:");
Serial.println(STSsid);
#endif
delay(200);
WiFi.mode(WIFI_AP_STA);
delay(200);
WiFi.begin(STSsid, STPassword);
delay(200);
Delay10Sec();
Delay10Sec();
}
DisableHotSpot = false;
}
else if(DisableHotSpot == false)
{
#ifdef LOG_ENABLE
Serial.println("WiFi connected: " + WiFi.localIP().toString());
#endif
if(byte(EEPROM.read(EEP_AC_CONFIG)) & 0x02)
WiFi.mode(WIFI_STA);
delay(200);
DisableHotSpot = true;
}
}