mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-08-25 15:30:57 +02:00
StreamConcat example
This commit is contained in:
33
examples/StreamFiles/StreamConcat.h
Normal file
33
examples/StreamFiles/StreamConcat.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Stream.h>
|
||||||
|
|
||||||
|
class StreamConcat : public Stream {
|
||||||
|
public:
|
||||||
|
StreamConcat(Stream* s1, Stream* s2) : _s1(s1), _s2(s2) {}
|
||||||
|
|
||||||
|
size_t write(const uint8_t* p, size_t n) override { return 0; }
|
||||||
|
size_t write(uint8_t c) override { return 0; }
|
||||||
|
void flush() override {}
|
||||||
|
|
||||||
|
int available() override { return _s1->available() + _s2->available(); }
|
||||||
|
|
||||||
|
int read() override {
|
||||||
|
int c = _s1->read();
|
||||||
|
return c != -1 ? c : _s2->read();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t readBytes(char* buffer, size_t length) override {
|
||||||
|
size_t count = _s1->readBytes(buffer, length);
|
||||||
|
return count > 0 ? count : _s2->readBytes(buffer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int peek() override {
|
||||||
|
int c = _s1->peek();
|
||||||
|
return c != -1 ? c : _s2->peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Stream* _s1;
|
||||||
|
Stream* _s2;
|
||||||
|
};
|
73
examples/StreamFiles/StreamFiles.ino
Normal file
73
examples/StreamFiles/StreamFiles.ino
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <DNSServer.h>
|
||||||
|
#ifdef ESP32
|
||||||
|
#include <AsyncTCP.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#elif defined(ESP8266)
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESPAsyncTCP.h>
|
||||||
|
#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();
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
WiFi.softAP("esp-captive");
|
||||||
|
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||||
|
|
||||||
|
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;
|
||||||
|
content.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
|
||||||
|
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) {
|
||||||
|
Serial.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
|
||||||
|
last = millis();
|
||||||
|
}
|
||||||
|
}
|
36
examples/StreamFiles/StreamString.h
Normal file
36
examples/StreamFiles/StreamString.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Stream.h>
|
||||||
|
|
||||||
|
class StreamString : public Stream {
|
||||||
|
public:
|
||||||
|
size_t write(const uint8_t* p, size_t n) override { return _buffer.concat(reinterpret_cast<const char*>(p), n) ? n : 0; }
|
||||||
|
size_t write(uint8_t c) override { return _buffer.concat(static_cast<char>(c)) ? 1 : 0; }
|
||||||
|
void flush() override {}
|
||||||
|
|
||||||
|
int available() override { return static_cast<int>(_buffer.length()); }
|
||||||
|
|
||||||
|
int read() override {
|
||||||
|
if (_buffer.length() == 0)
|
||||||
|
return -1;
|
||||||
|
char c = _buffer[0];
|
||||||
|
_buffer.remove(0, 1);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t readBytes(char* buffer, size_t length) override {
|
||||||
|
if (length > _buffer.length())
|
||||||
|
length = _buffer.length();
|
||||||
|
// Don't use _str.ToCharArray() because it inserts a terminator
|
||||||
|
memcpy(buffer, _buffer.c_str(), length);
|
||||||
|
_buffer.remove(0, static_cast<unsigned int>(length));
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
int peek() override { return _buffer.length() > 0 ? _buffer[0] : -1; }
|
||||||
|
|
||||||
|
const String& buffer() const { return _buffer; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
String _buffer;
|
||||||
|
};
|
@@ -3,7 +3,7 @@ framework = arduino
|
|||||||
build_flags =
|
build_flags =
|
||||||
-Wall -Wextra
|
-Wall -Wextra
|
||||||
-D CONFIG_ARDUHAL_LOG_COLORS
|
-D CONFIG_ARDUHAL_LOG_COLORS
|
||||||
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
|
; -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
|
||||||
lib_deps =
|
lib_deps =
|
||||||
bblanchon/ArduinoJson @ 7.0.4
|
bblanchon/ArduinoJson @ 7.0.4
|
||||||
mathieucarbou/Async TCP @ ^3.1.2
|
mathieucarbou/Async TCP @ ^3.1.2
|
||||||
@@ -13,8 +13,9 @@ monitor_filters = esp32_exception_decoder, log2file
|
|||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
lib_dir = .
|
lib_dir = .
|
||||||
src_dir = examples/CaptivePortal
|
; src_dir = examples/CaptivePortal
|
||||||
; src_dir = examples/SimpleServer
|
; src_dir = examples/SimpleServer
|
||||||
|
src_dir = examples/StreamFiles
|
||||||
|
|
||||||
[env:v660]
|
[env:v660]
|
||||||
platform = espressif32@6.6.0
|
platform = espressif32@6.6.0
|
||||||
|
Reference in New Issue
Block a user