| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
//
|
|
|
5 |
// Shows how to serve a static and dynamic template
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
#include <Arduino.h>
|
|
|
9 |
#if defined(ESP32) || defined(LIBRETINY)
|
|
|
10 |
#include <AsyncTCP.h>
|
|
|
11 |
#include <WiFi.h>
|
|
|
12 |
#elif defined(ESP8266)
|
|
|
13 |
#include <ESP8266WiFi.h>
|
|
|
14 |
#include <ESPAsyncTCP.h>
|
|
|
15 |
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
|
|
|
16 |
#include <RPAsyncTCP.h>
|
|
|
17 |
#include <WiFi.h>
|
|
|
18 |
#endif
|
|
|
19 |
|
|
|
20 |
#include <ESPAsyncWebServer.h>
|
|
|
21 |
#include <LittleFS.h>
|
|
|
22 |
|
|
|
23 |
static AsyncWebServer server(80);
|
|
|
24 |
|
|
|
25 |
static const char *htmlContent PROGMEM = R"(
|
|
|
26 |
<!DOCTYPE html>
|
|
|
27 |
<html>
|
|
|
28 |
<body>
|
|
|
29 |
<h1>Hello, %USER%</h1>
|
|
|
30 |
</body>
|
|
|
31 |
</html>
|
|
|
32 |
)";
|
|
|
33 |
|
|
|
34 |
static const size_t htmlContentLength = strlen_P(htmlContent);
|
|
|
35 |
|
|
|
36 |
// Variables used for dynamic cacheable template
|
|
|
37 |
static unsigned uptimeInMinutes = 0;
|
|
|
38 |
static AsyncStaticWebHandler *uptimeHandler = nullptr;
|
|
|
39 |
|
|
|
40 |
// Utility function for performing that update
|
|
|
41 |
static void setUptimeInMinutes(unsigned t) {
|
|
|
42 |
uptimeInMinutes = t;
|
|
|
43 |
// Update caching header with a new value as well
|
|
|
44 |
if (uptimeHandler) {
|
|
|
45 |
uptimeHandler->setLastModified();
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
void setup() {
|
|
|
50 |
Serial.begin(115200);
|
|
|
51 |
|
|
|
52 |
#if ASYNCWEBSERVER_WIFI_SUPPORTED
|
|
|
53 |
WiFi.mode(WIFI_AP);
|
|
|
54 |
WiFi.softAP("esp-captive");
|
|
|
55 |
#endif
|
|
|
56 |
|
|
|
57 |
#ifdef ESP32
|
|
|
58 |
LittleFS.begin(true);
|
|
|
59 |
#else
|
|
|
60 |
LittleFS.begin();
|
|
|
61 |
#endif
|
|
|
62 |
|
|
|
63 |
{
|
|
|
64 |
File f = LittleFS.open("/template.html", "w");
|
|
|
65 |
assert(f);
|
|
|
66 |
f.print(htmlContent);
|
|
|
67 |
f.close();
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Serve the static template file
|
|
|
71 |
//
|
|
|
72 |
// This call will have caching headers automatically added as it is a static file.
|
|
|
73 |
//
|
|
|
74 |
// curl -v http://192.168.4.1/template.html
|
|
|
75 |
server.serveStatic("/template.html", LittleFS, "/template.html");
|
|
|
76 |
|
|
|
77 |
// Serve a template with dynamic content
|
|
|
78 |
//
|
|
|
79 |
// serveStatic recognizes that template processing is in use, and will not automatically
|
|
|
80 |
// add caching headers.
|
|
|
81 |
//
|
|
|
82 |
// curl -v http://192.168.4.1/dynamic.html
|
|
|
83 |
server.serveStatic("/dynamic.html", LittleFS, "/template.html").setTemplateProcessor([](const String &var) -> String {
|
|
|
84 |
if (var == "USER") {
|
|
|
85 |
return String("Bob ") + millis();
|
|
|
86 |
}
|
|
|
87 |
return asyncsrv::emptyString;
|
|
|
88 |
});
|
|
|
89 |
|
|
|
90 |
// Serve a static template with a template processor
|
|
|
91 |
//
|
|
|
92 |
// By explicitly calling setLastModified() on the handler object, we enable
|
|
|
93 |
// sending the caching headers, even when a template is in use.
|
|
|
94 |
// This pattern should never be used with template data that can change.
|
|
|
95 |
// Example below: USER never changes.
|
|
|
96 |
//
|
|
|
97 |
// curl -v http://192.168.4.1/index.html
|
|
|
98 |
server.serveStatic("/index.html", LittleFS, "/template.html")
|
|
|
99 |
.setTemplateProcessor([](const String &var) -> String {
|
|
|
100 |
if (var == "USER") {
|
|
|
101 |
return "Bob";
|
|
|
102 |
}
|
|
|
103 |
return asyncsrv::emptyString;
|
|
|
104 |
})
|
|
|
105 |
.setLastModified("Sun, 28 Sep 2025 01:02:03 GMT");
|
|
|
106 |
|
|
|
107 |
// Serve a template with dynamic content *and* caching
|
|
|
108 |
//
|
|
|
109 |
// The data used in this template is updated in loop(). loop() is then responsible
|
|
|
110 |
// for calling setLastModified() on the handler object to notify any caches that
|
|
|
111 |
// the data has changed.
|
|
|
112 |
//
|
|
|
113 |
// curl -v http://192.168.4.1/uptime.html
|
|
|
114 |
uptimeHandler = &server.serveStatic("/uptime.html", LittleFS, "/template.html").setTemplateProcessor([](const String &var) -> String {
|
|
|
115 |
if (var == "USER") {
|
|
|
116 |
return String("Bob ") + uptimeInMinutes + " minutes";
|
|
|
117 |
}
|
|
|
118 |
return asyncsrv::emptyString;
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
// Serve a template with dynamic content based on user request
|
|
|
122 |
//
|
|
|
123 |
// In this case, the template is served via a callback request. Data from the request
|
|
|
124 |
// is used to generate the template callback.
|
|
|
125 |
//
|
|
|
126 |
// curl -v -G -d "USER=Bob" http://192.168.4.1/user_request.html
|
|
|
127 |
server.on("/user_request.html", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
|
128 |
request->send(LittleFS, "/template.html", "text/html", false, [=](const String &var) -> String {
|
|
|
129 |
if (var == "USER") {
|
|
|
130 |
const AsyncWebParameter *param = request->getParam("USER");
|
|
|
131 |
if (param) {
|
|
|
132 |
return param->value();
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
return asyncsrv::emptyString;
|
|
|
136 |
});
|
|
|
137 |
});
|
|
|
138 |
|
|
|
139 |
server.begin();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
// not needed
|
|
|
143 |
void loop() {
|
|
|
144 |
delay(100);
|
|
|
145 |
|
|
|
146 |
// Compute uptime
|
|
|
147 |
unsigned currentUptimeInMinutes = millis() / (60 * 1000);
|
|
|
148 |
|
|
|
149 |
if (currentUptimeInMinutes != uptimeInMinutes) {
|
|
|
150 |
setUptimeInMinutes(currentUptimeInMinutes);
|
|
|
151 |
}
|
|
|
152 |
}
|