esp32 server favicon

by referring this good thread

https://forum.arduino.cc/t/webserver-serving-an-image-in-code/1010809/3

I was able to make image/png favicon working.

however, I found that if I use binary array instead of using base64 converting, whole favicon.ico can be serviced.


favicon.ico is actually bigger than esp32 free SRAM, that was the reason of crash, author also mentioned about the safe size.

I realized that if I convert favicon.ico file to the binary using const char [] array, it will be loaded to flash only and does not spend SRAM.


I used below python script for converting favicon.ico file to the const char [] array.

------------------------------------------------

if __name__ == "__main__":
    file_path = "favicon.ico"
    with open(file_path, "rb") as file:
        bytes_data = file.read()
        file.close()
    str = "const char favicon_binary[] = {"
    for byte in bytes_data:
        str += f"0x{byte:02X}, "
    str += "};"        

    with open("favicon_const.h", "w") as file:
        file.write(str)

------------------------------------------------    

below is the relevant code only.


#include <ESPAsyncWebServer.h>

void handleFavicon(AsyncWebServerRequest *request)
{
  int binaryLen = sizeof(favicon_binary);  

  String type = "image/x-icon";

  AsyncResponseStream *response = request->beginResponseStream(type, binaryLen);

    for (size_t i = 0; i < binaryLen; i++) {
      response->write(pgm_read_byte(favicon_binary + i));
    }

  request->send(response);
}


server.on("/favicon.ico", HTTP_GET, handleFavicon);


댓글

이 블로그의 인기 게시물

공유기 2개 쓰는데 동일 네트워크 구성하기 ASUS 공유기 + iptime 공유기, LAN, WIFI 연결

OneDrive가 바꾼 문서, 사진, 바탕화면 기본 폴더 복구하기(레지스트리 사용안하고 복구)

갤럭시북 이온 노트북 소음 잡기