-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCaptive Portal_ESP32_LED_Matrix.ino
1294 lines (1185 loc) · 49.1 KB
/
Captive Portal_ESP32_LED_Matrix.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#include <DNSServer.h>
#include <EEPROM.h>
//#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
#define GPIO_OUT_W1TS_REG (DR_REG_GPIO_BASE + 0x0008)
#define GPIO_OUT_W1TC_REG (DR_REG_GPIO_BASE + 0x000c)
//#define PIXEL_PIN 17 // Digital IO pin connected to the NeoPixels.
#define LED_PIN 17
#define COLOR_ORDER GRB
#define CHIPSET WS2812
static const byte WiFiPwdLen = 25;
static const byte APSTANameLen = 20;
struct WiFiEEPromData
{
bool APSTA = true; // Access Point or Sation Mode - true AP Mode
bool PwDReq = false; // PasswordRequired
bool CapPortal = true ; //CaptivePortal on in AP Mode
char APSTAName[APSTANameLen]; // STATION /AP Point Name TO cONNECT, if definded
char WiFiPwd[WiFiPwdLen]; // WiFiPAssword, if definded
char ConfigValid[3]; //If Config is Vaild, Tag "TK" is required"
};
struct BMPHeader // BitMapStucture
{
uint32_t fileSize; // Serial.print("File size: ");
uint32_t creatorBytes; //
uint32_t imageOffset; // Start of image data "Image Offset:
uint32_t headerSize; // Serial.print("Header size: ");
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t depth; // bits per pixel
uint32_t format;
};
/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
const char *ESPHostname = "ESP32";
// DNS server
const byte DNS_PORT = 53;
DNSServer dnsServer;
//Conmmon Paramenters
bool SoftAccOK = false;
// Web server
WebServer server(80);
/* Soft AP network parameters */
IPAddress apIP(172, 20, 0, 1);
IPAddress netMsk(255, 255, 255, 0);
/** Should I connect to WLAN asap? */
//boolean connect;
/** Last time I tried to connect to WLAN */
//long lastConnectTry = 0;
unsigned long currentMillis = 0;
unsigned long startMillis;
//const short period = 10; // Sleep after this Minutes of inactivity
/** Current WLAN status */
short status = WL_IDLE_STATUS;
File fsUploadFile; // a File object to temporarily store the received file
WiFiEEPromData MyWiFiConfig;
String getContentType(String filename); // convert the file extension to the MIME type
bool handleFileRead(String path); // send the right file to the client (if it exists)
void handleFileUpload(); // upload a new file to the SPIFFS
String temp ="";
int ScreenResWidth = 8; // LED Display xy Dimesions
int ScreenResHeight = 8;
int Pixel_Count = ScreenResWidth *ScreenResHeight; // Maximal LED'S
byte BRIGHTNESS = 100; // PresetBrightness
// Params for LED's
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 8;
const bool kMatrixSerpentineLayout = false;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
// const char* Headers[] = {"User-Agent", "Connection"}; // Gewünschte Header-Elemente festlegen
//Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixel_Count, PIXEL_PIN, NEO_RGB + NEO_KHZ800);
void setup()
{
REG_WRITE(GPIO_OUT_W1TS_REG, BIT(GPIO_NUM_16)); // Guru Meditation Error Remediation set
delay(1);
REG_WRITE(GPIO_OUT_W1TC_REG, BIT(GPIO_NUM_16)); // Guru Meditation Error Remediation clear
bool ConnectSuccess = false;
bool CreateSoftAPSucc = false;
bool CInitFSSystem = false;
bool CInitHTTPServer = false;
byte len;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
Serial.println(F("Serial Interface initalized at 9600 Baud."));
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
FastLED.show();
// strip.begin();
// strip.setBrightness(LED_Brightness); // Set globalBRIGHTNESS
// strip.show(); // Initialize all pixels to 'off'
WiFi.setAutoReconnect (false);
WiFi.persistent(false);
WiFi.disconnect();
WiFi.setHostname(ESPHostname); // Set the DHCP hostname assigned to ESP station.
if (loadCredentials()) // Load WLAN credentials for WiFi Settings
{
Serial.println(F("Valid Credentials found."));
if (MyWiFiConfig.APSTA == true) // AP Mode
{
Serial.println(F("Access Point Mode selected."));
Serial.println(MyWiFiConfig.APSTA);
len = strlen(MyWiFiConfig.APSTAName);
MyWiFiConfig.APSTAName[len+1] = '\0';
len = strlen(MyWiFiConfig.WiFiPwd);
MyWiFiConfig.WiFiPwd[len+1] = '\0';
CreateSoftAPSucc = CreateWifiSoftAP();
} else
{
Serial.println(F("Station Mode selected."));
len = strlen(MyWiFiConfig.APSTAName);
MyWiFiConfig.APSTAName[len+1] = '\0';
len = strlen(MyWiFiConfig.WiFiPwd);
MyWiFiConfig.WiFiPwd[len+1] = '\0';
len = ConnectWifiAP();
if ( len == 3 ) { ConnectSuccess = true; } else { ConnectSuccess = false; }
}
} else
{ //Set default Config - Create AP
Serial.println(F("NO Valid Credentials found."));
SetDefaultWiFiConfig ();
CreateSoftAPSucc = CreateWifiSoftAP();
saveCredentials();
// Blink
delay(500);
}
// Initalize Filesystem
CInitFSSystem = InitalizeFileSystem();
if (!(CInitFSSystem)) {Serial.println(F("File System not initalized ! ")); }
if ((ConnectSuccess or CreateSoftAPSucc))
{
Serial.print (F("IP Address: "));
if (CreateSoftAPSucc) { Serial.println(WiFi.softAPIP());}
if (ConnectSuccess) { Serial.println(WiFi.localIP());}
InitalizeHTTPServer();
}
else
{
Serial.setDebugOutput(true); //Debug Output for WLAN on Serial Interface.
Serial.println(F("Error: Cannot connect to WLAN. Set DEFAULT Configuration."));
SetDefaultWiFiConfig();
CreateSoftAPSucc = CreateWifiSoftAP();
InitalizeHTTPServer();
SetDefaultWiFiConfig();
saveCredentials();
}
for ( int i = 0; i < 64; i++)
{
leds[i]= 0x000000;
}
FastLED.show(); // Clear Display :)
}
void InitalizeHTTPServer()
{
bool initok = false;
/* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
server.on("/", handleRoot);
server.on("/wifi", handleWifi);
server.on("/filesystem", HTTP_GET,handleDisplayFS);
server.on("/upload", HTTP_POST, []() {
server.send(200, "text/plain", "");
}, handleFileUpload);
//if (MyWiFiConfig.CapPortal) { server.on("/generate_204", handleRoot); } //Android captive portal. Maybe not needed. Might be handled by notFound handler.
// if (MyWiFiConfig.CapPortal) { server.on("/favicon.ico", handleRoot); } //Another Android captive portal. Maybe not needed. Might be handled by notFound handler. Checked on Sony Handy
// if (MyWiFiConfig.CapPortal) { server.on("/fwlink", handleRoot); } //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/generate_204", handleRoot); //Android captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/favicon.ico", handleRoot); //Another Android captive portal. Maybe not needed. Might be handled by notFound handler. Checked on Sony Handy
server.on("/fwlink", handleRoot); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.onNotFound ( handleNotFound );
// Speicherung Header-Elemente anfordern
// server.collectHeaders(Headers, sizeof(Headers)/ sizeof(Headers[0]));
server.begin(); // Web server start
}
boolean InitalizeFileSystem() {
bool initok = false;
initok = SPIFFS.begin();
delay(200);
if (!(initok))
{
Serial.println(F("Format SPIFFS"));
SPIFFS.format();
initok = SPIFFS.begin();
}
return initok;
}
boolean CreateWifiSoftAP()
{
WiFi.disconnect();
Serial.print(F("Initalize SoftAP "));
if (MyWiFiConfig.PwDReq)
{
SoftAccOK = WiFi.softAP(MyWiFiConfig.APSTAName, MyWiFiConfig.WiFiPwd); // Passwortlänge mindestens 8 Zeichen !
} else
{
SoftAccOK = WiFi.softAP(MyWiFiConfig.APSTAName); // Access Point WITHOUT Password
// Overload Function:; WiFi.softAP(ssid, password, channel, hidden)
}
delay(2000); // Without delay I've seen the IP address blank
WiFi.softAPConfig(apIP, apIP, netMsk);
if (SoftAccOK)
{
//Serial.println("IP Adress: " + WiFi.softAPIP());
//Serial.println(MyWiFiConfig.APSTAName);
//Serial.println(MyWiFiConfig.WiFiPwd);
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", apIP);
Serial.println(F("successful."));
// Serial.setDebugOutput(true); // Debug Output for WLAN on Serial Interface.
} else
{
Serial.println(F("Soft AP Error."));
Serial.println(MyWiFiConfig.APSTAName);
Serial.println(MyWiFiConfig.WiFiPwd);
}
return SoftAccOK;
}
byte ConnectWifiAP()
{
Serial.println(F("Initalizing Wifi Client."));
byte connRes = 0;
byte i = 0;
WiFi.disconnect();
WiFi.softAPdisconnect(true); // Function will set currently configured SSID and password of the soft-AP to null values. The parameter is optional. If set to true it will switch the soft-AP mode off.
delay(500);
// Serial.println("Length of PAssword = "); Serial.println(strlen(MyWiFiConfig.WiFiPwd));
// Serial.println("Size of PAssword = "); Serial.println(sizeof(MyWiFiConfig.WiFiPwd));
// Serial.println(MyWiFiConfig.WiFiPwd);
// Serial.println("Length of PAssword = "); Serial.println(strlen(pass));
// Serial.println("Size of PAssword = "); Serial.println(sizeof(pass));
// Serial.println(pass);
WiFi.begin(MyWiFiConfig.APSTAName, MyWiFiConfig.WiFiPwd);
connRes = WiFi.waitForConnectResult();
while (( connRes == 0 ) and (i != 10)) //if connRes == 0 "IDLE_STATUS - change Statius"
{
connRes = WiFi.waitForConnectResult();
delay(2000);
i++;
Serial.print(F("."));
// statement(s)
}
while (( connRes == 1 ) and (i != 10)) //if connRes == 1 NO_SSID_AVAILin - SSID cannot be reached
{
connRes = WiFi.waitForConnectResult();
delay(2000);
i++;
Serial.print(F("."));
// statement(s)
}
if (connRes == 3 ) {
WiFi.setAutoReconnect(true); // Set whether module will attempt to reconnect to an access point in case it is disconnected.
// Setup MDNS responder
if (!MDNS.begin(ESPHostname)) {
Serial.println(F("Error: MDNS"));
} else { MDNS.addService("http", "tcp", 80); }
}
while (( connRes == 4 ) and (i != 10)) //if connRes == 4 Bad Password. Sometimes happens this with corrct PWD
{
WiFi.begin(MyWiFiConfig.APSTAName, MyWiFiConfig.WiFiPwd);
connRes = WiFi.waitForConnectResult();
delay(2000);
i++;
Serial.print(F("."));
}
if (connRes == 4 ) {
Serial.println(F("STA Pwd Err"));
Serial.println(MyWiFiConfig.APSTAName);
Serial.println(MyWiFiConfig.WiFiPwd);
WiFi.disconnect();
}
// if (connRes == 6 ) { Serial.println("DISCONNECTED - Not in station mode"); }
// WiFi.printDiag(Serial);
Serial.println("");
return connRes;
}
uint16_t read16(File f)
{
// BMP data is stored little-endian, same as Arduino.
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f)
{
// BMP data is stored little-endian, same as Arduino.
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
BMPHeader ReadBitmapSpecs(String filename)
{
File file;
BMPHeader BMPData;
file =SPIFFS.open(filename, "r");
if (!file)
{
file.close();
// Serial.print("File not found");
return BMPData;
}
// Parse BMP header
if (read16(file) == 0x4D42) // BMP signature
{
BMPData.fileSize = read32(file);
BMPData.creatorBytes = read32(file);
BMPData.imageOffset = read32(file); // Start of image data
BMPData.headerSize = read32(file);
BMPData.width = read32(file);
BMPData.height = read32(file);
BMPData.planes = read16(file);
BMPData.depth = read16(file); // bits per pixel
BMPData.format = read32(file);
}
file.close();
return BMPData;
}
#define SD_BUFFER_PIXELS 20
void drawBitmap_SPIFFS(String filename, uint8_t x, uint8_t y)
{
File file;
uint8_t buffer[3 * SD_BUFFER_PIXELS]; // pixel buffer, size for r,g,b
bool valid = false; // valid format to be handled
bool flip = true; // bitmap is stored bottom-to-top
uint32_t pos = 0;
// uint32_t startTime = millis();
// if ((x >= display.width()) || (y >= display.height())) return;
// Serial.print("Loading image into Display.. '");
// Serial.println(filename);
file =SPIFFS.open(filename, "r");
if (!file)
{
Serial.print(F("Filesytem Error"));
return;
}
// Parse BMP header
if (read16(file) == 0x4D42) // BMP signature
{
uint32_t fileSize = read32(file);
uint32_t creatorBytes = read32(file);
uint32_t imageOffset = read32(file); // Start of image data
uint32_t headerSize = read32(file);
uint32_t width = read32(file);
uint32_t height = read32(file);
uint16_t planes = read16(file);
uint16_t depth = read16(file); // bits per pixel
uint32_t format = read32(file);
if ((planes == 1) && (format == 0)) // uncompressed is handled
{
Serial.print("File size: "); Serial.println(fileSize);
Serial.print("Image Offset: "); Serial.println(imageOffset);
Serial.print("Header size: "); Serial.println(headerSize);
Serial.print("Bit Depth: "); Serial.println(depth);
Serial.print("Image size: ");
Serial.print(width);
Serial.print('x');
Serial.println(height);
// BMP rows are padded (if needed) to 4-byte boundary
uint32_t rowSize = (width * depth / 8 + 3) & ~3;
if (height < 0)
{
height = -height;
flip = false;
}
uint16_t w = width;
uint16_t h = height;
//if ((x + w - 1) >= display.width()) w = display.width() - x;
//if ((y + h - 1) >= display.height()) h = display.height() - y;
size_t buffidx = sizeof(buffer); // force buffer load
for (uint16_t row = 0; row < h; row++) // for each line
{
if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = imageOffset + (height - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = imageOffset + row * rowSize;
if (file.position() != pos)
{ // Need seek?
file.seek(pos,SeekSet); // if mode is SeekSet, position is set to offset bytes from the beginning.
// if mode is SeekCur, current position is moved by offset bytes.
// if mode is SeekEnd, position is set to offset bytes from the end of the
buffidx = sizeof(buffer); // force buffer reload
}
uint8_t bits;
for (uint16_t col = 0; col < w; col++) // for each pixel
{
// Time to read more pixel data?
if (buffidx >= sizeof(buffer))
{
file.read(buffer, sizeof(buffer));
buffidx = 0; // Set index to beginning
}
switch (depth)
{
case 1: // one bit per pixel b/w format
{
valid = true;
if (0 == col % 8)
{
bits = buffer[buffidx++];
}
uint16_t bw_color = bits & 0x80;
Serial.println(bw_color,HEX);
// FastLED.setPixelColor(col*row,bw_color);
// display.drawPixel(col, row, bw_color);
bits <<= 1;
}
break;
case 24: // standard BMP format
{
valid = true;
uint16_t b = buffer[buffidx++];
uint16_t g = buffer[buffidx++];
uint16_t r = buffer[buffidx++];
// uint16_t bw_color = ((r + g + b) / 3 > 0xFF / 2)
uint16_t PixelNum = (row*8)+col;
Serial.print("PixelNumber: ");
Serial.print(PixelNum);
Serial.print(" Row: ");
Serial.print(row);
Serial.print(" Colum: ");
Serial.print(col);
Serial.print(" Color: ");
Serial.print(r,HEX);
Serial.print(g,HEX);
Serial.print(b,HEX);
Serial.print(" - ");
Serial.print(col,HEX);
Serial.println("");
leds[PixelNum].red = r;
leds[PixelNum].green = g;
leds[PixelNum].blue = b;
}
break;
}
} // end pixel
} // end line
FastLED.show(); // Show results :)
}
}
file.close();
if (!(valid))
{
Serial.println(F("Err: BMP"));
}
}
void handleFileUpload() { // Dateien vom Rechnenknecht oder Klingelkasten ins SPIFFS schreiben
if (server.uri() != "/upload") return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (upload.filename.length() > 30) {
upload.filename = upload.filename.substring(upload.filename.length() - 30, upload.filename.length()); // Dateinamen auf 30 Zeichen kürzen
}
Serial.println("FileUpload Name: " + upload.filename);
if (!filename.startsWith("/")) filename = "/" + filename;
//fsUploadFile = SPIFFS.open(filename, "w");
fsUploadFile = SPIFFS.open("/" + server.urlDecode(upload.filename), "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
// Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile)
fsUploadFile.close();
// Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
// server.sendContent(Header);
handleDisplayFS();
}
}
void handleDisplayFS() { // HTML Filesystem
// Page: /filesystem
temp ="";
// HTML Header
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
// HTML Content
server.send ( 200, "text/html", temp );
temp += "<!DOCTYPE HTML><html lang='de'><head><meta charset='UTF-8'><meta name= viewport content='width=device-width, initial-scale=1.0,'>";
server.sendContent(temp);
temp = "";
temp += "<style type='text/css'><!-- DIV.container { min-height: 10em; display: table-cell; vertical-align: middle }.button {height:35px; width:90px; font-size:16px}";
server.sendContent(temp);
temp = "";
temp += "body {background-color: powderblue;}</style><head><title>File System Manager</title></head>";
temp += "<h2>Serial Peripheral Interface Flash Filesystem</h2><body><left>";
server.sendContent(temp);
temp = "";
if (server.args() > 0) // Parameter wurden ubergeben
{
if (server.hasArg("delete"))
{
String FToDel = server.arg("delete");
if (SPIFFS.exists(FToDel))
{
SPIFFS.remove(FToDel);
temp += "File " + FToDel + " successfully deleted.";
} else
{
temp += "File " + FToDel + " cannot be deleted.";
}
server.sendContent(temp);
temp = "";
}
if (server.hasArg("format") and server.arg("on"))
{
SPIFFS.format();
temp += "SPI File System successfully formatted.";
server.sendContent(temp);
temp = "";
} // server.client().stop(); // Stop is needed because we sent no content length
}
temp += "<table border=2 bgcolor = white width = 400 ><td><h4>Current SPIFFS Status: </h4>";
temp += formatBytes(SPIFFS.usedBytes() * 1.05) + " of " + formatBytes(SPIFFS.totalBytes()) + " used. <br>";
temp += formatBytes((SPIFFS.totalBytes() - (SPIFFS.usedBytes() * 1.05)))+ " free. <br>";
temp += "</td></table><br>";
server.sendContent(temp);
temp = "";
// Check for Site Parameters
temp += "<table border=2 bgcolor = white width = 400><tr><th><br>";
temp += "<h4>Available Files on SPIFFS:</h4><table border=2 bgcolor = white ></tr></th><td>Filename</td><td>Size</td><td>Action </td></tr></th>";
server.sendContent(temp);
temp = "";
File root = SPIFFS.open("/");
File file = root.openNextFile();
while (file)
{
temp += "<td> <a title=\"Download\" href =\"" + String(file.name()) + "\" download=\"" + String(file.name()) + "\">" + String(file.name()) + "</a> <br></th>";
temp += "<td>"+ formatBytes(file.size())+ "</td>";
temp += "<td><a href =filesystem?delete=" + String(file.name()) + "> Delete </a></td>";
temp += "</tr></th>";
file = root.openNextFile();
}
temp += "</tr></th>";
temp += "</td></tr></th><br></th></tr></table></table><br>";
temp += "<table border=2 bgcolor = white width = 400 ><td><h4>Upload</h4>";
temp += "<label> Choose File: </label>";
temp += "<form method='POST' action='/upload' enctype='multipart/form-data' style='height:35px;'><input type='file' name='upload' style='height:35px; font-size:13px;' required>\r\n<input type='submit' value='Upload' class='button'></form>";
temp += " </table><br>";
server.sendContent(temp);
temp = "";
temp += "<td><a href =filesystem?format=on> Format SPIFFS Filesystem. (Takes up to 30 Seconds) </a></td>";
temp += "<table border=2 bgcolor = white width = 500 cellpadding =5 ><caption><p><h3>Systemlinks:</h2></p></caption><tr><th><br>";
temp += " <a href='/'>Main Page</a><br><br></th></tr></table><br><br>";
server.sendContent(temp);
temp = "";
temp += "<footer><p>Programmed and designed by: Tobias Kuch</p><p>Contact information: <a href='mailto:tobias.kuch@googlemail.com'>tobias.kuch@googlemail.com</a>.</p></footer></body></html>";
//server.send ( 200, "", temp );
server.sendContent(temp);
server.client().stop(); // Stop is needed because we sent no content length
temp = "";
}
/** Load WLAN credentials from EEPROM */
bool loadCredentials()
{
bool RetValue;
EEPROM.begin(512);
EEPROM.get(0, MyWiFiConfig);
EEPROM.end();
if (String(MyWiFiConfig.ConfigValid) == String("TK"))
{
RetValue = true;
} else
{
RetValue = false; // WLAN Settings not found.
}
return RetValue;
}
/** Store WLAN credentials to EEPROM */
bool saveCredentials()
{
bool RetValue;
// Check logical Errors
RetValue = true;
if (MyWiFiConfig.APSTA == true ) //AP Mode
{
if (MyWiFiConfig.PwDReq and (sizeof(String(MyWiFiConfig.WiFiPwd)) < 8))
{
RetValue = false; // Invalid Config
}
if (sizeof(String(MyWiFiConfig.APSTAName)) < 1)
{
RetValue = false; // Invalid Config
}
} //else //Station Mode
// {
/// if (MyWiFiConfig.PwDReq and (sizeof(String(MyWiFiConfig.WiFiPwd)) < 8))
// {
// RetValue = false; // Invalid Config
// }
// if ((sizeof(String(MyWiFiConfig.APSTAName)) < 1) or MyWiFiConfig.CapPortal)
// {
// RetValue = false; // Invalid Config
// }
// }
// End Check logical Errors
if (RetValue)
{
EEPROM.begin(512);
for (int i = 0 ; i < sizeof(MyWiFiConfig) ; i++)
{
EEPROM.write(i, 0);
}
strncpy( MyWiFiConfig.ConfigValid , "TK", sizeof(MyWiFiConfig.ConfigValid) );
EEPROM.put(0, MyWiFiConfig);
EEPROM.commit();
EEPROM.end();
}
return RetValue;
}
void SetDefaultWiFiConfig()
{
byte len;
MyWiFiConfig.APSTA = true;
MyWiFiConfig.PwDReq = true; // default PW required
MyWiFiConfig.CapPortal = true;
strncpy( MyWiFiConfig.APSTAName, "ESP_Config", sizeof(MyWiFiConfig.APSTAName) );
len = strlen(MyWiFiConfig.APSTAName);
MyWiFiConfig.APSTAName[len+1] = '\0';
strncpy( MyWiFiConfig.WiFiPwd, "12345678", sizeof(MyWiFiConfig.WiFiPwd) );
len = strlen(MyWiFiConfig.WiFiPwd);
MyWiFiConfig.WiFiPwd[len+1] = '\0';
strncpy( MyWiFiConfig.ConfigValid, "TK", sizeof(MyWiFiConfig.ConfigValid) );
len = strlen(MyWiFiConfig.ConfigValid);
MyWiFiConfig.ConfigValid[len+1] = '\0';
Serial.println(F("Reset WiFi Credentials."));
}
void handleRoot() {
// Main Page:
temp = "";
short PicCount = 0;
byte ServArgs = 0;
//Building Page
// HTML Header
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
// HTML Content
server.send ( 200, "text/html", temp ); // Speichersparen - Schon mal dem Client senden
temp = "";
temp += "<!DOCTYPE HTML><html lang='de'><head><meta charset='UTF-8'><meta name= viewport content='width=device-width, initial-scale=1.0,'>";
temp += "<style type='text/css'><!-- DIV.container { min-height: 10em; display: table-cell; vertical-align: middle }.button {height:35px; width:90px; font-size:16px}";
server.sendContent(temp);
temp = "";
temp += "body {background-color: powderblue;}</style>";
temp += "<head><title>Tobi's LED Display</title></head>";
temp += "<h2>LED Display</h2>";
temp += "<body>";
server.sendContent(temp);
temp = "";
// Processing User Request
if (server.args() > 0) // Parameter wurden ubergeben
{
temp += "<br>Eingaben werden verarbeitet. Bitte warten..<br><br>";;
server.sendContent(temp);
temp = "";
// Update Background Paper
if (server.arg("PicSelect") == "off") // Display wird gelöscht
{
temp = "";
drawBitmap_SPIFFS(temp,0,0);
temp = "";
} else
{
temp = server.arg("PicSelect"); // Bild gewählt. Display inhalt per Picselect hergstellt
drawBitmap_SPIFFS(temp,0,0);
temp = "";
}
}
temp += "<table border=2 bgcolor = white ><caption><p><h3>Verfügbare Bilder für das Led Display</h2></p></caption>";
temp += "<form>";
temp += "<tr><th><input type='radio' name='PicSelect' value = 'off' checked> KEIN Bild anzeigen <br></th></tr>";
temp += "<tr><th>";
//Listet die Aktuell im SPIFFS vorhandenen Dateien auf
File root = SPIFFS.open("/");
File file = root.openNextFile();
PicCount = 1;
while (file)
{
if (String(file.name()).endsWith(".bmp") or String(file.name()).endsWith(".BMP"))
{
BMPHeader PicData = ReadBitmapSpecs(file.name());
if ((PicData.width < ScreenResWidth + 1) and (PicData.height < ScreenResHeight +1 )) // nur anzeigen , wenn maximale Displayauslösung unterstützt ist . Größere Bilder nicht mehr anzeigen
{
temp += "<label for='radio1'><img src='"+ String(file.name())+"' alt='"+ String(file.name())+"' border='3' bordercolor=green> Bild "+ PicCount+"</label><input type='radio' value='"+ String(file.name())+"' name='PicSelect'/> <br>";
temp += String(file.name())+ " Res: "+ String(PicData.width) + "x" + String(PicData.height) + "px Filesize: "+ formatBytes(file.size()) + "</th></tr><tr><th>";
PicCount ++;
}
}
file = root.openNextFile();
}
server.sendContent(temp);
temp = "";
// temp += "</table><br>";
temp += "<button type='submit' name='action' value='0' style='height: 50px; width: 280px'>Auf Display anzeigen</button>";
temp += "</form>";
server.sendContent(temp);
temp += "<br><table border=2 bgcolor = white width = 280 cellpadding =5 ><caption><p><h3>Systemlinks:</h2></p></caption>";
temp += "<tr><th><br>";
temp += "<a href='/wifi'>WIFI Einstellungen</a><br><br>";
temp += "<a href='/filesystem'>Filemanager</a><br><br>";
temp += "</th></tr></table><br><br>";
temp += "<footer><p>Programmed and designed by: Tobias Kuch</p><p>Contact information: <a href='mailto:tobias.kuch@googlemail.com'>tobias.kuch@googlemail.com</a>.</p></footer>";
temp += "</body></html>";
server.sendContent(temp);
temp = "";
server.client().stop(); // Stop is needed because we sent no content length
}
void handleNotFound() {
if (captivePortal())
{ // If caprive portal redirect instead of displaying the error page.
return;
}
if (!handleFileRead(server.uri()))
{
temp = "";
// HTML Header
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
// HTML Content
temp += "<!DOCTYPE HTML><html lang='de'><head><meta charset='UTF-8'><meta name= viewport content='width=device-width, initial-scale=1.0,'>";
temp += "<style type='text/css'><!-- DIV.container { min-height: 10em; display: table-cell; vertical-align: middle }.button {height:35px; width:90px; font-size:16px}";
temp += "body {background-color: powderblue;}</style>";
temp += "<head><title>File not found</title></head>";
temp += "<h2> 404 File Not Found</h2><br>";
temp += "<h4>Debug Information:</h4><br>";
temp += "<body>";
temp += "URI: ";
temp += server.uri();
temp += "\nMethod: ";
temp+= ( server.method() == HTTP_GET ) ? "GET" : "POST";
temp += "<br>Arguments: ";
temp += server.args();
temp += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
temp += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
temp += "<br>Server Hostheader: "+ server.hostHeader();
for ( uint8_t i = 0; i < server.headers(); i++ ) {
temp += " " + server.headerName ( i ) + ": " + server.header ( i ) + "\n<br>";
}
temp += "</table></form><br><br><table border=2 bgcolor = white width = 500 cellpadding =5 ><caption><p><h2>You may want to browse to:</h2></p></caption>";
temp += "<tr><th>";
temp += "<a href='/'>Main Page</a><br>";
temp += "<a href='/wifi'>WIFI Settings</a><br>";
temp += "<a href='/filesystem'>Filemanager</a><br>";
temp += "</th></tr></table><br><br>";
temp += "<footer><p>Programmed and designed by: Tobias Kuch</p><p>Contact information: <a href='mailto:tobias.kuch@googlemail.com'>tobias.kuch@googlemail.com</a>.</p></footer>";
temp += "</body></html>";
server.send ( 404, "", temp );
server.client().stop(); // Stop is needed because we sent no content length
temp = "";
}
}
/** Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
boolean captivePortal() {
if (!isIp(server.hostHeader()) && server.hostHeader() != (String(ESPHostname)+".local")) {
// Serial.println("Request redirected to captive portal");
server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true);
server.send ( 302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
server.client().stop(); // Stop is needed because we sent no content length
return true;
}
return false;
}
/** Wifi config page handler */
void handleWifi()
{
// Page: /wifi
byte i;
byte len ;
temp = "";
// Check for Site Parameters
if (server.hasArg("Reboot") ) // Reboot System
{
temp = "Rebooting System in 5 Seconds..";
server.send ( 200, "text/html", temp );
delay(5000);
server.client().stop();
WiFi.disconnect();
delay(1000);
}
if (server.hasArg("WiFiMode") and (server.arg("WiFiMode") == "1") ) // STA Station Mode Connect to another WIFI Station
{
startMillis = millis(); // Reset Time Up Counter to avoid Idle Mode whiole operating
// Connect to existing STATION
if ( sizeof(server.arg("WiFi_Network")) > 0 )
{
Serial.println("STA Mode");
MyWiFiConfig.APSTA = false; // Access Point or Station Mode - false Station Mode
temp = "";
for ( i = 0; i < APSTANameLen;i++) { MyWiFiConfig.APSTAName[i] = 0; }
temp = server.arg("WiFi_Network");
len = temp.length();
for ( i = 0; i < len;i++)
{
MyWiFiConfig.APSTAName[i] = temp[i];
}
// MyWiFiConfig.APSTAName[len+1] = '\0';
temp = "";
for ( i = 0; i < WiFiPwdLen;i++) { MyWiFiConfig.WiFiPwd[i] = 0; }
temp = server.arg("STAWLanPW");
len = temp.length();
for ( i = 0; i < len;i++)
{
if (temp[i] > 32) //Steuerzeichen raus
{
MyWiFiConfig.WiFiPwd[i] = temp[i];
}
}
// MyWiFiConfig.WiFiPwd[len+1] = '\0';
temp = "WiFi Connect to AP: -";
temp += MyWiFiConfig.APSTAName;
temp += "-<br>WiFi PW: -";
temp += MyWiFiConfig.WiFiPwd;
temp += "-<br>";
temp += "Connecting to STA Mode in 2 Seconds..<br>";
server.send ( 200, "text/html", temp );
server.sendContent(temp);
delay(2000);
server.client().stop();
server.stop();
temp = "";
WiFi.disconnect();
WiFi.softAPdisconnect(true);
delay(500);
// ConnectWifiAP
bool SaveOk = saveCredentials();
i = ConnectWifiAP();
delay(700);
if (i != 3) // 4: WL_CONNECT_FAILED - Password is incorrect 1: WL_NO_SSID_AVAILin - Configured SSID cannot be reached
{
Serial.print(F("Cannot Connect to specified Network. Reason: "));
Serial.println(i);
server.client().stop();
delay(100);
WiFi.setAutoReconnect (false);
delay(100);
WiFi.disconnect();
delay(1000);
SetDefaultWiFiConfig();
CreateWifiSoftAP();
return;
} else
{
// Safe Config
bool SaveOk = saveCredentials();
InitalizeHTTPServer();
return;
}
}
}
if (server.hasArg("WiFiMode") and (server.arg("WiFiMode") == "2") ) // Change AP Mode
{
startMillis = millis(); // Reset Time Up Counter to avoid Idle Mode whiole operating
// Configure Access Point
temp = server.arg("APPointName");
len = temp.length();
temp =server.arg("APPW");
if (server.hasArg("PasswordReq"))
{
i = temp.length();
} else { i = 8; }
if ( ( len > 1 ) and (server.arg("APPW") == server.arg("APPWRepeat")) and ( i > 7) )
{
temp = "";
Serial.println(F("APMode"));
MyWiFiConfig.APSTA = true; // Access Point or Sation Mode - true AP Mode
if (server.hasArg("CaptivePortal"))
{
MyWiFiConfig.CapPortal = true ; //CaptivePortal on in AP Mode
} else { MyWiFiConfig.CapPortal = false ; }
if (server.hasArg("PasswordReq"))
{
MyWiFiConfig.PwDReq = true ; //Password Required in AP Mode
} else { MyWiFiConfig.PwDReq = false ; }
for ( i = 0; i < APSTANameLen;i++) { MyWiFiConfig.APSTAName[i] = 0; }
temp = server.arg("APPointName");
len = temp.length();
for ( i = 0; i < len;i++) { MyWiFiConfig.APSTAName[i] = temp[i]; }
MyWiFiConfig.APSTAName[len+1] = '\0';
temp = "";
for ( i = 0; i < WiFiPwdLen;i++) { MyWiFiConfig.WiFiPwd[i] = 0; }
temp = server.arg("APPW");
len = temp.length();
for ( i = 0; i < len;i++) { MyWiFiConfig.WiFiPwd[i] = temp[i]; }
MyWiFiConfig.WiFiPwd[len+1] = '\0';
temp = "";
if (saveCredentials()) // Save AP ConfigCongfig
{
temp = "Daten des AP Modes erfolgreich gespeichert. Reboot notwendig.";
} else { temp = "Daten des AP Modes fehlerhaft."; }
} else if (server.arg("APPW") != server.arg("APPWRepeat"))
{
temp = "";
temp = "WLAN Passwort nicht gleich. Abgebrochen.";
} else
{
temp = "";
temp = "WLAN Passwort oder AP Name zu kurz. Abgebrochen.";
}
// End WifiAP