This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
serveStatic.ino
191 lines (151 loc) · 6.4 KB
/
serveStatic.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
/****************************************************************************************************************************
serveStatic.ino - Dead simple web-server for Ethernet shields
demoing the usage of serveStatic class method
v. 1.0
Copyright (C) 2021 Sardar Azari
https://www.qrz.com/db/ac8l
Prior to the usage - use ESP8266/ESP32 Sketch data upoad tool to save the content of web-site to the SPIFFS flash memory.
This example sketch is based on HTML templates from Robert Ulbricht: https://www.arduinoslovakia.eu
https://github.com/RoboUlbricht/arduinoslovakia/tree/master/esp8266/simple_http_server_multiplepages_responsive_spiffs
This code's core is based on HelloServer.ino example of this library by Khoi Hoang
Some great examples of commercial use of serveStatic function can be seen and learned at uStepper's WiFiGUI project:
https://github.com/uStepper/uStepperSWiFiGUI
For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
Licensed under MIT license
*****************************************************************************************************************************/
/*****************************************************************************************************************************
How To Use:
1) Run the serveStaticLoadFile.ino
2) Upload the contents of a folder if you changedir in that folder and run the following command:
for file in `\ls -A1`; do curl -F "file=@$PWD/$file" localIPAddress/edit; done
3) access the sample web page at http://localIPAddress/
*****************************************************************************************************************************/
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
// 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 (_WIFIMGR_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 (_WIFIMGR_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
#include <WebServer_WT32_ETH01.h>
WebServer server(80);
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
void initFS()
{
// Initialize LittleFS/SPIFFS file-system
#if (ESP32)
// Format SPIFFS if not yet
if (!FileFS.begin(true))
{
Serial.println(F("SPIFFS/LittleFS failed! Formatting."));
#else
if (!FileFS.begin())
{
FileFS.format();
#endif
if (!FileFS.begin())
{
while (true)
{
#if USE_LITTLEFS
Serial.println(F("LittleFS failed!. Please use SPIFFS."));
#else
Serial.println(F("SPIFFS failed!. Please use LittleFS."));
#endif
// Stay forever here as useless to go further
delay(5000);
}
}
}
}
void initWebserver()
{
// Web Page handlers
server.serveStatic("/", FileFS, "/page1.html");
server.serveStatic("/page2", FileFS, "/page2.html");
server.serveStatic("/page3", FileFS, "/page3.html");
// Other usage with various web site assets:
// server.serveStatic("/assets/css/test.css", SPIFFS, "/assets/css/test.css"); // Style sheet
// server.serveStatic("/assets/js/test_script.js", SPIFFS,"/assets/js/test_script.js"); // Javascript
// server.serveStatic("/assets/font/fonticons.ttf", SPIFFS,"/assets/font/fonticons.ttf"); // font
// server.serveStatic("/assets/picture.png", SPIFFS,"/assets/picture.png"); // Picture
server.begin();
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Using this if Serial debugging is not necessary or not using Serial port
//while (!Serial && (millis() < 3000));
Serial.print("\nStarting serveStatic demoing 'serveStatic' function on " + String(ARDUINO_BOARD));
Serial.print(" using ");
Serial.println(FS_Name);
Serial.println("With " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
// 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);
// 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(myIP, myGW, mySN, myDNS);
WT32_ETH01_waitForConnect();
initFS();
initWebserver();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
}