This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Async_ESP32_FSWebServer.ino
819 lines (628 loc) · 23.4 KB
/
Async_ESP32_FSWebServer.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
/****************************************************************************************************************************
Async_ESP32_FSWebServer - Example WebServer with SPIFFS backend for ESP32
For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
AsyncWT32_ETH01_Manager is a library for the ESP32 with Ethernet LAN8720 to run Async Credential Manager
Modified from
1. Tzapu (https://github.com/tzapu/WiFiManager)
2. Ken Taylor (https://github.com/kentaylor)
3. Alan Steremberg (https://github.com/alanswx/ESPAsyncWiFiManager)
4. Khoi Hoang (https://github.com/khoih-prog/ESPAsync_WiFiManager)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWT32_ETH01_Manager
Licensed under MIT license
*****************************************************************************************************************************/
/*****************************************************************************************************************************
Compare this efficient Async_ESP32_FSWebServer_DRD example with the so complicated twin ESP32_FSWebServer
in (https://github.com/khoih-prog/ESP_WiFiManager) to appreciate the powerful AsynWebServer.
How To Upload Files:
1) Go to http://async-esp32fs.local/edit, then "Choose file" -> "Upload"
2) or Upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE)
3) or you can upload the contents of a folder if you CD in that folder and run the following command:
for file in `\ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
How To Use:
1) access the sample web page at http://async-esp32fs.local
2) edit the page by going to http://async-esp32fs.local/edit
3) Use configurable user/password to login. Default is admin/admin
*****************************************************************************************************************************/
#if !( defined(ESP32) )
#error This code is designed for WT32_ETH01 to run on ESP32 platform! Please check your Tools->Board setting.
#endif
//////////////////////////////////////////////////////////////
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ESPASYNC_ETH_MGR_LOGLEVEL_ 4
// To not display stored SSIDs and PWDs on Config Portal, select false. Default is true
// Even the stored Credentials are not display, just leave them all blank to reconnect and reuse the stored Credentials
//#define DISPLAY_STORED_CREDENTIALS_IN_CP false
//////////////////////////////////////////////////////////////
#include <ESPmDNS.h>
//////////////////////////////////////////////////////////////
// You only need to format the filesystem once
//#define FORMAT_FILESYSTEM true
#define FORMAT_FILESYSTEM false
//////////////////////////////////////////////////////////////
// LittleFS has higher priority than SPIFFS
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
#define USE_LITTLEFS true
#define USE_SPIFFS false
#elif defined(ARDUINO_ESP32C3_DEV)
// For core v1.0.6-, ESP32-C3 only supporting SPIFFS and EEPROM. To use v2.0.0+ for LittleFS
#define USE_LITTLEFS false
#define USE_SPIFFS true
#endif
#if USE_LITTLEFS
// Use LittleFS
#include "FS.h"
// Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h
//#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2)
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
#if (_ESPASYNC_ETH_MGR_LOGLEVEL_ > 3)
#warning Using ESP32 Core 1.0.6 or 2.0.0+
#endif
// The library has been merged into esp32 core from release 1.0.6
#include <LittleFS.h> // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS
FS* filesystem = &LittleFS;
#define FileFS LittleFS
#define FS_Name "LittleFS"
#else
#if (_ESPASYNC_ETH_MGR_LOGLEVEL_ > 3)
#warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library
#endif
// The library has been merged into esp32 core from release 1.0.6
#include <LITTLEFS.h> // https://github.com/lorol/LITTLEFS
FS* filesystem = &LITTLEFS;
#define FileFS LITTLEFS
#define FS_Name "LittleFS"
#endif
#elif USE_SPIFFS
#include <SPIFFS.h>
FS* filesystem = &SPIFFS;
#define FileFS SPIFFS
#define FS_Name "SPIFFS"
#else
// Use FFat
#include <FFat.h>
FS* filesystem = &FFat;
#define FileFS FFat
#define FS_Name "FFat"
#endif
#define LED_BUILTIN 2
#define LED_ON HIGH
#define LED_OFF LOW
#include <SPIFFSEditor.h>
//////////////////////////////////////////////////////////////
// Assuming max 49 chars
#define TZNAME_MAX_LEN 50
#define TIMEZONE_MAX_LEN 50
typedef struct
{
char TZ_Name[TZNAME_MAX_LEN]; // "America/Toronto"
char TZ[TIMEZONE_MAX_LEN]; // "EST5EDT,M3.2.0,M11.1.0"
uint16_t checksum;
} EthConfig;
EthConfig Ethconfig;
//////////////////////////////////////////////////////////////
#define CONFIG_FILENAME F("/eth_cred.dat")
//////////////////////////////////////////////////////////////
// Indicates whether ESP has credentials saved from previous session, or double reset detected
bool initialConfig = false;
// Use false if you don't like to display Available Pages in Information Page of Config Portal
// Comment out or use true to display Available Pages in Information Page of Config Portal
// Must be placed before #include <AsyncWT32_ETH01_Manager.h>
#define USE_AVAILABLE_PAGES false
// From v1.0.10 to permit disable/enable StaticIP configuration in Config Portal from sketch. Valid only if DHCP is used.
// You'll loose the feature of dynamically changing from DHCP to static IP, or vice versa
// You have to explicitly specify false to disable the feature.
//#define USE_STATIC_IP_CONFIG_IN_CP false
// Use false to disable NTP config. Advisable when using Cellphone, Tablet to access Config Portal.
// See Issue 23: On Android phone ConfigPortal is unresponsive (https://github.com/khoih-prog/ESP_WiFiManager/issues/23)
#define USE_ESP_ETH_MANAGER_NTP false
// Just use enough to save memory. On ESP8266, can cause blank ConfigPortal screen
// if using too much memory
#define USING_AFRICA false
#define USING_AMERICA true
#define USING_ANTARCTICA false
#define USING_ASIA false
#define USING_ATLANTIC false
#define USING_AUSTRALIA false
#define USING_EUROPE false
#define USING_INDIAN false
#define USING_PACIFIC false
#define USING_ETC_GMT false
// Use true to enable CloudFlare NTP service. System can hang if you don't have Internet access while accessing CloudFlare
// See Issue #21: CloudFlare link in the default portal (https://github.com/khoih-prog/ESP_WiFiManager/issues/21)
#define USE_CLOUDFLARE_NTP false
#define USING_CORS_FEATURE true
//////////////////////////////////////////////////////////////
// Use USE_DHCP_IP == true for dynamic DHCP IP, false to use static IP which you have to change accordingly to your network
#if (defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP)
// Force DHCP to be true
#if defined(USE_DHCP_IP)
#undef USE_DHCP_IP
#endif
#define USE_DHCP_IP true
#else
// You can select DHCP or Static IP here
//#define USE_DHCP_IP true
#define USE_DHCP_IP false
#endif
#if ( USE_DHCP_IP )
// Use DHCP
#if (_ESPASYNC_ETH_MGR_LOGLEVEL_ > 3)
#warning Using DHCP IP
#endif
IPAddress stationIP = IPAddress(0, 0, 0, 0);
IPAddress gatewayIP = IPAddress(192, 168, 2, 1);
IPAddress netMask = IPAddress(255, 255, 255, 0);
#else
// Use static IP
#if (_ESPASYNC_ETH_MGR_LOGLEVEL_ > 3)
#warning Using static IP
#endif
IPAddress stationIP = IPAddress(192, 168, 2, 232);
IPAddress gatewayIP = IPAddress(192, 168, 2, 1);
IPAddress netMask = IPAddress(255, 255, 255, 0);
#endif
//////////////////////////////////////////////////////////////
#define USE_CONFIGURABLE_DNS true
IPAddress dns1IP = gatewayIP;
IPAddress dns2IP = IPAddress(8, 8, 8, 8);
//////////////////////////////////////////////////////////////
// To eliminate WebServer.h conflicts when using AsyncWebServer
#define WEBSERVER_H
#include <AsyncWT32_ETH01_Manager.h> //https://github.com/khoih-prog/AsyncWT32_ETH01_Manager
String host = "async-esp32fs";
#define HTTP_PORT 80
AsyncWebServer server(HTTP_PORT);
//AsyncDNSServer dnsServer;
AsyncEventSource events("/events");
String http_username = "admin";
String http_password = "admin";
String separatorLine = "===============================================================";
//////////////////////////////////////////////////////////////
/******************************************
// Defined in AsyncWT32_ETH01_Manager.hpp
typedef struct
{
IPAddress _sta_static_ip;
IPAddress _sta_static_gw;
IPAddress _sta_static_sn;
#if USE_CONFIGURABLE_DNS
IPAddress _sta_static_dns1;
IPAddress _sta_static_dns2;
#endif
} ETH_STA_IPConfig;
******************************************/
ETH_STA_IPConfig EthSTA_IPconfig;
//////////////////////////////////////////////////////////////
void initSTAIPConfigStruct(ETH_STA_IPConfig &in_EthSTA_IPconfig)
{
in_EthSTA_IPconfig._sta_static_ip = stationIP;
in_EthSTA_IPconfig._sta_static_gw = gatewayIP;
in_EthSTA_IPconfig._sta_static_sn = netMask;
#if USE_CONFIGURABLE_DNS
in_EthSTA_IPconfig._sta_static_dns1 = dns1IP;
in_EthSTA_IPconfig._sta_static_dns2 = dns2IP;
#endif
}
//////////////////////////////////////////////////////////////
void displayIPConfigStruct(ETH_STA_IPConfig in_EthSTA_IPconfig)
{
LOGERROR3(F("stationIP ="), in_EthSTA_IPconfig._sta_static_ip, ", gatewayIP =", in_EthSTA_IPconfig._sta_static_gw);
LOGERROR1(F("netMask ="), in_EthSTA_IPconfig._sta_static_sn);
#if USE_CONFIGURABLE_DNS
LOGERROR3(F("dns1IP ="), in_EthSTA_IPconfig._sta_static_dns1, ", dns2IP =", in_EthSTA_IPconfig._sta_static_dns2);
#endif
}
//////////////////////////////////////////////////////////////
//format bytes
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";
}
}
//////////////////////////////////////////////////////////////
void toggleLED()
{
//toggle state
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
//////////////////////////////////////////////////////////////
#if USE_ESP_ETH_MANAGER_NTP
void printLocalTime()
{
struct tm timeinfo;
getLocalTime( &timeinfo );
// Valid only if year > 2000.
// You can get from timeinfo : tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec
if (timeinfo.tm_year > 100 )
{
Serial.print("Local Date/Time: ");
Serial.print( asctime( &timeinfo ) );
}
}
#endif
//////////////////////////////////////////////////////////////
void heartBeatPrint()
{
#if USE_ESP_ETH_MANAGER_NTP
printLocalTime();
#else
static int num = 1;
if (WT32_ETH01_isConnected())
Serial.print(F("H")); // H means connected to Ethernet
else
Serial.print(F("F")); // F means not connected to Ethernet
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
#endif
}
//////////////////////////////////////////////////////////////
void check_status()
{
static ulong checkstatus_timeout = 0;
static ulong LEDstatus_timeout = 0;
static ulong current_millis;
#if USE_ESP_ETH_MANAGER_NTP
#define HEARTBEAT_INTERVAL 60000L
#else
#define HEARTBEAT_INTERVAL 10000L
#endif
#define LED_INTERVAL 2000L
current_millis = millis();
if ((current_millis > LEDstatus_timeout) || (LEDstatus_timeout == 0))
{
// Toggle LED at LED_INTERVAL = 2s
toggleLED();
LEDstatus_timeout = current_millis + LED_INTERVAL;
}
// Print hearbeat every HEARTBEAT_INTERVAL (10) seconds.
if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = current_millis + HEARTBEAT_INTERVAL;
}
}
//////////////////////////////////////////////////////////////
int calcChecksum(uint8_t* address, uint16_t sizeToCalc)
{
uint16_t checkSum = 0;
for (uint16_t index = 0; index < sizeToCalc; index++)
{
checkSum += * ( ( (byte*) address ) + index);
}
return checkSum;
}
//////////////////////////////////////////////////////////////
bool loadConfigData()
{
File file = FileFS.open(CONFIG_FILENAME, "r");
LOGERROR(F("LoadCfgFile "));
memset((void *) &Ethconfig, 0, sizeof(Ethconfig));
memset((void *) &EthSTA_IPconfig, 0, sizeof(EthSTA_IPconfig));
if (file)
{
file.readBytes((char *) &Ethconfig, sizeof(Ethconfig));
file.readBytes((char *) &EthSTA_IPconfig, sizeof(EthSTA_IPconfig));
file.close();
LOGERROR(F("OK"));
if ( Ethconfig.checksum != calcChecksum( (uint8_t*) &Ethconfig, sizeof(Ethconfig) - sizeof(Ethconfig.checksum) ) )
{
LOGERROR(F("Ethconfig checksum wrong"));
return false;
}
displayIPConfigStruct(EthSTA_IPconfig);
return true;
}
else
{
LOGERROR(F("failed"));
return false;
}
}
//////////////////////////////////////////////////////////////
void saveConfigData()
{
File file = FileFS.open(CONFIG_FILENAME, "w");
LOGERROR(F("SaveCfgFile "));
if (file)
{
Ethconfig.checksum = calcChecksum( (uint8_t*) &Ethconfig, sizeof(Ethconfig) - sizeof(Ethconfig.checksum) );
file.write((uint8_t*) &Ethconfig, sizeof(Ethconfig));
displayIPConfigStruct(EthSTA_IPconfig);
file.write((uint8_t*) &EthSTA_IPconfig, sizeof(EthSTA_IPconfig));
file.close();
LOGERROR(F("OK"));
}
else
{
LOGERROR(F("failed"));
}
}
//////////////////////////////////////////////////////////////
void beginEthernet()
{
// To be called before ETH.begin()
WT32_ETH01_onEvent();
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
}
void initEthernet()
{
#if !( USE_DHCP_IP )
displayIPConfigStruct(EthSTA_IPconfig);
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(stationIP, gatewayIP, netMask, dns1IP, dns2IP);
ETH.config(EthSTA_IPconfig._sta_static_ip, EthSTA_IPconfig._sta_static_gw, EthSTA_IPconfig._sta_static_sn,
EthSTA_IPconfig._sta_static_dns1);
#endif
WT32_ETH01_waitForConnect();
}
//////////////////////////////////////////////////////////////
void setup()
{
//set led pin as output
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print(F("\nStarting Async_ESP32_FSWebServer using "));
Serial.print(FS_Name);
Serial.print(F(" on "));
Serial.print(ARDUINO_BOARD);
Serial.print(F(" with "));
Serial.println(SHIELD_TYPE);
Serial.println(ASYNC_WT32_ETH01_MANAGER_VERSION);
Serial.setDebugOutput(false);
if (FORMAT_FILESYSTEM)
FileFS.format();
// Format FileFS if not yet
if (!FileFS.begin(true))
{
Serial.println(F("SPIFFS/LittleFS failed! Already tried formatting."));
if (!FileFS.begin())
{
// prevents debug info from the library to hide err message.
delay(100);
#if USE_LITTLEFS
Serial.println(F("LittleFS failed!. Please use SPIFFS or EEPROM. Stay forever"));
#else
Serial.println(F("SPIFFS failed!. Please use LittleFS or EEPROM. Stay forever"));
#endif
while (true)
{
delay(1);
}
}
}
File root = FileFS.open("/");
File file = root.openNextFile();
while (file)
{
String fileName = file.name();
size_t fileSize = file.size();
Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
file = root.openNextFile();
}
Serial.println();
unsigned long startedAt = millis();
beginEthernet();
initSTAIPConfigStruct(EthSTA_IPconfig);
digitalWrite(LED_BUILTIN, LED_ON);
//Local intialization. Once its business is done, there is no need to keep it around
// Use this to default DHCP hostname to ESP32-XXXXXX
//AsyncWT32_ETH01_Manager AsyncWT32_ETH01_manager(&webServer, &dnsServer);
// Use this to personalize DHCP hostname (RFC952 conformed)
#if ( USING_ESP32_S2 || USING_ESP32_C3 )
AsyncWT32_ETH01_Manager AsyncWT32_ETH01_manager(&server, NULL, "AsyncESP32-FSWebServer");
#else
AsyncDNSServer dnsServer;
AsyncWT32_ETH01_Manager AsyncWT32_ETH01_manager(&server, &dnsServer, "AsyncESP32-FSWebServer");
#endif
#if !USE_DHCP_IP
// Set (static IP, Gateway, Subnetmask, DNS1 and DNS2) or (IP, Gateway, Subnetmask)
AsyncWT32_ETH01_manager.setSTAStaticIPConfig(EthSTA_IPconfig);
#endif
#if USING_CORS_FEATURE
AsyncWT32_ETH01_manager.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
bool configDataLoaded = loadConfigData();
//////////////////////////////////
// Connect ETH now if using STA
initEthernet();
//////////////////////////////////
if (configDataLoaded)
{
#if USE_ESP_ETH_MANAGER_NTP
if ( strlen(Ethconfig.TZ_Name) > 0 )
{
LOGERROR3(F("Saving current TZ_Name ="), Ethconfig.TZ_Name, F(", TZ = "), Ethconfig.TZ);
//configTzTime(Ethconfig.TZ, "pool.ntp.org" );
configTzTime(Ethconfig.TZ, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
}
else
{
Serial.println(F("Current Timezone is not set. Enter Config Portal to set."));
}
#endif
}
else
{
{
Serial.println(F("We haven't got any access point credentials, so get them now"));
initialConfig = true;
Serial.print(F("Starting configuration portal @ "));
Serial.println(ETH.localIP());
//sets timeout in seconds until configuration portal gets turned off.
//If not specified device will remain in configuration mode until
//switched off via webserver or device is restarted.
//AsyncWT32_ETH01_manager.setConfigPortalTimeout(600);
// Starts an access point
if (!AsyncWT32_ETH01_manager.startConfigPortal())
Serial.println(F("Not connected to ETH network but continuing anyway."));
else
{
Serial.println(F("ETH network connected...yeey :)"));
}
#if USE_ESP_ETH_MANAGER_NTP
String tempTZ = AsyncWT32_ETH01_manager.getTimezoneName();
if (strlen(tempTZ.c_str()) < sizeof(Ethconfig.TZ_Name) - 1)
strcpy(Ethconfig.TZ_Name, tempTZ.c_str());
else
strncpy(Ethconfig.TZ_Name, tempTZ.c_str(), sizeof(Ethconfig.TZ_Name) - 1);
const char * TZ_Result = AsyncWT32_ETH01_manager.getTZ(Ethconfig.TZ_Name);
if (strlen(TZ_Result) < sizeof(Ethconfig.TZ) - 1)
strcpy(Ethconfig.TZ, TZ_Result);
else
strncpy(Ethconfig.TZ, TZ_Result, sizeof(Ethconfig.TZ_Name) - 1);
if ( strlen(Ethconfig.TZ_Name) > 0 )
{
LOGERROR3(F("Saving current TZ_Name ="), Ethconfig.TZ_Name, F(", TZ = "), Ethconfig.TZ);
//configTzTime(Ethconfig.TZ, "pool.ntp.org" );
configTzTime(Ethconfig.TZ, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
}
else
{
LOGERROR(F("Current Timezone Name is not set. Enter Config Portal to set."));
}
#endif
AsyncWT32_ETH01_manager.getSTAStaticIPConfig(EthSTA_IPconfig);
saveConfigData();
}
}
startedAt = millis();
Serial.print(F("After waiting "));
Serial.print((float) (millis() - startedAt) / 1000);
Serial.print(F(" secs more in setup(), connection result is "));
if (WT32_ETH01_isConnected())
{
Serial.print(F("connected. Local IP: "));
Serial.println(ETH.localIP());
}
if ( !MDNS.begin(host.c_str()) )
{
Serial.println(F("Error starting MDNS responder!"));
}
// Add service to MDNS-SD
MDNS.addService("http", "tcp", HTTP_PORT);
//SERVER INIT
events.onConnect([](AsyncEventSourceClient * client)
{
client->send("hello!", NULL, millis(), 1000);
});
server.addHandler(&events);
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest * request)
{
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server.addHandler(new SPIFFSEditor(FileFS, http_username, http_password));
server.serveStatic("/", FileFS, "/").setDefaultFile("index.htm");
server.onNotFound([](AsyncWebServerRequest * request)
{
Serial.print(F("NOT_FOUND: "));
if (request->method() == HTTP_GET)
Serial.print(F("GET"));
else if (request->method() == HTTP_POST)
Serial.print(F("POST"));
else if (request->method() == HTTP_DELETE)
Serial.print(F("DELETE"));
else if (request->method() == HTTP_PUT)
Serial.print(F("PUT"));
else if (request->method() == HTTP_PATCH)
Serial.print(F("PATCH"));
else if (request->method() == HTTP_HEAD)
Serial.print(F("HEAD"));
else if (request->method() == HTTP_OPTIONS)
Serial.print(F("OPTIONS"));
else
Serial.print(F("UNKNOWN"));
Serial.println(" http://" + request->host() + request->url());
if (request->contentLength())
{
Serial.println("_CONTENT_TYPE: " + request->contentType());
Serial.println("_CONTENT_LENGTH: " + request->contentLength());
}
int headers = request->headers();
int i;
for (i = 0; i < headers; i++)
{
AsyncWebHeader* h = request->getHeader(i);
Serial.println("_HEADER[" + h->name() + "]: " + h->value());
}
int params = request->params();
for (i = 0; i < params; i++)
{
AsyncWebParameter* p = request->getParam(i);
if (p->isFile())
{
Serial.println("_FILE[" + p->name() + "]: " + p->value() + ", size: " + p->size());
}
else if (p->isPost())
{
Serial.println("_POST[" + p->name() + "]: " + p->value());
}
else
{
Serial.println("_GET[" + p->name() + "]: " + p->value());
}
}
request->send(404);
});
server.onFileUpload([](AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t *data,
size_t len, bool final)
{
(void) request;
if (!index)
Serial.println("UploadStart: " + filename);
Serial.print((const char*)data);
if (final)
Serial.println("UploadEnd: " + filename + "(" + String(index + len) + ")" );
});
server.onRequestBody([](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total)
{
(void) request;
if (!index)
Serial.println("BodyStart: " + total);
Serial.print((const char*)data);
if (index + len == total)
Serial.println("BodyEnd: " + total);
});
server.begin();
//////////////////////////////////////////////////////////////
Serial.print(F("HTTP server started @ "));
Serial.println(ETH.localIP());
Serial.println(separatorLine);
Serial.print("Open http://");
Serial.print(ETH.localIP());
Serial.println("/edit to see the file browser");
Serial.println("Using username = " + http_username + " and password = " + http_password);
Serial.println(separatorLine);
digitalWrite(LED_BUILTIN, LED_OFF);
}
//////////////////////////////////////////////////////////////
void loop()
{
check_status();
}