Serving images with webserver class
Posted: Thu Feb 25, 2016 4:30 pm
I've generated a html form from phpform.org, something with just a multiple choice box and a text input field which I want to serve form a esp8266.
To serve the generated form I've converted the text components with txt2c (https://sourceforge.net/projects/txt2c/) and converted the binary files (png & gif) with bin2hex.
I then included all these as header files, and added commands like server.on("blank.gif", handleBlankGif);
Where that function looks like
void handleBlankGif()
{
Serial.println("Handle Gif");
server->sendImage( 200, "image/gif", blank_gif, blank_gif_size);
}
Then had to add the follow two functions (and appropriate header) to ESP8266WebServer.cpp as the send would terminate early since the binary data has '\0' in it.
void ESP8266WebServer::sendImage(int code, const char* content_type, const char *image, int image_size) {
String header;
_prepareHeader(header, code, content_type, image_size);
sendContent(header);
sendContent(image, image_size);
}
void ESP8266WebServer::sendContent(const char *image, int image_size) {
const size_t unit_size = HTTP_DOWNLOAD_UNIT_SIZE;
size_t size_to_send = image_size;
const char* send_start = image;
while (size_to_send) {
size_t will_send = (size_to_send < unit_size) ? size_to_send : unit_size;
size_t sent = _currentClient.write(send_start, will_send);
if (sent == 0) {
break;
}
size_to_send -= sent;
send_start += sent;
}
}
This works but there must be a better way (and if the libraries get updated there goes my hack). Is there a way to upload the support files (.html, .css, .js, .png, etc) to the file system and serve directly form there?
Thanks,
Timothy
To serve the generated form I've converted the text components with txt2c (https://sourceforge.net/projects/txt2c/) and converted the binary files (png & gif) with bin2hex.
I then included all these as header files, and added commands like server.on("blank.gif", handleBlankGif);
Where that function looks like
void handleBlankGif()
{
Serial.println("Handle Gif");
server->sendImage( 200, "image/gif", blank_gif, blank_gif_size);
}
Then had to add the follow two functions (and appropriate header) to ESP8266WebServer.cpp as the send would terminate early since the binary data has '\0' in it.
void ESP8266WebServer::sendImage(int code, const char* content_type, const char *image, int image_size) {
String header;
_prepareHeader(header, code, content_type, image_size);
sendContent(header);
sendContent(image, image_size);
}
void ESP8266WebServer::sendContent(const char *image, int image_size) {
const size_t unit_size = HTTP_DOWNLOAD_UNIT_SIZE;
size_t size_to_send = image_size;
const char* send_start = image;
while (size_to_send) {
size_t will_send = (size_to_send < unit_size) ? size_to_send : unit_size;
size_t sent = _currentClient.write(send_start, will_send);
if (sent == 0) {
break;
}
size_to_send -= sent;
send_start += sent;
}
}
This works but there must be a better way (and if the libraries get updated there goes my hack). Is there a way to upload the support files (.html, .css, .js, .png, etc) to the file system and serve directly form there?
Thanks,
Timothy