22 Web server test program
33*/
44
5+ // #define NO_LIBRARY 1
6+ // #define INDEX_PAGE 1
7+ // #define NOT_FOUND_PAGE 1
8+ // #define MARK_BOUNDARY 1
9+
510#include < AsyncTCP.h>
611#include < ESPAsyncWebServer.h>
12+ #ifndef NO_LIBRARY
13+ #include < SdCardServer.h>
14+ #endif // NO_LIBRARY
715#include < SPI.h>
816#include < pgmspace.h>
917#include < WiFi.h>
@@ -17,8 +25,6 @@ int keyIndex = 0;
1725char password[1024 ]; // WiFi network password
1826char ssid[1024 ]; // WiFi network name
1927
20- #define MAX_FILE_NAME_SIZE (256 * 3 )
21-
2228const int pin_microSD_CS = 25 ;
2329
2430typedef struct struct_settings {
@@ -35,11 +41,9 @@ const TickType_t fatSemaphore_longWait_ms = 200 / portTICK_PERIOD_MS;
3541char htmlBuffer[256 ];
3642Online online;
3743SdFat sd;
38- int sdCardEmpty;
3944int sdCardMounted;
4045float sdCardSizeMB;
4146SdFile sdFile;
42- int sdListingState;
4347SdFile sdRootDir;
4448AsyncWebServer server (80 );
4549Settings settings;
@@ -48,20 +52,24 @@ int wifiBeginCalled;
4852int wifiConnected;
4953SemaphoreHandle_t xFATSemaphore;
5054
55+ #ifdef NO_LIBRARY
56+ #define MAX_FILE_NAME_SIZE (256 * 3 )
57+
58+ int sdCardEmpty;
59+ int sdListingState;
60+
5161enum {
5262 LS_HEADER = 0 ,
5363 LS_DISPLAY_FILES,
5464 LS_TRAILER,
5565 LS_DONE
5666} LISTING_STATE;
67+ #endif // NO_LIBRARY
5768
5869// html document
5970// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
6071
61- prog_char htmlHeaderStart[] PROGMEM = R"rawliteral( <!DOCTYPE HTML>
62- <html>
63- <head>
64- <title>)rawliteral" ;
72+ prog_char htmlHeaderStart[] PROGMEM = " <!DOCTYPE HTML>\n <html lang=\" en\" >\n <head>\n <meta http-equiv=\" Content-Type\" content=\" text/html; charset=utf-8\" />\n <title>" ;
6573
6674prog_char htmlHeaderEndBodyStart[] PROGMEM = R"rawliteral( </title>
6775</head>
@@ -92,10 +100,10 @@ prog_char htmlListItemStart[] PROGMEM = R"rawliteral( <li>)rawliteral";
92100prog_char htmlListItemEnd[] PROGMEM = R"rawliteral( </li>
93101)rawliteral" ;
94102
95- prog_char htmlUlListStart[] PROGMEM = R"rawliteral( <ul >
103+ prog_char htmlUlListStart[] PROGMEM = R"rawliteral( <ol >
96104)rawliteral" ;
97105
98- prog_char htmlUlListEnd[] PROGMEM = R"rawliteral( </ul >
106+ prog_char htmlUlListEnd[] PROGMEM = R"rawliteral( </ol >
99107)rawliteral" ;
100108
101109// sd/
@@ -120,11 +128,19 @@ prog_char sdDirectory[] PROGMEM = "/sd/";
120128// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
121129const char htmlTitle[] PROGMEM = " SD Card Server" ;
122130
131+ #ifdef NO_LIBRARY
123132const char index_html[] PROGMEM = R"rawliteral( %H%%T%%/HB%
124133 <h1>%T%</h1>
125134 <p>%A%%SD%%Q%>%H1%</a></p>
126135%/B%
127136)rawliteral" ;
137+ #else // NO_LIBRARY
138+ const char index_html[] PROGMEM = R"rawliteral( %H%%T%%/HB%
139+ <h1>%T%</h1>
140+ <p>%SD%</p>
141+ %/B%
142+ )rawliteral" ;
143+ #endif // NO_LIBRARY
128144
129145const char no_sd_card_html[] PROGMEM = R"rawliteral( %H%%T%%/HB%
130146 <h1>%T%</h1>
@@ -139,6 +155,8 @@ const char not_formatted_html[] PROGMEM = R"rawliteral(%H%%T%%/HB%
139155)rawliteral" ;
140156
141157// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
158+
159+ #ifdef NO_LIBRARY
142160class HtmlPrint : public Print
143161{
144162 uint8_t * buf;
@@ -165,6 +183,7 @@ public:
165183 return size;
166184 }
167185};
186+ #endif // NO_LIBRARY
168187
169188// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
170189void beginSD ()
@@ -376,7 +395,13 @@ int mountSdCard(void) {
376395 return sdCardMounted;
377396}
378397
398+ #ifndef NO_LIBRARY
399+ SdCardServer sdCardServer (&sd, mountSdCard, " /sd/" , " SD Card Files" );
400+ #endif // NO_LIBRARY
401+
379402// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
403+ #ifdef INDEX_PAGE
404+
380405String processor (const String& var) {
381406 if (var == " A" ) {
382407 strcpy_P (htmlBuffer, htmlAnchorStart);
@@ -403,8 +428,15 @@ String processor(const String& var) {
403428 return String (htmlListItemStart);
404429 if (var == " /LI" )
405430 return String (htmlListItemEnd);
431+ #ifdef NO_LIBRARY
406432 if (var == " SD" )
407433 return String (sdDirectory);
434+ #else // NO_LIBRARY
435+ if (var == " SD" ) {
436+ sdCardServer.sdCardListingWebPageLink ((char *)&htmlBuffer[0 ], sizeof (htmlBuffer), " SD Card Files" , " target=\" _blank\" " );
437+ return String (htmlBuffer);
438+ }
439+ #endif // NO_LIBRARY
408440 if (var == " SZ" ) {
409441 sprintf (htmlBuffer, " %3.0f %s" ,
410442 sdCardSizeMB < 1000 . ? sdCardSizeMB : sdCardSizeMB / 1000 .,
@@ -428,7 +460,11 @@ indexHtml (AsyncWebServerRequest * request) {
428460 request->send_P (200 , " text/html" , mountSdCard () ? index_html : no_sd_card_html, processor);
429461}
430462
463+ #endif // INDEX_PAGE
464+
431465// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
466+
467+ #ifdef NO_LIBRARY
432468void
433469buildHtmlAnchor (HtmlPrint * htmlPrint, uint8_t *buffer) {
434470 uint64_t u64 ;
@@ -492,6 +528,8 @@ int addFileName(HtmlPrint * htmlPrint, uint8_t * buffer) {
492528 return strlen ((char *)buffer);
493529
494530 case LS_TRAILER:
531+ if (!sdCardEmpty)
532+ strcat_P ((char *)buffer, htmlUlListEnd);
495533 strcat_P ((char *)buffer, htmlBodyEnd);
496534 sdListingState = LS_DONE;
497535 return strlen ((char *)buffer);
@@ -513,8 +551,17 @@ int sdCardListing(uint8_t * buffer, size_t maxLen) {
513551 do {
514552 // Determine if the buffer is full enough
515553 bytesWritten = buffer - bufferStart;
516- if (maxLen < MAX_FILE_NAME_SIZE)
554+ if (maxLen < MAX_FILE_NAME_SIZE) {
555+ #ifdef MARK_BOUNDARY
556+ if (bytesWritten) {
557+ Serial.println (" Next buffer" );
558+ strcat ((char *)buffer, " --------------------<br>\n " );
559+ buffer += strlen ((char *)buffer);
560+ bytesWritten = buffer - bufferStart;
561+ }
562+ #endif // MARK_BOUNDARY
517563 break ;
564+ }
518565
519566 // Add the next file name
520567 *buffer = 0 ;
@@ -543,6 +590,7 @@ void sdDirectoryListing (AsyncWebServerRequest *request) {
543590 request->send (404 );
544591 else {
545592 // Open the root directory
593+ sdCardEmpty = 1 ;
546594 sdRootDir = SdFile ();
547595 if (!sdRootDir.openRoot (sd.vol ())) {
548596 // Invalid SD card format
@@ -580,9 +628,18 @@ int returnFile(uint8_t * buffer, size_t maxLen) {
580628 // Return the number of bytes read
581629 return bytesRead;
582630}
631+ #endif // NO_LIBRARY
583632
584633// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
634+ #ifdef NOT_FOUND_PAGE
635+
585636void httpRequestNotFound (AsyncWebServerRequest *request) {
637+ #ifndef NO_LIBRARY
638+ // Display the SD card page if necessary
639+ if (sdCardServer.isSdCardWebPage (request))
640+ return ;
641+ #else // NO_LIBRARY
642+
586643 int dirLength;
587644 const char * filename;
588645 const char * url;
@@ -626,10 +683,12 @@ void httpRequestNotFound(AsyncWebServerRequest *request) {
626683 request->send (response);
627684 return ;
628685 } while (0 );
686+ #endif // NO_LIBRARY
629687
630688 // URL not found
631689 request->send (404 );
632690}
691+ #endif // NOT_FOUND_PAGE
633692
634693// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
635694void setup () {
@@ -717,19 +776,29 @@ void loop() {
717776 displayWiFiGatewayIp ();
718777
719778 // index.html
779+ #ifdef INDEX_PAGE
720780 server.on (" /" , HTTP_GET, [](AsyncWebServerRequest *request) {
721781 indexHtml (request);
722782 });
783+ #else // INDEX_PAGE
784+ sdCardServer.sdCardWebSite (&server);
785+ #endif // INDEX_PAGE
723786
787+ #ifdef NO_LIBRARY
724788 // /sd/
725789 server.on (" /sd/" , HTTP_GET, [](AsyncWebServerRequest *request) {
726790 sdDirectoryListing (request);
727791 });
792+ #endif // NO_LIBRARY
728793
729794 // All other pages
795+ #ifdef NOT_FOUND_PAGE
730796 server.onNotFound ([](AsyncWebServerRequest *request) {
731797 httpRequestNotFound (request);
732798 });
799+ #else // NOT_FOUND_PAGE
800+ sdCardServer.onNotFound (&server);
801+ #endif // NOT_FOUND_PAGE
733802
734803 // Start server
735804 server.begin ();
0 commit comments