小和尚 发表于 2018-5-23 11:04:05

esp32 sta+ap模式无法上网

设置esp32模式sta+ap
sta连接路由器,手机连接ap后无法上网。
请问需要怎么编码?
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#define WIFI_SSID "KKKKg"
#define WIFI_PASS "123456zzz"

static const char *TAG = "espressif";

/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

static esp_err_t event_handler(void *ctx, system_event_t event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/ This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}

static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
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) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t sta_wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
wifi_config_t ap_wifi_config = {
.ap = {
.ssid = "esp32_ap",
.password = "xj123456",
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.max_connection = 3,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", sta_wifi_config.sta.ssid);
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_APSTA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_wifi_config) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}

void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
initialise_wifi();
}





pATAq 发表于 2018-5-23 11:31:45

代码是自己写的还是参考的教程?没用ESP32尝试过,只在树莓派这种Linux操作系统上弄过,需要注意的包括STA、AP需要在同一信道(除非双频芯片),开启内核转发。欢迎具体讨论一下,或者贴出你的参考链接

小和尚 发表于 2018-5-24 09:22:36

pATAq 发表于 2018-5-23 11:31
代码是自己写的还是参考的教程?没用ESP32尝试过,只在树莓派这种Linux操作系统上弄过,需要注意的包括STA ...

参考的官方IDF文档。
我同时看了乐鑫的Q&A,表明8266在sta+ap模式下,硬件是单信道。所以是有问题的。从测试效果来看esp32也是AP无法上网。估计esp32也是单信道。硬件上不支持吧。这样就把sta+ap模式价值大打折扣了
页: [1]
查看完整版本: esp32 sta+ap模式无法上网