-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptivePortal-esp8266.ino
550 lines (462 loc) · 15.2 KB
/
CaptivePortal-esp8266.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <stdio.h>
#include <string.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <Hash.h>
#include "DNSServer.h"
extern "C"
{
#include "user_interface.h"
void system_set_os_print ( uint8 onoff );
void ets_install_putc1 ( void *routine );
}
ADC_MODE ( ADC_VCC ); // Set ADC for Voltage Monitoring
// Use the internal hardware buffer
static void _u0_putc ( char c )
{
while ( ( ( U0S >> USTXC ) & 0x7F ) == 0x7F );
U0F = c;
}
//***************************************************************************
// Global data section. *
//***************************************************************************
char ssid[] = "SbiHotspot";
bool DEBUG = 1;
// Maximum number of simultaneous clients connected (WebSocket)
#define MAX_WS_CLIENT 5
#define CLIENT_NONE 0
#define CLIENT_ACTIVE 5
int counter =0;
// Web Socket client state
typedef struct
{
uint32_t id;
uint8_t state;
} _ws_client;
IPAddress ip ( 10, 10, 10, 1 ); // Private network for httpd
DNSServer dnsd; // Create the DNS object
AsyncWebServer httpd ( 80 ); // Instance of embedded webserver
AsyncWebSocket ws ( "/ws" ); // access at ws://[esp ip]/ws
_ws_client ws_client[MAX_WS_CLIENT]; // State Machine for WebSocket Client;
int rrcount;
char str_vcc[8];
const char* fp = "44 40 9E 34 92 2D E4 61 A4 89 A8 D5 7F 71 B7 62 B3 FD DD E1";
//***************************************************************************
// End of global data section. *
//***************************************************************************
//***************************************************************************
// D B G P R I N T *
//***************************************************************************
void dbg_printf ( const char *format, ... )
{
static char sbuf[1400] ; // For debug lines
va_list varArgs ; // For variable number of params
va_start ( varArgs, format ) ; // Prepare parameters
vsnprintf ( sbuf, sizeof ( sbuf ), format, varArgs ) ; // Format the message
va_end ( varArgs ) ; // End of using parameters
Serial.println ( sbuf ) ;
}
//***************************************************************************
// F O R M A T B Y T E S *
//***************************************************************************
String formatBytes ( size_t bytes )
{
if ( bytes < 1024 )
{
return String ( bytes ) + " B";
}
else if ( bytes < ( 1024 * 1024 ) )
{
return String ( bytes / 1024.0 ) + " KB";
}
else if ( bytes < ( 1024 * 1024 * 1024 ) )
{
return String ( bytes / 1024.0 / 1024.0 ) + " MB";
}
else
{
return String ( bytes / 1024.0 / 1024.0 / 1024.0 ) + " GB";
}
}
//***************************************************************************
// S E T U P *
//***************************************************************************
void setup ( void )
{
uint8_t mac[6];
ets_install_putc1 ( ( void * ) &_u0_putc );
system_set_os_print ( 1 );
system_update_cpu_freq ( 160 ) ; // Set CPU to 80/160 MHz
Serial.begin ( 115200 ) ; // For debug
Serial.println() ;
// Setup Access Point
setupAP();
WiFi.softAPmacAddress ( mac );
dbg_printf ( "SYSTEM ---" );
dbg_printf ( "getSdkVersion: %s", ESP.getSdkVersion() );
dbg_printf ( "getBootVersion: %08X", ESP.getBootVersion() );
dbg_printf ( "getBootMode: %08X", ESP.getBootMode() );
dbg_printf ( "getChipId: %08X", ESP.getChipId() );
dbg_printf ( "getCpuFreqMHz: %d Mhz", ESP.getCpuFreqMHz() );
dbg_printf ( "getCycleCount: %u\n", ESP.getCycleCount() );
dbg_printf ( "POWER ---" );
dbg_printf ( "Voltage: %d.%d v\n ", (ESP.getVcc() / 1000), (ESP.getVcc() % 1000) );
dbg_printf ( "MEMORY ---" );
dbg_printf ( "getFreeHeap: %d B", ESP.getFreeHeap() );
dbg_printf ( "getSketchSize: %s", formatBytes ( ESP.getSketchSize() ).c_str() );
dbg_printf ( "getFreeSketchSpace: %s", formatBytes ( ESP.getFreeSketchSpace() ).c_str() );
dbg_printf ( "getFlashChipSize: %s", formatBytes ( ESP.getFlashChipRealSize() ).c_str() );
dbg_printf ( "getFlashChipSpeed: %d MHz\n", int ( ESP.getFlashChipSpeed() / 1000000 ) );
setupSPIFFS();
// Setup DNS Server
// if DNS Server is started with "*" for domain name,
// it will reply with provided IP to all DNS request
dbg_printf ( "Starting DNS Server" ) ;
dnsd.start ( 53, "*", ip );
httpd.onNotFound ( onRequest ) ;
setupHTTPServer();
setupOTAServer();
dbg_printf ( "\nReady!\n--------------------" ) ;
}
void setupAP()
{
WiFi.mode ( WIFI_AP );
WiFi.softAPConfig ( ip, ip, IPAddress ( 255, 255, 255, 0 ) );
WiFi.softAP ( ssid );
}
void setupSPIFFS()
{
FSInfo fs_info ; // Info about SPIFFS
Dir dir ; // Directory struct for SPIFFS
File f ; // Filehandle
String filename ; // Name of file found in SPIFFS
SPIFFS.begin() ; // Enable file system
// Show some info about the SPIFFS
uint16_t cnt = 0;
SPIFFS.info ( fs_info ) ;
dbg_printf ( "SPIFFS Files\nName - Size" );
dir = SPIFFS.openDir ( "/" ) ; // Show files in FS
while ( dir.next() ) // All files
{
f = dir.openFile ( "r" ) ;
filename = dir.fileName() ;
dbg_printf ( "%-30s - %9s", // Show name and size
filename.c_str(),
formatBytes ( f.size() ).c_str()
) ;
cnt++;
}
dbg_printf ( "%d Files, %s of %s Used",
cnt,
formatBytes ( fs_info.usedBytes ).c_str(),
formatBytes ( fs_info.totalBytes ).c_str()
);
dbg_printf ( "%s Free\n",
formatBytes ( fs_info.totalBytes - fs_info.usedBytes ).c_str()
);
}
void setupHTTPServer()
{
// Web Server Document Setup
dbg_printf ( "Starting HTTP Captive Portal" ) ;
httpd.on ( "/", HTTP_GET, onRequest ) ; // Handle startpage
httpd.onNotFound ( onRequest ) ; // Handle file from FS
httpd.on ( "/trigger", HTTP_GET, [] ( AsyncWebServerRequest * request )
{
AsyncWebHeader *h = request->getHeader ( "User-Agent" );
rrcount++;
IPAddress remoteIP = request->client()->remoteIP();
request->send ( 200, "text/html", String ( rrcount ) ) ;
} );
// attach AsyncWebSocket
dbg_printf ( "Starting Websocket Console" ) ;
ws.onEvent ( onEvent );
httpd.addHandler ( &ws );
httpd.begin() ;
}
void setupOTAServer()
{
dbg_printf ( "Starting OTA Update Server" ) ;
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("FreeeWiFi");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
// OTA callbacks
ArduinoOTA.onStart ( []()
{
ws.enable ( false ); // Disable client connections
dbg_printf ( "OTA Update Started" ); // Let connected clients know what's going on
} );
ArduinoOTA.onEnd ( []()
{
dbg_printf ( "OTA Update Complete!\n" );
if ( ws.count() )
{
ws.closeAll(); // Close connected clients
delay ( 1000 );
}
} );
ArduinoOTA.onProgress ( [] ( unsigned int progress, unsigned int total )
{
dbg_printf ( "Progress: %u%%\r", ( progress / ( total / 100 ) ) );
} );
ArduinoOTA.onError ( [] ( ota_error_t error )
{
dbg_printf ( "Error[%u]: ", error );
if ( error == OTA_AUTH_ERROR ) dbg_printf ( "Auth Failed" );
else if ( error == OTA_BEGIN_ERROR ) dbg_printf ( "Begin Failed" );
else if ( error == OTA_CONNECT_ERROR ) dbg_printf ( "Connect Failed" );
else if ( error == OTA_RECEIVE_ERROR ) dbg_printf ( "Receive Failed" );
else if ( error == OTA_END_ERROR ) dbg_printf ( "End Failed" );
} );
ArduinoOTA.begin();
}
//***************************************************************************
// L O O P *
//***************************************************************************
// Main program loop. *
//***************************************************************************
void loop ( void )
{
dnsd.processNextRequest();
delay(1000);
if(counter>100){
dbg_printf ( "restart" ) ;
ws.closeAll() ;
ESP.restart() ;
counter=0;
}
counter++;
Serial.println(counter);
ArduinoOTA.handle(); // Handle remote Wifi Updates
}
//***************************************************************************
// HTTPD onRequest *
//***************************************************************************
void onRequest ( AsyncWebServerRequest *request )
{
digitalWrite ( LED_BUILTIN, LOW ); // Turn the LED on by making the voltage LOW
IPAddress remoteIP = request->client()->remoteIP();
dbg_printf (
"HTTP[%d]: %s%s",
remoteIP[3],
request->host().c_str(),
request->url().c_str()
) ;
String path = request->url();
if ( path.endsWith ( "/" ) )
path += "index.html";
if ( !SPIFFS.exists ( path ) && !SPIFFS.exists ( path + ".gz" ) )
{
//AsyncWebHeader *h = request->getHeader ( "User-Agent" );
// Redirect to captive portal
// dbg_printf ( "HTTP[%d]: Redirected to captive portal\n%s", remoteIP[3], h->value().c_str() ) ;
request->redirect ( "http://www.SbiHotspot.com/index.html" );
}
else
{
if ( !request->hasParam ( "download" ) && SPIFFS.exists ( path + ".gz" ) )
{
request->send ( SPIFFS, path, String(), request->hasParam ( "download" ) );
}
else
{
request->send ( SPIFFS, path ) ; // Okay, send the file
}
}
digitalWrite ( LED_BUILTIN, HIGH ); // Turn the LED off by making the voltage HIGH
}
//***************************************************************************
// WebSocket onEvent *
//***************************************************************************
// Manage routing of websocket events *
//***************************************************************************
void onEvent ( AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len )
{
if ( type == WS_EVT_CONNECT )
{
uint8_t index;
//dbg_printf ( "ws[%s][%u] connect\n", server->url(), client->id() );
for ( index = 0; index < MAX_WS_CLIENT ; index++ )
{
if ( ws_client[index].id == 0 )
{
ws_client[index].id = client->id();
ws_client[index].state = CLIENT_ACTIVE;
//dbg_printf ( "added #%u at index[%d]\n", client->id(), index );
client->printf ( "[[b;green;]Hello Client #%u, added you at index %d]", client->id(), index );
client->ping();
break; // Exit for loop
}
}
if ( index >= MAX_WS_CLIENT )
{
dbg_printf ( "not added, table is full" );
}
}
else if ( type == WS_EVT_DISCONNECT )
{
dbg_printf ( "ws[%s][%u] disconnect: %u\n", server->url(), client->id() );
for ( uint8_t i = 0; i < MAX_WS_CLIENT ; i++ )
{
if ( ws_client[i].id == client->id() )
{
ws_client[i].id = 0;
ws_client[i].state = CLIENT_NONE;
dbg_printf ( "freed[%d]\n", i );
break; // Exit for loop
}
}
}
else if ( type == WS_EVT_ERROR )
{
dbg_printf ( "WS[%u]: error(%u) - %s", client->id(), * ( ( uint16_t * ) arg ), ( char * ) data );
}
else if ( type == WS_EVT_DATA )
{
//data packet
AwsFrameInfo *info = ( AwsFrameInfo * ) arg;
char *msg = NULL;
size_t n = info->len;
uint8_t index;
// Size of buffer needed
// String same size +1 for \0
// Hex size*3+1 for \0 (hex displayed as "FF AA BB ...")
n = info->opcode == WS_TEXT ? n + 1 : n * 3 + 1;
msg = ( char * ) calloc ( n, sizeof ( char ) );
if ( msg )
{
// Grab all data
for ( size_t i = 0; i < info->len; i++ )
{
if ( info->opcode == WS_TEXT )
{
msg[i] = ( char ) data[i];
}
else
{
sprintf_P ( msg + i * 3, PSTR ( "%02x " ), ( uint8_t ) data[i] );
}
}
}
//dbg_printf ( "ws[%s][%u] message %s\n", server->url(), client->id(), msg );
// Search if it's a known client
for ( index = 0; index < MAX_WS_CLIENT ; index++ )
{
if ( ws_client[index].id == client->id() )
{
//dbg_printf ( "known[%d] '%s'\n", index, msg );
//dbg_printf ( "client #%d info state=%d\n", client->id(), ws_client[index].state );
// Received text message
if ( info->opcode == WS_TEXT )
{
execCommand ( client, msg );
}
else
{
dbg_printf ( "Binary 0x:%s", msg );
}
// Exit for loop
break;
} // if known client
} // for all clients
// Free up allocated buffer
if ( msg )
free ( msg );
} // EVT_DATA
}
//***************************************************************************
// WebSocket execCommand *
//***************************************************************************
// translate and execute command *
//***************************************************************************
void execCommand ( AsyncWebSocketClient * client, char * msg )
{
uint16_t l = strlen ( msg );
uint8_t index = MAX_WS_CLIENT;
// Search if we're known client
if ( client )
{
for ( index = 0; index < MAX_WS_CLIENT ; index++ )
{
// Exit for loop if we are there
if ( ws_client[index].id == client->id() )
break;
} // for all clients
}
else
{
return;
}
// Custom command to talk to device
if ( !strcasecmp_P ( msg, PSTR ( "ping" ) ) )
{
client->printf_P ( PSTR ( "received your [[b;cyan;]ping], here is my [[b;cyan;]pong]" ) );
}
else if ( !strcasecmp_P ( msg, PSTR ( "debug" ) ) )
{
DEBUG = !DEBUG;
if ( DEBUG )
{
client->printf_P ( PSTR ( "[[b;green;]Debug output enabled]" ) );
}
else
{
client->printf_P ( PSTR ( "[[b;red;]Debug output disabled]" ) );
}
}
// Dir files on SPIFFS system
// --------------------------
else if ( !strcasecmp_P ( msg, PSTR ( "ls" ) ) )
{
FSInfo fs_info ;
uint16_t cnt = 0;
String filename;
Dir dir = SPIFFS.openDir ( "/" ) ; // Show files in FS
while ( dir.next() ) // All files
{
cnt++;
File f = dir.openFile ( "r" ) ;
filename = dir.fileName() ;
f.close();
}
SPIFFS.info ( fs_info ) ;
}
else if ( !strcasecmp_P ( msg, PSTR ( "who" ) ) )
{
uint8_t cnt = 0;
// Count client
for ( uint8_t i = 0; i < MAX_WS_CLIENT ; i++ )
{
if ( ws_client[i].id )
{
cnt++;
}
}
}
else if ( !strcasecmp_P ( msg, PSTR ( "info" ) ) )
{
uint8_t mac[6];
WiFi.softAPmacAddress ( mac );
}
else if ( ( l > 5 && !strncasecmp_P ( msg, PSTR ( "ssid " ), 5 ) ) )
{
sprintf ( ssid, "%s", &msg[5] );
dbg_printf ( "[[b;yellow;]Changing SSID:] %s" , ssid );
setupAP();
}
else if ( !strcasecmp_P ( msg, PSTR ( "reboot" ) ) )
{
ws.closeAll() ;
delay ( 1000 ) ;
ESP.restart() ;
}
dbg_printf ( "WS[%d]: %s", client->id(), msg );
}