ESPAsyncWebServer/examples/StreamFiles/StreamFiles.ino

87 lines
2.1 KiB
Arduino
Raw Permalink Normal View History

2024-05-30 08:41:06 +02:00
#include <Arduino.h>
#include <DNSServer.h>
#ifdef ESP32
2024-07-02 20:06:33 +02:00
#include <AsyncTCP.h>
#include <WiFi.h>
2024-05-30 08:41:06 +02:00
#elif defined(ESP8266)
2024-07-02 20:06:33 +02:00
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(TARGET_RP2040)
#include <WebServer.h>
#include <WiFi.h>
2024-05-30 08:41:06 +02:00
#endif
#include "StreamConcat.h"
#include "StreamString.h"
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
DNSServer dnsServer;
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
LittleFS.begin();
2024-09-02 06:59:11 +02:00
#ifndef CONFIG_IDF_TARGET_ESP32H2
2024-05-30 08:41:06 +02:00
WiFi.mode(WIFI_AP);
WiFi.softAP("esp-captive");
2024-09-02 06:59:11 +02:00
2024-05-30 08:41:06 +02:00
dnsServer.start(53, "*", WiFi.softAPIP());
2024-09-02 06:59:11 +02:00
#endif
2024-05-30 08:41:06 +02:00
File file1 = LittleFS.open("/header.html", "w");
file1.print("<html><head><title>ESP Captive Portal</title><meta http-equiv=\"refresh\" content=\"1\"></head><body>");
file1.close();
File file2 = LittleFS.open("/body.html", "w");
file2.print("<h1>Welcome to ESP Captive Portal</h1>");
file2.close();
File file3 = LittleFS.open("/footer.html", "w");
file3.print("</body></html>");
file3.close();
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
File header = LittleFS.open("/header.html", "r");
File body = LittleFS.open("/body.html", "r");
StreamConcat stream1(&header, &body);
StreamString content;
2024-07-02 20:06:33 +02:00
#if defined(TARGET_RP2040)
content.printf("FreeHeap: %d", rp2040.getFreeHeap());
#else
2024-05-30 08:41:06 +02:00
content.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
2024-07-02 20:06:33 +02:00
#endif
2024-05-30 08:41:06 +02:00
StreamConcat stream2 = StreamConcat(&stream1, &content);
File footer = LittleFS.open("/footer.html", "r");
StreamConcat stream3 = StreamConcat(&stream2, &footer);
request->send(stream3, "text/html", stream3.available());
header.close();
body.close();
footer.close();
});
server.onNotFound([](AsyncWebServerRequest* request) {
request->send(404, "text/plain", "Not found");
});
server.begin();
}
uint32_t last = 0;
void loop() {
// dnsServer.processNextRequest();
if (millis() - last > 2000) {
2024-07-02 20:06:33 +02:00
#if defined(TARGET_RP2040)
Serial.printf("FreeHeap: %d", rp2040.getFreeHeap());
#else
2024-05-30 08:41:06 +02:00
Serial.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
2024-07-02 20:06:33 +02:00
#endif
2024-05-30 08:41:06 +02:00
last = millis();
}
}