esp8266 idf 使用ap+sta模式,代码示例
时间: 2023-04-07 17:03:57 浏览: 242
我可以回答这个问题。以下是一个 esp8266 idf 使用 ap sta 模式的代码示例:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASS "your_wifi_password"
static const char *TAG = "wifi station";
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "station start");
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "station connected to AP");
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "station got IP address");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "station disconnected from AP");
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta(void)
{
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
void app_main()
{
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
}
阅读全文