54 ESP32Wifi模块之开启AP+STA共存及IP_NAPT模式

Posted Chasing_Chasing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了54 ESP32Wifi模块之开启AP+STA共存及IP_NAPT模式相关的知识,希望对你有一定的参考价值。

54.1 引言

探索完4G模块的应用、蓝牙模块的应用,最近开始了WiFi模块的使用。WiFi模块我们选择了广泛应用的esp32WiFi模组,所以就重新踏入了WiFi探索之旅。

然后项目上要用到WiFi的AP+STA模式,并且要开启NAT模式,就是WiFi模块能连接路由器,然后手机能连接WiFi模块,然后手机通过WiFi模块连接到路由器,能通过WiFi模块上网。

54.2 AP+STA+IP_NAPT代码

话不多说,先上app_main.c代码。这个代码也是网上搜集到的,自己理解后翻炒了一遍吧。。

#include <string.h>
#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 				0x08080808 // 8.8.8.8

//模块AP自己的SSID和PWD
#define HT_ESP32_AP_SSID 			CONFIG_ESP_AP_SSID
#define HT_ESP32_AP_PASS 			CONFIG_ESP_AP_PASSWORD
//连接路由的SSID和PWD
#define HT_ESP32_STA_SSID      		"hello"
#define HT_ESP32_STA_PASS      		"world"

#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();
    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 = htonl(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);


static void htHrint_task(void *pvParameters)

	while(1)
	
		ESP_LOGI(TAG, "hello world\\r\\n");
		vTaskDelay(1000 / portTICK_PERIOD_MS);	/* 延时1000ms*/
	



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
  u32_t napt_netif_ip = 0xC0A80401; // Set to ip address of softAP netif (Default is 192.168.4.1)
  ip_napt_enable(htonl(napt_netif_ip), 1);
  ESP_LOGI(TAG, "------------------------>NAT is enabled");
#endif

	xTaskCreate(htHrint_task, "HTTask", 4096, NULL, 5, NULL);



54.3 IP_NAPT模式配置

上述代码中,已经有开启IP_NAPT模式的重要几行代码,但需要关注的是,IP_NAPT这个宏是在哪里开启的,这是本blog的价值所在。

对于新手(像我一样新的应该是不知道),这个可以通过idf.py menuconfig 图形界面进行配置,这个配置在下面这个链接是有相关说明的。

Project Configuration - ESP32 - — ESP-IDF 编程指南 latest 文档

 由上述图片可知,需要配置两个宏才能打开nat的转换功能。

配置步骤:

1.使用命令打开配置页面,并进入component config

idf.py menuconfig

 2.在component config找到LWIP,然后进入

 3. 进入后选中下图的这两个

 4.保存,编译,并重新下载程序即可

over!

ESP32将继续探究中.........

以上是关于54 ESP32Wifi模块之开启AP+STA共存及IP_NAPT模式的主要内容,如果未能解决你的问题,请参考以下文章

请教怎样配置ESP8266的WiFi帐户及密码

ESP32作为接入点AP

esp32-智能语音-wifi

ESP32那些事儿:功能开发之蓝牙及WiFi功能

WIFI ESP8266模块输入AT指令 不回复OK

ESP32 开启 Wi-Fi 热点与手机端 Iperf 测试 APP 来测试 ESP32 Wi-Fi AP 速率的流程