First I've created variable for HTML code and pointer to it:
static const char ICACHE_FLASH_ATTR __html[] = "actual html code";
const __FlashStringHelper *html = reinterpret_cast<const __FlashStringHelper *>(&__html[0]);
Then I've created FlashStream class which implements functions needed by webServer's streamFile method(size, available and read):
#ifndef FlashStream_h
#define FlashStream_h
#include <pgmspace.h>
#include <Arduino.h>
class FlashStream
{
public:
FlashStream(const char *data);
size_t size();
size_t available();
size_t read(void *buffer, size_t count);
private:
const char* _data;
size_t _currentPosition;
size_t _size;
};
#endif
#include "FlashStream.h"
FlashStream::FlashStream(const char *data)
{
_data = data;
_currentPosition = 0;
_size = strlen_P(_data);
}
size_t FlashStream::size()
{
return _size;
}
size_t FlashStream::available()
{
return _size - _currentPosition;
}
size_t FlashStream::read(void *buffer, size_t count)
{
count = (count > _size - _currentPosition) ? _size - _currentPosition : count;
memcpy_P(buffer, _data + _currentPosition, count);
_currentPosition += count;
return count;
}
Finally, I've used it like this:
webServer.on ( "/", []() {
FlashStream htmlStream((PGM_P)html);
webServer.streamFile(htmlStream, "text/html");
} );
Works perfectly If anyone has the time, please look at the code and let me know what can be improved