欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > esp32 ap httpsever 控制led

esp32 ap httpsever 控制led

2025/5/31 14:16:23 来源:https://blog.csdn.net/m0_59802969/article/details/143471343  浏览:    关键词:esp32 ap httpsever 控制led

功能:浏览器/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);}
}

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词