Ich möchte einen Webserver mit ESP32 eröffnenC++

Programme in C++. Entwicklerforum
Anonymous
 Ich möchte einen Webserver mit ESP32 eröffnen

Post by Anonymous »

Ich bin nicht gut in Englisch, deshalb habe ich einen Übersetzer verwendet.
Vielen Dank
Bitte haben Sie Verständnis dafür, dass wir nicht den gesamten Code anzeigen können.
Derzeit versuche ich, den AP-Modus in ESP32 zu öffnen und HTML-Informationen an die verbundene Person zu senden.
Ich versuche jedoch, chart.min.js oder chart.min.js.gz zu senden.
Die Die Anfrage wurde eindeutig von esp32 empfangen.
Ich kann Ihnen keine Daten senden.
Was soll ich tun?

Code: Select all

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"
)

Code: Select all

#include "_gateway_demo.h"
#include 
#include 
#include 
#include 

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

std::atomic 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

Code: Select all

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

Code: Select all

Serving chart.min.js.gz, size: 68436  bytes
Response object created
Added Content-Encoding header
Response sent

Code: Select all

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post