Skip to content

Commit baaa4c8

Browse files
examples:Added example for NAT from Wifi AP to STA.
1 parent 3d23b3d commit baaa4c8

File tree

7 files changed

+411
-0
lines changed

7 files changed

+411
-0
lines changed

examples/wifi/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
3+
14
# Wi-Fi Examples
25

36
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
# The following five lines of boilerplate have to be in your project's
4+
# CMakeLists in this exact order for cmake to work correctly
5+
cmake_minimum_required(VERSION 3.16)
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(softap_sta)

examples/wifi/softap_sta/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
3+
4+
# Wi-Fi SoftAP & Station Example
5+
6+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
7+
8+
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.
9+
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.
10+
11+
## How to use example
12+
### Configure the project
13+
14+
Open the project configuration menu (`idf.py menuconfig`).
15+
16+
In the `Example Configuration` menu:
17+
18+
* Set the Wi-Fi SoftAP configuration.
19+
* Set `WiFi AP SSID`.
20+
* Set `WiFi AP Password`.
21+
22+
* Set the Wi-Fi STA configuration.
23+
* Set `WiFi Remote AP SSID`.
24+
* Set `WiFi Remote AP Password`.
25+
26+
Optional: If necessary, modify the other choices to suit your needs.
27+
28+
### Build and Flash
29+
30+
Build the project and flash it to the board, then run the monitor tool to view the serial output:
31+
32+
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
33+
34+
(To exit the serial monitor, type ``Ctrl-]``.)
35+
36+
## Example Output
37+
38+
There is the console output for this example:
39+
40+
```
41+
I (680) WiFi SoftAP: ESP_WIFI_MODE_AP
42+
I (690) WiFi SoftAP: wifi_init_softap finished. SSID:myssid password:mypassword channel:1
43+
I (690) WiFi Sta: ESP_WIFI_MODE_STA
44+
I (690) WiFi Sta: wifi_init_sta finished.
45+
I (700) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
46+
I (800) wifi:mode : sta (58:bf:25:e0:41:00) + softAP (58:bf:25:e0:41:01)
47+
I (800) wifi:enable tsf
48+
I (810) wifi:Total power save buffer number: 16
49+
I (810) wifi:Init max length of beacon: 752/752
50+
I (810) wifi:Init max length of beacon: 752/752
51+
I (820) WiFi Sta: Station started
52+
I (820) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<1,1>, prof:1
53+
I (820) wifi:state: init -> auth (b0)
54+
I (830) wifi:state: auth -> assoc (0)
55+
E (840) wifi:Association refused temporarily, comeback time 1536 mSec
56+
I (2380) wifi:state: assoc -> assoc (0)
57+
I (2390) wifi:state: assoc -> run (10)
58+
I (2400) wifi:connected with myssid_c3, aid = 1, channel 1, 40U, bssid = 84:f7:03:60:86:1d
59+
I (2400) wifi:security: WPA2-PSK, phy: bgn, rssi: -14
60+
I (2410) wifi:pm start, type: 1
61+
62+
I (2410) wifi:AP's beacon interval = 102400 us, DTIM period = 2
63+
I (3920) WiFi Sta: Got IP:192.168.5.2
64+
I (3920) esp_netif_handlers: sta ip: 192.168.5.2, mask: 255.255.255.0, gw: 192.168.5.1
65+
I (3920) WiFi Sta: connected to ap SSID:myssid_c3 password:mypassword_c3
66+
```
67+
68+
## Troubleshooting
69+
70+
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "softap_sta.c"
2+
INCLUDE_DIRS ".")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
menu "Example Configuration"
2+
3+
menu "SoftAP Configuration"
4+
comment "SoftAP Configuration"
5+
6+
config ESP_WIFI_AP_SSID
7+
string "WiFi AP SSID"
8+
default "myssid"
9+
help
10+
SSID (network name) of the AP for the example to connect to.
11+
12+
config ESP_WIFI_AP_PASSWORD
13+
string "WiFi AP Password"
14+
default "mypassword"
15+
help
16+
WiFi password of the AP for the example to use.
17+
18+
config ESP_WIFI_AP_CHANNEL
19+
int "WiFi AP Channel"
20+
range 1 14
21+
default 1
22+
help
23+
WiFi channel (network channel) of the AP for the example to use.
24+
25+
config ESP_MAX_STA_CONN_AP
26+
int "Maximal STA connections"
27+
default 4
28+
help
29+
Max number of the STA connects to AP.
30+
endmenu
31+
32+
menu "STA Configuration"
33+
comment "STA Configuration"
34+
35+
config ESP_WIFI_REMOTE_AP_SSID
36+
string "WiFi Remote AP SSID"
37+
default "otherapssid"
38+
help
39+
SSID (network name) for the example's sta to connect to.
40+
41+
config ESP_WIFI_REMOTE_AP_PASSWORD
42+
string "WiFi Remote AP Password"
43+
default "otherappassword"
44+
help
45+
WiFi password for the example to use.
46+
47+
config ESP_MAXIMUM_STA_RETRY
48+
int "Maximum retry"
49+
default 5
50+
help
51+
Set the maximum retry value to prevent the station from continuously
52+
attempting to reconnect to the Access Point (AP) when the AP doesn't exist.
53+
54+
choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
55+
prompt "WiFi Scan auth mode threshold"
56+
default ESP_WIFI_AUTH_WPA2_PSK
57+
help
58+
The weakest authmode to accept in the scan mode.
59+
This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present
60+
and ESP_WIFI_AUTH_OPEN is used. Please select ESP_WIFI_AUTH_WEP / ESP_WIFI_AUTH_WPA_PSK
61+
incase AP is operating in WEP / WPA mode.
62+
63+
config ESP_WIFI_AUTH_OPEN
64+
bool "OPEN"
65+
config ESP_WIFI_AUTH_WEP
66+
bool "WEP"
67+
config ESP_WIFI_AUTH_WPA_PSK
68+
bool "WPA PSK"
69+
config ESP_WIFI_AUTH_WPA2_PSK
70+
bool "WPA2 PSK"
71+
config ESP_WIFI_AUTH_WPA_WPA2_PSK
72+
bool "WPA/WPA2 PSK"
73+
config ESP_WIFI_AUTH_WPA3_PSK
74+
bool "WPA3 PSK"
75+
config ESP_WIFI_AUTH_WPA2_WPA3_PSK
76+
bool "WPA2/WPA3 PSK"
77+
config ESP_WIFI_AUTH_WAPI_PSK
78+
bool "WAPI PSK"
79+
endchoice
80+
81+
endmenu
82+
endmenu

0 commit comments

Comments
 (0)