功能:浏览器/open 开 led
/close 关led
如不关,则亮灯10秒自动关闭
已买220v 转5V电源,5V固态继电器,开始组装无线开关。
代替家里华为的hlink灯泡,此灯必须要连入网络才能控制开和关,经常时灵时不灵。
#include <stdio.h>
#include <string.h>
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_server.h"
#include "driver/gpio.h"
#include "esp_timer.h"#define WIFI_SSID "ESP32_AP"
#define WIFI_PASS "wz123456"
#define MAX_STA_CONN 4#define GPIO_out 2
static int bz=0;
static const char *TAG = "ESP32_AP_HTTP";
static int64_t time = 0;// HTTP GET 处理程序,响应 "OK"
esp_err_t open_handler(httpd_req_t *req) {char resp_str[21]="led open"; httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");httpd_resp_set_type(req, "text/plain");httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); gpio_set_level(GPIO_out,1);bz=1;return ESP_OK;
}//http GET "/close"处理函数
esp_err_t close_handler(httpd_req_t *req){char resp_str[21]="led close";httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_set_type(req, "text/plain");httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); gpio_set_level(GPIO_out,0);bz=0;return ESP_OK;
}
// 配置 HTTP 服务器并注册 URI 处理程序
httpd_handle_t start_webserver(void) {httpd_config_t config = HTTPD_DEFAULT_CONFIG();httpd_handle_t server = NULL;if (httpd_start(&server, &config) == ESP_OK) {httpd_uri_t ok_uri = {.uri = "/open",.method = HTTP_GET,.handler = open_handler,.user_ctx = NULL};httpd_register_uri_handler(server, &ok_uri);httpd_uri_t close_uri = {.uri = "/close",.method = HTTP_GET,.handler = close_handler,.user_ctx = NULL};httpd_register_uri_handler(server, &close_uri); }return server;
}// 初始化 AP 模式
void wifi_init_softap() {wifi_config_t wifi_config = {.ap = {.ssid = WIFI_SSID,.ssid_len = strlen(WIFI_SSID),.password = WIFI_PASS,.max_connection = MAX_STA_CONN,.authmode = WIFI_AUTH_WPA_WPA2_PSK},};if (strlen(WIFI_PASS) == 0) {wifi_config.ap.authmode = WIFI_AUTH_OPEN;}ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));ESP_ERROR_CHECK(esp_wifi_start());ESP_LOGI(TAG, "AP initialized. SSID:%s password:%s", WIFI_SSID, WIFI_PASS);
}void app_main(void) {gpio_config_t io_conf = {}; io_conf.intr_type = GPIO_INTR_DISABLE;io_conf.mode = GPIO_MODE_OUTPUT;io_conf.pin_bit_mask = 1ULL<<GPIO_out; io_conf.pull_down_en = 0;io_conf.pull_up_en = 0;gpio_config(&io_conf);esp_err_t ret = nvs_flash_init();if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {ESP_ERROR_CHECK(nvs_flash_erase());ret = nvs_flash_init();}ESP_ERROR_CHECK(ret);ESP_ERROR_CHECK(esp_netif_init());ESP_ERROR_CHECK(esp_event_loop_create_default());esp_netif_create_default_wifi_ap();wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();ESP_ERROR_CHECK(esp_wifi_init(&cfg));wifi_init_softap();// 启动 HTTP 服务器start_webserver();gpio_set_level(GPIO_out,0);while(1){if(bz){time=esp_timer_get_time();while((esp_timer_get_time()-time)<10000000){ //开灯10秒vTaskDelay(10/portTICK_PERIOD_MS);}gpio_set_level(GPIO_out,0);bz=0;}vTaskDelay(10 / portTICK_PERIOD_MS);}
}