mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
examples:Added example for NAT from Wifi AP to STA.
This commit is contained in:
parent
3d23b3dfec
commit
baaa4c8910
@ -1,3 +1,6 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# Wi-Fi Examples
|
||||
|
||||
This directory contains a range of examples ESP-IDF projects. These are intended to demonstrate the Wi-Fi functionality, and to provide code that you can copy and adapt into your own projects.
|
||||
|
8
examples/wifi/softap_sta/CMakeLists.txt
Normal file
8
examples/wifi/softap_sta/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(softap_sta)
|
70
examples/wifi/softap_sta/README.md
Normal file
70
examples/wifi/softap_sta/README.md
Normal file
@ -0,0 +1,70 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# Wi-Fi SoftAP & Station Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example demonstrates how to use the ESP Wi-Fi driver to act as both an Access Point and a Station simultaneously using the SoftAP and Station features.
|
||||
With NAPT enabled on the softAP interface and the station interface set as the defaut interface this example can be used as Wifi nat router.
|
||||
|
||||
## How to use example
|
||||
### Configure the project
|
||||
|
||||
Open the project configuration menu (`idf.py menuconfig`).
|
||||
|
||||
In the `Example Configuration` menu:
|
||||
|
||||
* Set the Wi-Fi SoftAP configuration.
|
||||
* Set `WiFi AP SSID`.
|
||||
* Set `WiFi AP Password`.
|
||||
|
||||
* Set the Wi-Fi STA configuration.
|
||||
* Set `WiFi Remote AP SSID`.
|
||||
* Set `WiFi Remote AP Password`.
|
||||
|
||||
Optional: If necessary, modify the other choices to suit your needs.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run the monitor tool to view the serial output:
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
## Example Output
|
||||
|
||||
There is the console output for this example:
|
||||
|
||||
```
|
||||
I (680) WiFi SoftAP: ESP_WIFI_MODE_AP
|
||||
I (690) WiFi SoftAP: wifi_init_softap finished. SSID:myssid password:mypassword channel:1
|
||||
I (690) WiFi Sta: ESP_WIFI_MODE_STA
|
||||
I (690) WiFi Sta: wifi_init_sta finished.
|
||||
I (700) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
|
||||
I (800) wifi:mode : sta (58:bf:25:e0:41:00) + softAP (58:bf:25:e0:41:01)
|
||||
I (800) wifi:enable tsf
|
||||
I (810) wifi:Total power save buffer number: 16
|
||||
I (810) wifi:Init max length of beacon: 752/752
|
||||
I (810) wifi:Init max length of beacon: 752/752
|
||||
I (820) WiFi Sta: Station started
|
||||
I (820) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<1,1>, prof:1
|
||||
I (820) wifi:state: init -> auth (b0)
|
||||
I (830) wifi:state: auth -> assoc (0)
|
||||
E (840) wifi:Association refused temporarily, comeback time 1536 mSec
|
||||
I (2380) wifi:state: assoc -> assoc (0)
|
||||
I (2390) wifi:state: assoc -> run (10)
|
||||
I (2400) wifi:connected with myssid_c3, aid = 1, channel 1, 40U, bssid = 84:f7:03:60:86:1d
|
||||
I (2400) wifi:security: WPA2-PSK, phy: bgn, rssi: -14
|
||||
I (2410) wifi:pm start, type: 1
|
||||
|
||||
I (2410) wifi:AP's beacon interval = 102400 us, DTIM period = 2
|
||||
I (3920) WiFi Sta: Got IP:192.168.5.2
|
||||
I (3920) esp_netif_handlers: sta ip: 192.168.5.2, mask: 255.255.255.0, gw: 192.168.5.1
|
||||
I (3920) WiFi Sta: connected to ap SSID:myssid_c3 password:mypassword_c3
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
2
examples/wifi/softap_sta/main/CMakeLists.txt
Normal file
2
examples/wifi/softap_sta/main/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "softap_sta.c"
|
||||
INCLUDE_DIRS ".")
|
82
examples/wifi/softap_sta/main/Kconfig.projbuild
Normal file
82
examples/wifi/softap_sta/main/Kconfig.projbuild
Normal file
@ -0,0 +1,82 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
menu "SoftAP Configuration"
|
||||
comment "SoftAP Configuration"
|
||||
|
||||
config ESP_WIFI_AP_SSID
|
||||
string "WiFi AP SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) of the AP for the example to connect to.
|
||||
|
||||
config ESP_WIFI_AP_PASSWORD
|
||||
string "WiFi AP Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password of the AP for the example to use.
|
||||
|
||||
config ESP_WIFI_AP_CHANNEL
|
||||
int "WiFi AP Channel"
|
||||
range 1 14
|
||||
default 1
|
||||
help
|
||||
WiFi channel (network channel) of the AP for the example to use.
|
||||
|
||||
config ESP_MAX_STA_CONN_AP
|
||||
int "Maximal STA connections"
|
||||
default 4
|
||||
help
|
||||
Max number of the STA connects to AP.
|
||||
endmenu
|
||||
|
||||
menu "STA Configuration"
|
||||
comment "STA Configuration"
|
||||
|
||||
config ESP_WIFI_REMOTE_AP_SSID
|
||||
string "WiFi Remote AP SSID"
|
||||
default "otherapssid"
|
||||
help
|
||||
SSID (network name) for the example's sta to connect to.
|
||||
|
||||
config ESP_WIFI_REMOTE_AP_PASSWORD
|
||||
string "WiFi Remote AP Password"
|
||||
default "otherappassword"
|
||||
help
|
||||
WiFi password for the example to use.
|
||||
|
||||
config ESP_MAXIMUM_STA_RETRY
|
||||
int "Maximum retry"
|
||||
default 5
|
||||
help
|
||||
Set the maximum retry value to prevent the station from continuously
|
||||
attempting to reconnect to the Access Point (AP) when the AP doesn't exist.
|
||||
|
||||
choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
|
||||
prompt "WiFi Scan auth mode threshold"
|
||||
default ESP_WIFI_AUTH_WPA2_PSK
|
||||
help
|
||||
The weakest authmode to accept in the scan mode.
|
||||
This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present
|
||||
and ESP_WIFI_AUTH_OPEN is used. Please select ESP_WIFI_AUTH_WEP / ESP_WIFI_AUTH_WPA_PSK
|
||||
incase AP is operating in WEP / WPA mode.
|
||||
|
||||
config ESP_WIFI_AUTH_OPEN
|
||||
bool "OPEN"
|
||||
config ESP_WIFI_AUTH_WEP
|
||||
bool "WEP"
|
||||
config ESP_WIFI_AUTH_WPA_PSK
|
||||
bool "WPA PSK"
|
||||
config ESP_WIFI_AUTH_WPA2_PSK
|
||||
bool "WPA2 PSK"
|
||||
config ESP_WIFI_AUTH_WPA_WPA2_PSK
|
||||
bool "WPA/WPA2 PSK"
|
||||
config ESP_WIFI_AUTH_WPA3_PSK
|
||||
bool "WPA3 PSK"
|
||||
config ESP_WIFI_AUTH_WPA2_WPA3_PSK
|
||||
bool "WPA2/WPA3 PSK"
|
||||
config ESP_WIFI_AUTH_WAPI_PSK
|
||||
bool "WAPI PSK"
|
||||
endchoice
|
||||
|
||||
endmenu
|
||||
endmenu
|
241
examples/wifi/softap_sta/main/softap_sta.c
Normal file
241
examples/wifi/softap_sta/main/softap_sta.c
Normal file
@ -0,0 +1,241 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
/* WiFi softAP & station 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 "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif_net_stack.h"
|
||||
#include "esp_netif.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sockets.h"
|
||||
#if IP_NAPT
|
||||
#include "lwip/lwip_napt.h"
|
||||
#endif
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
/* The examples use WiFi configuration that you can set via project configuration menu.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define EXAMPLE_ESP_WIFI_STA_SSID "mywifissid"
|
||||
*/
|
||||
|
||||
/* STA Configuration */
|
||||
#define EXAMPLE_ESP_WIFI_STA_SSID CONFIG_ESP_WIFI_REMOTE_AP_SSID
|
||||
#define EXAMPLE_ESP_WIFI_STA_PASSWD CONFIG_ESP_WIFI_REMOTE_AP_PASSWORD
|
||||
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_STA_RETRY
|
||||
|
||||
#if CONFIG_ESP_WIFI_AUTH_OPEN
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WEP
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
|
||||
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
|
||||
#endif
|
||||
|
||||
/* AP Configuration */
|
||||
#define EXAMPLE_ESP_WIFI_AP_SSID CONFIG_ESP_WIFI_AP_SSID
|
||||
#define EXAMPLE_ESP_WIFI_AP_PASSWD CONFIG_ESP_WIFI_AP_PASSWORD
|
||||
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_AP_CHANNEL
|
||||
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN_AP
|
||||
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static const char *TAG_AP = "WiFi SoftAP";
|
||||
static const char *TAG_STA = "WiFi Sta";
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected/disconnected */
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
|
||||
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
|
||||
ESP_LOGI(TAG_AP, "Station "MACSTR" joined, AID=%d",
|
||||
MAC2STR(event->mac), event->aid);
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
||||
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
|
||||
ESP_LOGI(TAG_AP, "Station "MACSTR" left, AID=%d",
|
||||
MAC2STR(event->mac), event->aid);
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
ESP_LOGI(TAG_STA, "Station started");
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
|
||||
ESP_LOGI(TAG_STA, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize soft AP */
|
||||
esp_netif_t *wifi_init_softap(void)
|
||||
{
|
||||
esp_netif_t *esp_netif_ap = esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_config_t wifi_ap_config = {
|
||||
.ap = {
|
||||
.ssid = EXAMPLE_ESP_WIFI_AP_SSID,
|
||||
.ssid_len = strlen(EXAMPLE_ESP_WIFI_AP_SSID),
|
||||
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
||||
.password = EXAMPLE_ESP_WIFI_AP_PASSWD,
|
||||
.max_connection = EXAMPLE_MAX_STA_CONN,
|
||||
.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.required = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (strlen(EXAMPLE_ESP_WIFI_AP_PASSWD) == 0) {
|
||||
wifi_ap_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config));
|
||||
|
||||
ESP_LOGI(TAG_AP, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
||||
EXAMPLE_ESP_WIFI_AP_SSID, EXAMPLE_ESP_WIFI_AP_PASSWD, EXAMPLE_ESP_WIFI_CHANNEL);
|
||||
|
||||
return esp_netif_ap;
|
||||
}
|
||||
|
||||
/* Initialize wifi station */
|
||||
esp_netif_t *wifi_init_sta(void)
|
||||
{
|
||||
esp_netif_t *esp_netif_sta = esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_config_t wifi_sta_config = {
|
||||
.sta = {
|
||||
.ssid = EXAMPLE_ESP_WIFI_STA_SSID,
|
||||
.password = EXAMPLE_ESP_WIFI_STA_PASSWD,
|
||||
.scan_method = WIFI_ALL_CHANNEL_SCAN,
|
||||
.failure_retry_cnt = EXAMPLE_ESP_MAXIMUM_RETRY,
|
||||
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
|
||||
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
|
||||
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
|
||||
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
|
||||
*/
|
||||
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
||||
},
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config) );
|
||||
|
||||
ESP_LOGI(TAG_STA, "wifi_init_sta finished.");
|
||||
|
||||
return esp_netif_sta;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
//Initialize NVS
|
||||
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);
|
||||
|
||||
/* Initialize event group */
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
/* Register Event handler */
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
|
||||
/*Initialize WiFi */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
|
||||
|
||||
/* Initialize AP */
|
||||
ESP_LOGI(TAG_AP, "ESP_WIFI_MODE_AP");
|
||||
esp_netif_t *esp_netif_ap = wifi_init_softap();
|
||||
|
||||
/* Initialize STA */
|
||||
ESP_LOGI(TAG_STA, "ESP_WIFI_MODE_STA");
|
||||
esp_netif_t *esp_netif_sta = wifi_init_sta();
|
||||
|
||||
/* Start WiFi */
|
||||
ESP_ERROR_CHECK(esp_wifi_start() );
|
||||
|
||||
/*
|
||||
* Wait until either the connection is established (WIFI_CONNECTED_BIT) or
|
||||
* connection failed for the maximum number of re-tries (WIFI_FAIL_BIT).
|
||||
* The bits are set by event_handler() (see above)
|
||||
*/
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned,
|
||||
* hence we can test which event actually happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG_STA, "connected to ap SSID:%s password:%s",
|
||||
EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG_STA, "Failed to connect to SSID:%s, password:%s",
|
||||
EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
|
||||
} else {
|
||||
ESP_LOGE(TAG_STA, "UNEXPECTED EVENT");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set sta as the default interface */
|
||||
esp_netif_set_default_netif(esp_netif_sta);
|
||||
|
||||
/* Enable napt on the AP netif */
|
||||
if (esp_netif_napt_enable(esp_netif_ap) != ESP_OK) {
|
||||
ESP_LOGE(TAG_STA, "NAPT not enabled on the netif: %p", esp_netif_ap);
|
||||
}
|
||||
}
|
5
examples/wifi/softap_sta/sdkconfig.defaults
Normal file
5
examples/wifi/softap_sta/sdkconfig.defaults
Normal file
@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_LWIP_IP_FORWARD=y
|
||||
CONFIG_LWIP_IPV4_NAPT=y
|
Loading…
Reference in New Issue
Block a user