menuconfig打开CONFIG_LWIP_IPV4_NAPT
中继的代码如下:
#include
#include
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "lwip/opt.h"
#if IP_NAPT
#include "lwip/lwip_napt.h"
#endif
#include "lwip/err.h"
#include "lwip/sys.h"
#define MY_DNS_IP_ADDR "8.8.8.8"
#define MY_AP_IP_ADDR "10.0.1.1"
//模块AP自己的SSID和PWD
#define HT_ESP32_AP_SSID "esp-ap"
#define HT_ESP32_AP_PASS "88888888"
//连接路由的SSID和PWD
#define HT_ESP32_STA_SSID "source-ap"
#define HT_ESP32_STA_PASS "88888888"
#define HT_ESP32_MAXIMUM_RETRY (10)
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_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 WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "wifi apsta";
static int s_retry_num = 0;
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:
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED: {
if (s_retry_num < HT_ESP32_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG, "connect to the AP failed:%d", event->event_info.disconnected.reason);
break;
}
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station connected");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station disconnected");
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta() {
ip_addr_t dnsserver;
//tcpip_adapter_dns_info_t dnsinfo;
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
// 配置AP的ip地址
tcpip_adapter_ip_info_t ip_info = {
.ip.addr = ipaddr_addr(MY_AP_IP_ADDR),
.netmask.addr = ipaddr_addr("255.255.255.0"),
.gw.addr = ipaddr_addr(MY_AP_IP_ADDR),
};
ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info));
ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
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));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
/* ESP STATION CONFIG */
wifi_config_t sta_config = {
.sta = {
.ssid = HT_ESP32_STA_SSID,
.password = HT_ESP32_STA_PASS,
/* 设置密码意味着工作站将连接到所有安全模式,包括WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.bssid_set = false,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
/* ESP AP CONFIG */
wifi_config_t ap_config = {
.ap = {
.ssid = HT_ESP32_AP_SSID,
.channel = 0,
.authmode = WIFI_AUTH_WPA2_PSK,
.password = HT_ESP32_AP_PASS,
.ssid_hidden = 0,
.max_connection = 4,
.beacon_interval = 100
}
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config));
// Enable DNS (offer) for dhcp server
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
dhcps_set_option_info(6, &dhcps_dns_value, sizeof(dhcps_dns_value));
// Set custom dns server address for dhcp server
dnsserver.u_addr.ip4.addr = ipaddr_addr(MY_DNS_IP_ADDR);
dnsserver.type = IPADDR_TYPE_V4;
dhcps_dns_setserver(&dnsserver);
//tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dnsinfo);
// ESP_LOGI(TAG, "DNS IP:" IPSTR, IP2STR(&dnsinfo.ip.u_addr.ip4));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_apsta finished.");
ESP_LOGI(TAG, "connect to ap SSID: %s ", sta_config.sta.ssid);
}
void app_main() {
// 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);
// Setup WIFI
wifi_init_sta();
//这里就是打开nat转换的代码,但是除了代码这里要有,还需要配置
#if IP_NAPT
ip_napt_enable(ipaddr_addr(MY_AP_IP_ADDR), 1);
ESP_LOGI(TAG, "------------------------>NAT is enabled");
#endif
}
补一份cpp代码
#include
#include
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "lwip/opt.h"
#if IP_NAPT
#include "lwip/lwip_napt.h"
#endif
#include "lwip/err.h"
#include "lwip/sys.h"
#define MY_DNS_IP_ADDR "8.8.8.8"
#define MY_AP_IP_ADDR "10.0.1.1"
#define MY_AP_CHANNEL 3
//模块AP自己的SSID和PWD
#define HT_ESP32_AP_SSID "esp-ap"
#define HT_ESP32_AP_PASS "88888888"
//连接路由的SSID和PWD
#define HT_ESP32_STA_SSID "Wireless 2.4G"
#define HT_ESP32_STA_PASS "66666666"
#define HT_ESP32_MAXIMUM_RETRY (10)
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_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 WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "wifi apsta";
static int s_retry_num = 0;
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:
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED: {
if (s_retry_num < HT_ESP32_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG, "connect to the AP failed:%d", event->event_info.disconnected.reason);
break;
}
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station connected");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station disconnected");
break;
default:
break;
}
return ESP_OK;
}
void wifi_init_sta() {
//tcpip_adapter_dns_info_t dnsinfo;
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
// 配置AP的ip地址
tcpip_adapter_ip_info_t ip_info;
ip_info.ip.addr = ipaddr_addr(MY_AP_IP_ADDR);
ip_info.netmask.addr = ipaddr_addr("255.255.255.0");
ip_info.gw.addr = ipaddr_addr(MY_AP_IP_ADDR);
ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info));
ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
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));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
/* ESP STATION CONFIG */
wifi_config_t sta_config;
strcpy((char *) sta_config.sta.ssid, HT_ESP32_STA_SSID);
strcpy((char *) sta_config.sta.password, HT_ESP32_STA_PASS);
// memcpy(sta_config.sta.ssid, HT_ESP32_STA_SSID, strlen(HT_ESP32_STA_SSID));
// memcpy(sta_config.sta.password, HT_ESP32_STA_PASS, strlen(HT_ESP32_STA_PASS));
/* 设置密码意味着工作站将连接到所有安全模式,包括WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
sta_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
sta_config.sta.bssid_set = false;
sta_config.sta.pmf_cfg.capable = true;
sta_config.sta.pmf_cfg.required = false;
/* ESP AP CONFIG */
wifi_config_t ap_config;
strcpy((char *) ap_config.ap.ssid, HT_ESP32_AP_SSID);
strcpy((char *) ap_config.ap.password, HT_ESP32_AP_PASS);
// memcpy(ap_config.ap.ssid, HT_ESP32_AP_SSID, strlen(HT_ESP32_AP_SSID));
// memcpy(ap_config.ap.password, HT_ESP32_AP_PASS, strlen(HT_ESP32_AP_PASS));
ap_config.ap.ssid_len = strlen(HT_ESP32_AP_SSID);
ap_config.ap.channel = MY_AP_CHANNEL;
ap_config.ap.authmode = WIFI_AUTH_WPA2_PSK;
ap_config.ap.ssid_hidden = 0;
ap_config.ap.max_connection = 4;
ap_config.ap.beacon_interval = 100;
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
// Enable DNS (offer) for dhcp server
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
dhcps_set_option_info(6, &dhcps_dns_value, sizeof(dhcps_dns_value));
// Set custom dns server address for dhcp server
ip_addr_t dnsserver;
dnsserver.addr = ipaddr_addr(MY_DNS_IP_ADDR);
dhcps_dns_setserver(&dnsserver);
//tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dnsinfo);
// ESP_LOGI(TAG, "DNS IP:" IPSTR, IP2STR(&dnsinfo.ip.u_addr.ip4));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_apsta finished.");
ESP_LOGI(TAG, "connect to ap SSID: %s ", sta_config.sta.ssid);
}
extern "C" void app_main() {
// 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);
// Setup WIFI
wifi_init_sta();
//这里就是打开nat转换的代码,但是除了代码这里要有,还需要配置
#if IP_NAPT
ip_napt_enable(ipaddr_addr(MY_AP_IP_ADDR), 1);
ESP_LOGI(TAG, "------------------------>NAT is enabled");
#endif
}