15#include "esp_chip_info.h"
16#include "esp_http_server.h"
19#include "esp_system.h"
21static const char *
TAG =
"web_server";
31 esp_netif_ip_info_t ip_info = {0};
33 esp_netif_t *netif = esp_netif_get_handle_from_ifkey(
"ETH_DEF");
36 esp_netif_get_ip_info(netif, &ip_info);
39 uint32_t uptime_sec = esp_log_timestamp() / 1000;
42 int len = snprintf(response,
sizeof(response),
46 "<meta charset=\"utf-8\">"
47 "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">"
48 "<title>WT32-ETH01</title>"
50 "body{font-family:system-ui,sans-serif;max-width:640px;margin:2rem auto;padding:0 1rem;color:#222}"
51 "h1{color:#0066cc}code{background:#f0f0f0;padding:2px 6px;border-radius:4px}"
52 "table{border-collapse:collapse;width:100%%}td{padding:6px 0;border-bottom:1px solid #eee}"
53 "td:first-child{color:#666;width:40%%}"
58 "<p>Веб-сервер работает по Ethernet.</p>"
60 "<tr><td>IP</td><td><code>" IPSTR
"</code></td></tr>"
61 "<tr><td>Маска</td><td><code>" IPSTR
"</code></td></tr>"
62 "<tr><td>Шлюз</td><td><code>" IPSTR
"</code></td></tr>"
63 "<tr><td>Uptime</td><td>%lu с</td></tr>"
65 "<p>API: <a href=\"/api/status\">/api/status</a></p>"
68 IP2STR(&ip_info.netmask),
70 (
unsigned long)uptime_sec);
72 httpd_resp_set_type(req,
"text/html; charset=utf-8");
73 return httpd_resp_send(req, response, len);
84 esp_netif_ip_info_t ip_info = {0};
85 esp_netif_t *netif = esp_netif_get_handle_from_ifkey(
"ETH_DEF");
88 esp_netif_get_ip_info(netif, &ip_info);
91 esp_chip_info_t chip_info;
92 esp_chip_info(&chip_info);
95 int len = snprintf(response,
sizeof(response),
97 "\"board\":\"WT32-ETH01\","
98 "\"ip\":\"" IPSTR
"\","
99 "\"netmask\":\"" IPSTR
"\","
100 "\"gateway\":\"" IPSTR
"\","
106 IP2STR(&ip_info.netmask),
110 (
unsigned long)(esp_log_timestamp() / 1000));
112 httpd_resp_set_type(req,
"application/json");
113 return httpd_resp_send(req, response, len);
125 .uri =
"/api/status",
132 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
134 config.lru_purge_enable =
true;
136 httpd_handle_t server = NULL;
137 ESP_RETURN_ON_FALSE(httpd_start(&server, &config) == ESP_OK, ESP_FAIL,
138 TAG,
"httpd start failed");
140 httpd_register_uri_handler(server, &
root_uri);
141 httpd_register_uri_handler(server, &
status_uri);
143 ESP_LOGI(
TAG,
"HTTP server on port %d", config.server_port);
144 *server_out = server;
151 ESP_LOGI(
TAG,
"Stopping HTTP server");
152 return httpd_stop(server);
esp_err_t web_server_stop(httpd_handle_t server)
Останавливает HTTP-сервер и освобождает ресурсы.
static const httpd_uri_t status_uri
Дескриптор маршрута JSON API.
static const httpd_uri_t root_uri
Дескриптор маршрута корневой страницы
static esp_err_t root_get_handler(httpd_req_t *req)
Обработчик GET / — HTML-страница с сетевой информацией.
esp_err_t web_server_start(httpd_handle_t *server_out)
Запускает HTTP-сервер на порту по умолчанию (80).
static esp_err_t status_get_handler(httpd_req_t *req)
Обработчик GET /api/status — JSON со статусом платы и сети.
Публичный интерфейс встроенного HTTP-сервера.