AP、STA的概念以及AP+STA的实现-结合ESP32浅谈

1.概念

在无线局域网(英语:Wireless LAN,缩写WLAN)中,提供网络接入服务的设备,称为AP(Access Point)也叫网络接入点,俗称:热点。

连接AP,继而接入网络的设备,称为STA(Station),也叫站点。

举个例子,你的电脑连接到路由器,这就构成了一个路由器+电脑的局域网(不了解局域网概念的,可以看我上一篇博客),此时提供网路接入服务的是路由器,它就是AP,而作为连接路由器的设备-电脑,就是STA。通过路由器,电脑可以访问外网。

2.代码及分析

最近,在使用ESP32制作一些小玩意,不得不说,ESP32的AP+STA模式使用真香。

啥叫“AP+STA”?,就是一个设备,同时即当作AP(向外提供网络服务),又作为STA。简单地讲,就是双网卡。

在此分享一个代码,供大家使用玩耍。(使用开发板为ESP32(ESP8266也是可以滴),编译环境是ESP-IDF v4.2)

代码中的一些概念,如果不了解可以留言,大部分东西都可以直接拿来用,不要客气,客官们。

/* BSD AP+STA API Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"

#define AP_DEFAULT_SSID         "Who_love_me"
#define AP_DEFAULT_PASSWORD         "nobody"
#define AP_DEFAULT_MAX_CONN         (5)

typedef struct {
    wifi_sta_config_t sta;
    wifi_ap_config_t ap;
} custom_wifi_config_t;

static custom_wifi_config_t custom_wifi_config = {
    .sta = {
        .ssid = "Xiaoxin",//target ap ssid
        .password = "666888666",//target ap password
        .bssid_set = false,
    },
    .ap = {
        .ssid = AP_DEFAULT_SSID,
        .password = AP_DEFAULT_PASSWORD,
        .max_connection = AP_DEFAULT_MAX_CONN,
        .authmode = WIFI_AUTH_WPA_WPA2_PSK,
    },
};

static const char *TAG = "example";

static void custom_wifi_ap_staconnected_cb(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
    ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
                MAC2STR(event->mac), event->aid);
}

static void custom_wifi_ap_stadisconnected_cb(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
    ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
                MAC2STR(event->mac), event->aid);   
}

static void custom_wifi_got_ip_cb(void *arg, esp_event_base_t event_base,
                      int32_t event_id, void *event_data)
{
    ESP_LOGI(TAG, "Got IP event!");
    ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
    ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&event->ip_info.ip));
}

static void custom_wifi_disconnect_cb(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    static int count = 5;
    esp_err_t err;
    ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
    if (count > 0) {
        err = esp_wifi_connect();
        if (err == ESP_ERR_WIFI_NOT_STARTED) {
            return;
        }
        count--;
    } else {
        ESP_LOGE(TAG, "try max connect count, but failed");
    }
}

bool custom_wifi_ap_init()
{
    esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_AP();
    esp_netif_t *netif = esp_netif_new(&netif_config);
    assert(netif);
    esp_netif_attach_wifi_ap(netif);
    return true;
}

bool custom_wifi_sta_init(void)
{
    esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA();
    esp_netif_t *netif = esp_netif_new(&netif_config);
    assert(netif);
    esp_netif_attach_wifi_station(netif);
    return true;
}

esp_err_t custom_ap_sta_init(void)
{
    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_FLASH));

    
    custom_wifi_ap_init();
    custom_wifi_sta_init();
    esp_wifi_set_default_wifi_sta_handlers(); 

    ESP_ERROR_CHECK(esp_wifi_start());
    
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
    esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &custom_wifi_ap_staconnected_cb, NULL);
    esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &custom_wifi_ap_stadisconnected_cb, NULL);

    esp_wifi_set_config(ESP_IF_WIFI_AP, (wifi_config_t*)&custom_wifi_config.ap);

    esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &custom_wifi_disconnect_cb, NULL);
    esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &custom_wifi_got_ip_cb, NULL);
    esp_wifi_set_config(ESP_IF_WIFI_STA, (wifi_config_t*)&custom_wifi_config.sta);
    esp_wifi_connect();
    return ESP_OK;
}

void app_main(void)
{
    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    custom_ap_sta_init();
}

(谢谢点赞或收藏)

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值