Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently using a Nodemcu ESP8266-12E with the Arduino IDE in order to control my boiler. Everything is working fine, however I haven't figured out yet how to serve binary files, eg. images. (I am hosting them on a server at the moment).

ESP8266WebServer server(80);

void setup()
{
  [...]
  server.on("/", handleResponse);
  server.onNotFound(handleResponse);
  server.begin();
}

void handleResponse() {
  if (server.uri() == "/style.css") {
    server.send(200, "text/css", "[css]");
    return;
  }
  if (server.uri() == "/favicon.ico") {
    server.send(404, "text/html", "Not set");
    return;
  }
  [...]
}

unsigned char favicon_ico[] = {
  0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x10, 0x00, 0x00, 0x01, 0x00,
  0x18, 0x00, 0x68, 0x03, 0x00,......... 0x00
};
unsigned int favicon_ico_len = 894;

Converting the arary to a string and serving it with server.send does not work and server.stream expects a file.

share|improve this question
up vote 3 down vote accepted

It looks like you can use the send_P function to send raw PROGMEM data:

void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);

I.e., (from what I can gather):

PGM_P favicon = {
  0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x10, 0x00, 0x00, 0x01, 0x00,
  0x18, 0x00, 0x68, 0x03, 0x00,......... 0x00
};

server.send_P(200, "image/x-icon", favicon, sizeof(favicon));

Incidentally, PGM_P is just a typedef for const char *.

share|improve this answer
    
Thank you very much, after changing unsigned char favicon_ico to const char favicon_ico it worked with server.send_P()! Setting it to PGM_P somehow threw an error. – Force yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.