/** * @file web_ctrl.c * @brief WiFi STA + HTTP 服务器 + 网页控制面板 */ #include "web_ctrl.h" #include "emm_v5.h" #include "magnet.h" #include "servo.h" #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_http_server.h" #include "esp_log.h" #include "esp_netif.h" #include "nvs_flash.h" static const char *TAG = "WEB_CTRL"; /* ---- WiFi 配置 ---- */ #define WIFI_SSID "ICE" #define WIFI_PASS "12345678" #define WIFI_MAX_RETRY 5 #define WIFI_CONNECT_TIMEOUT_MS 30000 static EventGroupHandle_t s_wifi_event_group; #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 static int s_retry_num = 0; /* 保存 IP 供 /api/status 使用 */ static char s_ip_str[16] = "0.0.0.0"; /* ---- HTML 页面 ---- */ static const char INDEX_HTML[] = R"raw( EDC 步进电机控制

EDC 步进电机控制

WiFi: -- IP: --

电机使能

速度模式

RPM

位置模式

RPM

舵机控制

90°

电磁铁

运行日志

等待指令...
)raw"; /* ============================================================ * WiFi 事件处理 * ============================================================ */ static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, void *data) { if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { wifi_event_sta_disconnected_t *ev = (wifi_event_sta_disconnected_t *)data; ESP_LOGW(TAG, "WiFi 断开 (reason=%d), 重试 %d/%d", ev->reason, s_retry_num + 1, WIFI_MAX_RETRY); if (s_retry_num < WIFI_MAX_RETRY) { s_retry_num++; esp_wifi_connect(); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } } else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *ev = (ip_event_got_ip_t *)data; snprintf(s_ip_str, sizeof(s_ip_str), IPSTR, IP2STR(&ev->ip_info.ip)); ESP_LOGI(TAG, "========================================"); ESP_LOGI(TAG, " WiFi 已连接!"); ESP_LOGI(TAG, " SSID: %s", WIFI_SSID); ESP_LOGI(TAG, " IP: %s", s_ip_str); ESP_LOGI(TAG, " 浏览器打开 http://%s", s_ip_str); ESP_LOGI(TAG, "========================================"); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } } static void wifi_init_sta(void) { s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); esp_event_handler_instance_t inst_any_id, inst_got_ip; ESP_ERROR_CHECK(esp_event_handler_instance_register( WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &inst_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register( IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &inst_got_ip)); wifi_config_t wifi_cfg = { .sta = { .ssid = WIFI_SSID, .password = WIFI_PASS, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "正在连接 WiFi: %s ...", WIFI_SSID); /* 等待连接成功或失败 (30 秒超时) */ EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, pdMS_TO_TICKS(WIFI_CONNECT_TIMEOUT_MS)); if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "WiFi 就绪 (IP: %s)", s_ip_str); } else if (bits & WIFI_FAIL_BIT) { ESP_LOGE(TAG, "WiFi 连接失败! 请检查 SSID/密码"); } else { ESP_LOGE(TAG, "WiFi 连接超时 (%d秒)!", WIFI_CONNECT_TIMEOUT_MS / 1000); } /* 无论 WiFi 是否成功, 都继续启动 HTTP 服务器 */ } /* ============================================================ * HTTP 查询字符串解析 * ============================================================ */ static int get_query_int(httpd_req_t *req, const char *key, int def) { char buf[128]; size_t len = httpd_req_get_url_query_len(req) + 1; if (len > sizeof(buf)) len = sizeof(buf); if (httpd_req_get_url_query_str(req, buf, len) == ESP_OK) { char val[16]; if (httpd_query_key_value(buf, key, val, sizeof(val)) == ESP_OK) { return atoi(val); } } return def; } /* ============================================================ * HTTP 处理器 * ============================================================ */ static esp_err_t root_handler(httpd_req_t *req) { const char *html = INDEX_HTML; size_t total = strlen(html); size_t offset = 0; httpd_resp_set_type(req, "text/html; charset=utf-8"); httpd_resp_set_hdr(req, "Connection", "close"); while (offset < total) { size_t chunk = total - offset; if (chunk > 1024) chunk = 1024; esp_err_t err = httpd_resp_send_chunk(req, html + offset, (int)chunk); if (err != ESP_OK) { ESP_LOGE(TAG, "发送 HTML 失败: %d", err); return err; } offset += chunk; } httpd_resp_send_chunk(req, NULL, 0); /* 结束分块 */ return ESP_OK; } static esp_err_t api_enable_handler(httpd_req_t *req) { int addr = get_query_int(req, "addr", 1); int state = get_query_int(req, "state", 0); Emm_V5_En_Control((uint8_t)addr, (bool)state, false); char resp[64]; snprintf(resp, sizeof(resp), "电机 %d %s", addr, state ? "使能" : "关闭"); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_velocity_handler(httpd_req_t *req) { int addr = get_query_int(req, "addr", 1); int dir = get_query_int(req, "dir", 0); int vel = get_query_int(req, "vel", 100); int acc = get_query_int(req, "acc", 10); Emm_V5_Vel_Control((uint8_t)addr, (uint8_t)dir, (uint16_t)vel, (uint8_t)acc, false); char resp[64]; snprintf(resp, sizeof(resp), "速度模式: 地址%d %s %dRPM acc=%d", addr, dir ? "CCW" : "CW", vel, acc); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_position_handler(httpd_req_t *req) { int addr = get_query_int(req, "addr", 1); int dir = get_query_int(req, "dir", 0); int vel = get_query_int(req, "vel", 100); int acc = get_query_int(req, "acc", 10); int clk = get_query_int(req, "clk", 2000); int raf = get_query_int(req, "raf", 0); Emm_V5_Pos_Control((uint8_t)addr, (uint8_t)dir, (uint16_t)vel, (uint8_t)acc, (uint32_t)clk, (bool)raf, false); char resp[80]; snprintf(resp, sizeof(resp), "位置模式: 地址%d %s %dRPM %d脉冲 %s", addr, dir ? "CCW" : "CW", vel, clk, raf ? "绝对" : "相对"); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_stop_handler(httpd_req_t *req) { int addr = get_query_int(req, "addr", 1); Emm_V5_Stop_Now((uint8_t)addr, false); char resp[32]; snprintf(resp, sizeof(resp), "电机 %d 已停止", addr); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_magnet_handler(httpd_req_t *req) { int state = get_query_int(req, "state", 0); magnet_set((bool)state); char resp[32]; snprintf(resp, sizeof(resp), "电磁铁 %s", state ? "吸合" : "释放"); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_status_handler(httpd_req_t *req) { char json[128]; wifi_ap_record_t ap; esp_err_t err = esp_wifi_sta_get_ap_info(&ap); snprintf(json, sizeof(json), "{\"ssid\":\"%s\",\"ip\":\"%s\"}", (err == ESP_OK) ? (char *)ap.ssid : "N/A", s_ip_str); httpd_resp_set_type(req, "application/json"); httpd_resp_send(req, json, HTTPD_RESP_USE_STRLEN); return ESP_OK; } static esp_err_t api_servo_handler(httpd_req_t *req) { char buf[128]; size_t len = httpd_req_get_url_query_len(req) + 1; if (len > sizeof(buf)) len = sizeof(buf); if (httpd_req_get_url_query_str(req, buf, len) == ESP_OK) { char val[16]; if (httpd_query_key_value(buf, "pos", val, sizeof(val)) == ESP_OK) { if (strcmp(val, "up") == 0) { servo_set_up(); } else { servo_set_down(); } char resp[32]; snprintf(resp, sizeof(resp), "舵机: %s", val); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } if (httpd_query_key_value(buf, "angle", val, sizeof(val)) == ESP_OK) { float angle = atof(val); servo_set_angle(angle); char resp[32]; snprintf(resp, sizeof(resp), "舵机: %.1f°", (double)angle); httpd_resp_set_type(req, "text/plain; charset=utf-8"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } } httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "缺少 angle 或 pos 参数"); return ESP_FAIL; } /* favicon — 204 No Content, 避免浏览器反复请求 */ static esp_err_t favicon_handler(httpd_req_t *req) { httpd_resp_set_status(req, "204 No Content"); httpd_resp_send(req, NULL, 0); return ESP_OK; } static void http_server_init(void) { httpd_handle_t server = NULL; httpd_config_t cfg = HTTPD_DEFAULT_CONFIG(); cfg.stack_size = 8192; /* 默认 4096 不够, 增大 */ cfg.max_uri_handlers = 16; cfg.max_open_sockets = 4; cfg.lru_purge_enable = true; ESP_ERROR_CHECK(httpd_start(&server, &cfg)); httpd_uri_t uri_root = { .uri = "/", .method = HTTP_GET, .handler = root_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_root); httpd_uri_t uri_enable = { .uri = "/api/enable", .method = HTTP_GET, .handler = api_enable_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_enable); httpd_uri_t uri_velocity = { .uri = "/api/velocity", .method = HTTP_GET, .handler = api_velocity_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_velocity); httpd_uri_t uri_position = { .uri = "/api/position", .method = HTTP_GET, .handler = api_position_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_position); httpd_uri_t uri_stop = { .uri = "/api/stop", .method = HTTP_GET, .handler = api_stop_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_stop); httpd_uri_t uri_magnet = { .uri = "/api/magnet", .method = HTTP_GET, .handler = api_magnet_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_magnet); httpd_uri_t uri_status = { .uri = "/api/status", .method = HTTP_GET, .handler = api_status_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_status); httpd_uri_t uri_servo = { .uri = "/api/servo", .method = HTTP_GET, .handler = api_servo_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_servo); /* favicon.ico — 返回空, 避免 404 警告 */ httpd_uri_t uri_favicon = { .uri = "/favicon.ico", .method = HTTP_GET, .handler = favicon_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &uri_favicon); ESP_LOGI(TAG, "HTTP 服务器已启动 (端口 %d)", cfg.server_port); } /* ============================================================ * 公开接口 * ============================================================ */ void web_ctrl_init(void) { wifi_init_sta(); http_server_init(); }