RCM — ESP32 Room Controller
WT32-ETH01 firmware
Loading...
Searching...
No Matches
web_server.c
Go to the documentation of this file.
1
9#include "web_server.h"
10
11#include <stdio.h>
12#include <string.h>
13
14#include "esp_check.h"
15#include "esp_chip_info.h"
16#include "esp_http_server.h"
17#include "esp_log.h"
18#include "esp_netif.h"
19#include "esp_system.h"
20
21static const char *TAG = "web_server";
22
29static esp_err_t root_get_handler(httpd_req_t *req)
30{
31 esp_netif_ip_info_t ip_info = {0};
32 /* Интерфейс Ethernet по умолчанию, созданный ESP_NETIF_DEFAULT_ETH() */
33 esp_netif_t *netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
34
35 if (netif) {
36 esp_netif_get_ip_info(netif, &ip_info);
37 }
38
39 uint32_t uptime_sec = esp_log_timestamp() / 1000;
40
41 char response[1024];
42 int len = snprintf(response, sizeof(response),
43 "<!DOCTYPE html>"
44 "<html lang=\"ru\">"
45 "<head>"
46 "<meta charset=\"utf-8\">"
47 "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">"
48 "<title>WT32-ETH01</title>"
49 "<style>"
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%%}"
54 "</style>"
55 "</head>"
56 "<body>"
57 "<h1>WT32-ETH01</h1>"
58 "<p>Веб-сервер работает по Ethernet.</p>"
59 "<table>"
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>"
64 "</table>"
65 "<p>API: <a href=\"/api/status\">/api/status</a></p>"
66 "</body></html>",
67 IP2STR(&ip_info.ip),
68 IP2STR(&ip_info.netmask),
69 IP2STR(&ip_info.gw),
70 (unsigned long)uptime_sec);
71
72 httpd_resp_set_type(req, "text/html; charset=utf-8");
73 return httpd_resp_send(req, response, len);
74}
75
82static esp_err_t status_get_handler(httpd_req_t *req)
83{
84 esp_netif_ip_info_t ip_info = {0};
85 esp_netif_t *netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
86
87 if (netif) {
88 esp_netif_get_ip_info(netif, &ip_info);
89 }
90
91 esp_chip_info_t chip_info;
92 esp_chip_info(&chip_info);
93
94 char response[256];
95 int len = snprintf(response, sizeof(response),
96 "{"
97 "\"board\":\"WT32-ETH01\","
98 "\"ip\":\"" IPSTR "\","
99 "\"netmask\":\"" IPSTR "\","
100 "\"gateway\":\"" IPSTR "\","
101 "\"cores\":%d,"
102 "\"revision\":%d,"
103 "\"uptime_sec\":%lu"
104 "}",
105 IP2STR(&ip_info.ip),
106 IP2STR(&ip_info.netmask),
107 IP2STR(&ip_info.gw),
108 chip_info.cores,
109 chip_info.revision,
110 (unsigned long)(esp_log_timestamp() / 1000));
111
112 httpd_resp_set_type(req, "application/json");
113 return httpd_resp_send(req, response, len);
114}
115
117static const httpd_uri_t root_uri = {
118 .uri = "/",
119 .method = HTTP_GET,
120 .handler = root_get_handler,
121};
122
124static const httpd_uri_t status_uri = {
125 .uri = "/api/status",
126 .method = HTTP_GET,
127 .handler = status_get_handler,
128};
129
130esp_err_t web_server_start(httpd_handle_t *server_out)
131{
132 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
133 /* При нехватке слотов соединений вытеснять самые старые (LRU) */
134 config.lru_purge_enable = true;
135
136 httpd_handle_t server = NULL;
137 ESP_RETURN_ON_FALSE(httpd_start(&server, &config) == ESP_OK, ESP_FAIL,
138 TAG, "httpd start failed");
139
140 httpd_register_uri_handler(server, &root_uri);
141 httpd_register_uri_handler(server, &status_uri);
142
143 ESP_LOGI(TAG, "HTTP server on port %d", config.server_port);
144 *server_out = server;
145 return ESP_OK;
146}
147
148esp_err_t web_server_stop(httpd_handle_t server)
149{
150 if (server) {
151 ESP_LOGI(TAG, "Stopping HTTP server");
152 return httpd_stop(server);
153 }
154 return ESP_OK;
155}
esp_err_t web_server_stop(httpd_handle_t server)
Останавливает HTTP-сервер и освобождает ресурсы.
Definition web_server.c:148
static const httpd_uri_t status_uri
Дескриптор маршрута JSON API.
Definition web_server.c:124
static const char * TAG
Definition web_server.c:21
static const httpd_uri_t root_uri
Дескриптор маршрута корневой страницы
Definition web_server.c:117
static esp_err_t root_get_handler(httpd_req_t *req)
Обработчик GET / — HTML-страница с сетевой информацией.
Definition web_server.c:29
esp_err_t web_server_start(httpd_handle_t *server_out)
Запускает HTTP-сервер на порту по умолчанию (80).
Definition web_server.c:130
static esp_err_t status_get_handler(httpd_req_t *req)
Обработчик GET /api/status — JSON со статусом платы и сети.
Definition web_server.c:82
Публичный интерфейс встроенного HTTP-сервера.