1

I'm not good at English, so I used a translator.

Please understand that we cannot show all the code.

Currently, I am trying to open AP mode in ESP32 and send HTML information to the connected person.

However, I am trying to send chart.min.js or chart.min.js.gz.

The request was clearly received by esp32, I can't send you data.

What should I do?

idf_component_register(SRCS "main.cpp"
                     INCLUDE_DIRS "."
                     EMBED_TXTFILES "html_data/index.html"
                                 "html_data/style.css"
                                 "html_data/chart.min.js"
                                 "html_data/chart.min.js.gz"
                                 "html_data/script.js"
                                 "html_data/favicon.ico"
                                 "html_data/images/heartbeat.png"
                                 "html_data/images/lungs.png"
                               )
#include "_gateway_demo.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>

#include "esp_wifi.h"
#include "esp_chip_info.h"
#include "esp_mac.h"
#include "nvs_flash.h"
#include "FFat.h"
#include <ESPAsyncWebServer.h>
#include "ArduinoJson.h"
#include "esp_timer.h"

std::atomic<bool> running_{false};
demo_config_t demo_config;
AsyncWebServer httpServer = AsyncWebServer(DEMO_WEB_SERVER_PORT);
AsyncWebSocket ws = AsyncWebSocket("/ws");

extern const uint8_t index_html_start[] asm("_binary_index_html_start");
extern const uint8_t index_html_end[] asm("_binary_index_html_end");
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
extern const uint8_t style_css_end[] asm("_binary_style_css_end");
extern const uint8_t script_js_start[] asm("_binary_script_js_start");
extern const uint8_t script_js_end[] asm("_binary_script_js_end");
extern const uint8_t chart_min_js_start[] asm("_binary_chart_min_js_start");
extern const uint8_t chart_min_js_end[] asm("_binary_chart_min_js_end");
extern const uint8_t chart_min_js_gz_start[] asm("_binary_chart_min_js_gz_start");
extern const uint8_t chart_min_js_gz_end[]   asm("_binary_chart_min_js_gz_end");
extern const uint8_t favicon_ico_start[] asm("_binary_favicon_ico_start");
extern const uint8_t favicon_ico_end[] asm("_binary_favicon_ico_end");
extern const uint8_t heartbeat_png_start[] asm("_binary_heartbeat_png_start");
extern const uint8_t heartbeat_png_end[] asm("_binary_heartbeat_png_end");
extern const uint8_t lungs_png_start[] asm("_binary_lungs_png_start");
extern const uint8_t lungs_png_end[] asm("_binary_lungs_png_end");

size_t index_html_size = index_html_end - index_html_start;
size_t style_css_size = style_css_end - style_css_start;
size_t chart_min_js_size = chart_min_js_end - chart_min_js_start;
size_t script_js_size = script_js_end - script_js_start;

int GatewayDemo::onCommand(char **argList, int argCount)
{
    // 명령어 처리 로직 (임시로 0 반환)
    return 0;
}

// 본 process 함수는 별도 스레드에서 주기적으로 호출됨.
int GatewayDemo::process(void *param)
{
    vTaskDelay(1);
    return 0;
}

// 비동기 웹서버 실행 및 웹소켓 서버 생성
int GatewayDemo::initializeWebServer()
{
    DEMO_LOG_INFO("Starting Web Server on port %d\r\n", DEMO_WEB_SERVER_PORT);

    // websocket event handler 등록
    ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
                  void *arg, uint8_t *data, size_t len)
               {
        if (type == WS_EVT_CONNECT) {
            DEMO_LOG_INFO("WebSocket client connected: %ld\r\n", client->id());
        } else if (type == WS_EVT_DISCONNECT) {
            DEMO_LOG_INFO("WebSocket client disconnected: %ld\r\n", client->id());
        } else if (type == WS_EVT_DATA) {
            // 데이터 수신
        } });

    httpServer.onNotFound([](AsyncWebServerRequest *request)
                          { request->send(404, "text/plain", "Not found"); });

    // index.html 및 기타 정적 파일 제공
    httpServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "text/html", index_html_start, index_html_size); });

    httpServer.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "text/css", style_css_start, style_css_size); });

    // httpServer.on("/chart.min.js", HTTP_GET, [](AsyncWebServerRequest *request)
    //               { request->send_P(200, "application/javascript", chart_min_js_start, chart_min_js_size); });

    httpServer.on("/chart.min.js", HTTP_GET, [](AsyncWebServerRequest *request)
                  {
        size_t chart_len = chart_min_js_gz_end - chart_min_js_gz_start;
        Serial.printf("Serving chart.min.js.gz, size: %u bytes\r\n", chart_len);
        AsyncWebServerResponse *response =
            request->beginResponse_P(
                200,
                "application/javascript",
                chart_min_js_gz_start,
                chart_len
            );
        Serial.printf("Response object created\r\n");
        response->addHeader("Content-Encoding", "gzip");
        Serial.printf("Added Content-Encoding header\r\n");
        request->send(response); 
        Serial.printf("Response sent\r\n");
    });

    httpServer.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "application/javascript", script_js_start, script_js_size); });
    httpServer.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "image/x-icon", favicon_ico_start, favicon_ico_end - favicon_ico_start); });

    httpServer.on("/images/heartbeat.png", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "image/png", heartbeat_png_start, heartbeat_png_end - heartbeat_png_start); });
    httpServer.on("/images/lungs.png", HTTP_GET, [](AsyncWebServerRequest *request)
                  { request->send_P(200, "image/png", lungs_png_start, lungs_png_end - lungs_png_start); });
    // add event handlers and other server routes here
    httpServer.addHandler(&ws);
    httpServer.begin();
    return 0;
}

log

curl -v -m 5 http://4.3.2.1
*   Trying 4.3.2.1:80...
* Established connection to 4.3.2.1 (4.3.2.1 port 80) from 4.3.2.2 port 61535
* using HTTP/1.x
> GET / HTTP/1.1
> Host: 4.3.2.1
> User-Agent: curl/8.16.0
> Accept: */*
>
* Request completely sent off
* Operation timed out after 5011 milliseconds with 0 bytes received
* closing connection #0
curl: (28) Operation timed out after 5011 milliseconds with 0 bytes received
Serving chart.min.js.gz, size: 68436  bytes
Response object created
Added Content-Encoding header
Response sent
2928 1131.640125 4.3.2.2 4.3.2.1 TCP 66 61535 → 80 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=256 SACK_PERM
2929 1131.646070 4.3.2.1 4.3.2.2 TCP 58 80 → 61535 [SYN, ACK] Seq=0 Ack=1 Win=20000 Len=0 MSS=1436
2930 1131.646211 4.3.2.2 4.3.2.1 TCP 54 61535 → 80 [ACK] Seq=1 Ack=1 Win=65535 Len=0
2931 1131.660875 4.3.2.2 4.3.2.1 HTTP 125 GET / HTTP/1.1
2932 1131.871797 4.3.2.1 4.3.2.2 TCP 54 80 → 61535 [ACK] Seq=1 Ack=72 Win=19929 Len=0
2935 1136.653013 4.3.2.2 4.3.2.1 TCP 54 61535 → 80 [FIN, ACK] Seq=72 Ack=1 Win=65535 Len=0
2936 1136.662427 4.3.2.1 4.3.2.2 TCP 54 80 → 61535 [FIN, ACK] Seq=1 Ack=73 Win=19928 Len=0
2937 1136.662535 4.3.2.2 4.3.2.1 TCP 54 61535 → 80 [ACK] Seq=73 Ack=2 Win=65535 Len=0
New contributor
조민석 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • You're reporting problems serving chart.min.js.gz, but your curl request is requesting /, which is supposed to serve index.html... Commented Nov 19 at 5:55
  • 3
    You can't use EMBED_TXTFILES for binary data, you need to use EMBED_FILES instead. Also, since requests to / are failing too, I would suggest to first try to send back simple strings (using request->send()) to rule out any networking issues. Commented Nov 19 at 5:56
  • 1
    @JesperJuhl Good luck running Apache on an ESP32 Commented 2 days ago
  • 1
    I found an error, and fortunately, I fixed it. The reason for the error was that the file was too big to hold esp32, and I changed the chart to a lighter library, so I checked that it worked well. Thank you for your help. Commented yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.