Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By knovoselic
#20499 So it wasn't as easy as I thought it would be. I thought I could just use the pointer, but ofc it doesn't work like that. Anyway, I've managed to make it work, like this:

First I've created variable for HTML code and pointer to it:
Code: Select allstatic 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):
Code: Select all#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

Code: Select all#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:

Code: Select all  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 :)
User avatar
By frippe75
#30871 Tried your code... without too much thought put into it. Didn't work for me.
Realize that there is no name function in your class. Any ideas?

In file included from AdvancedWebServer_edited.ino:33:0:
/home/frta/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-947-g39819f0/libraries/ESP8266WebServer/src/ESP8266WebServer.h: In instantiation of 'size_t ESP8266WebServer::streamFile(T&, const String&) [with T = FlashStream; size_t = unsigned int]':
AdvancedWebServer_edited.ino:161:46: required from here
/home/frta/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-947-g39819f0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:90:41: error: 'class FlashStream' has no member named 'name'
if (String(file.name()).endsWith(".gz") &&
User avatar
By knovoselic
#31154 ESP8266 SDK has changed, here's the updated code:

FlashStream.h
Code: Select all#ifndef FlashStream_h
#define FlashStream_h
#include <pgmspace.h>
#include <Arduino.h>

class FlashStream
{
  public:
    FlashStream(const char *data, const String& fileName);

    size_t size();
    size_t available();
    size_t read(void *buffer, size_t count);
    String name();

  private:
    const char* _data;
    size_t _currentPosition;
    size_t _size;
    String _fileName;
};
#endif


FlashStream.cpp
Code: Select all#include "FlashStream.h"

FlashStream::FlashStream(const char *data, const String& fileName)
{
  _data = data;
  _currentPosition = 0;
  _size = strlen_P(_data);
  _fileName = fileName;
}

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;
}

String FlashStream::name()
{
  return _fileName;
}


Example:
Code: Select all  FlashStream htmlStream((PGM_P)html, "index.html");
  webServer.streamFile(htmlStream, "text/html");