Reach a server

HTTP Server local page

Serve a page on the device to change settings. Common for small-batch debug.

Reach a server Page on the device ESP32 Wi-Fi parts

Official path: examples/protocols/http_server/simple

01 Overview

Runs an HTTP server on an ESP32 that is already connected to Wi-Fi. The device's IP address can be opened in a browser to access a configuration page. Suitable for LAN-based device debugging, parameter changes, and status monitoring.

Use when: engineer debug, local switch control, provisioning portal (SoftAP mode), production line batch configuration.

Skip when: public internet exposure without authentication and TLS, high concurrency, or HTTPS requirement.

Essence: This is a lightweight embedded web server based on the esp_http_server component, consuming minimal resources, ideal for resource-constrained MCUs.

Difference from cloud: It does not rely on external servers; all logic is local, providing fast response but not accessible from the internet without port forwarding or tunneling.

02 Hardware

  • No extra parts: This example only requires an ESP32 development board; no external sensors or actuators are needed.
  • Wi-Fi prerequisite: Ensure Station or SoftAP mode is properly connected; otherwise, the HTTP service cannot be accessed.
  • RAM consideration: Each concurrent connection consumes about 3-4KB of RAM. If serving multiple browsers, reserve enough memory based on the number of connections.
  • Flash consideration: If the web page content is large, store it in SPI Flash or a file system (e.g., SPIFFS) to avoid consuming RAM.
  • Power: In SoftAP mode, Wi-Fi transmission power is higher; ensure a stable power supply.
  • Layout: For productization, consider antenna layout and RF interference, but this example does not involve that.

03 Software flow

1. Initialize Wi-Fi: Connect to a router (Station) or create an access point (SoftAP), and wait for an IP address.

2. Start HTTP server: Call esp_http_server_start() to create a server instance, configuring the port (default 80) and maximum number of connections.

3. Register URI handlers: Use httpd_register_uri_handler() to register callbacks for HTTP methods (GET/POST), e.g., root path '/' returns an HTML page.

4. Handle requests: In the callback, parse URL parameters or request body, perform actions (e.g., read/modify parameters), and return an HTTP response (text, JSON, or redirect).

5. Persist parameters: Write modified parameters to NVS (non-volatile storage) to ensure they survive a reboot.

6. Error handling: Check for server start failures, memory exhaustion, etc., and retry or log errors appropriately.

7. Security enhancements (for production): Add basic authentication, CSRF protection, input validation, etc., to avoid vulnerabilities.

04 Core points

  • A LAN page is not a cloud API: Do not expose the LAN configuration page to the public internet; it is vulnerable to attacks.
  • Do not ship debug pages on the public internet: Production firmware should remove or disable debug interfaces.
  • Custom work: Configuration portal, access control, NVS binding, custom page styling.
  • Mind HTTP methods: Use GET for reads and POST for modifications; avoid sending sensitive data in URLs.
  • RAM limits: Do not send large pages at once; use chunked transfer or asynchronous processing.
  • Do not copy HTTP implementations from other chips: ESP32's esp_http_server is asynchronous and event-driven, unlike blocking servers on other platforms (e.g., Linux).

中文

连上服务器

HTTP Server 本地网页

设备开网页改参数。小批量调试和本地控制常用。

连上服务器 设备上的网页 ESP32 Wi-Fi parts

官方路径: examples/protocols/http_server/simple

01 项目概述

在已联网的 ESP32 上运行 HTTP 服务,通过浏览器访问设备 IP 即可打开配置页面。适合局域网内的设备调试、参数修改和状态查看。

适用场景: 工程师现场调试、本地开关控制、配网门户(SoftAP 模式)、产线批量设置。

不适用场景: 公网远程访问(无鉴权与 TLS 时不要暴露)、高并发请求、需要 HTTPS 加密的场景。

本质: 这是一个轻量级的嵌入式 Web 服务器,基于 esp_http_server 组件,占用资源少,适合资源受限的 MCU。

与云平台的区别: 它不依赖外部服务器,所有逻辑都在设备本地,响应快,但无法从外网直接访问(除非做端口映射或内网穿透)。

02 项目硬件描述

  • 无额外器件: 本示例仅需 ESP32 开发板,无需外接传感器或执行器。
  • Wi-Fi 前提: 必须确保 Station 或 SoftAP 模式已正常连接,否则无法访问 HTTP 服务。
  • RAM 考虑: 每个并发连接会占用约 3-4KB RAM,若同时服务多个浏览器,需根据连接数预留足够内存。
  • Flash 考虑: 网页内容若较大,可存储在 SPI Flash 或文件系统(如 SPIFFS)中,避免占用 RAM。
  • 电源: 若使用 SoftAP 模式,Wi-Fi 发射功耗较高,需确保电源稳定。
  • 布局: 若产品化,需考虑天线布局和射频干扰,但本示例不涉及。

03 项目软件流程描述

1. 初始化 Wi-Fi: 先连接路由器(Station)或创建热点(SoftAP),等待获取 IP 地址。

2. 启动 HTTP 服务器: 调用 esp_http_server_start() 创建服务器实例,配置端口(默认 80)和最大连接数。

3. 注册 URI 处理函数: 使用 httpd_register_uri_handler() 注册 GET/POST 等方法的处理回调,例如根路径 '/' 返回 HTML 页面。

4. 处理请求: 在回调中解析 URL 参数或请求体,执行相应操作(如读取/修改参数),并返回 HTTP 响应(文本、JSON 或重定向)。

5. 参数持久化: 将修改后的参数写入 NVS(非易失存储),确保重启后不丢失。

6. 错误处理: 检查服务器启动失败、内存不足等错误,并适当重试或记录日志。

7. 安全增强(产品化): 添加基本认证(Basic Auth)、CSRF 防护、输入验证等,避免安全漏洞。

04 项目核心点

  • 局域网网页 ≠ 云 API: 不要将局域网配置页直接暴露到公网,否则易受攻击。
  • 不要把调试页留在公网固件里: 量产固件应移除或禁用调试接口。
  • 可定制: 配置门户、权限控制、与 NVS 绑定、自定义页面样式。
  • 注意 HTTP 方法: 根据操作类型使用 GET(读取)和 POST(修改),避免在 URL 中传输敏感数据。
  • RAM 限制: 不要一次性发送大页面,可分块发送或使用异步处理。
  • 不要照搬其他芯片的 HTTP 实现: ESP32 的 esp_http_server 是异步事件驱动,与其他平台(如 Linux)的阻塞式服务器不同。