• [ESP32][esp-idf] AP+STA实现无线桥接(中转wifi信号)


    引言:无线中继的主要作用是扩展WiFi信号,扩展WiFi后,令原本覆盖不到WiFi的地方也可以有WiFi,免去布线的烦恼。两个无线路由器桥接到一起,可以实现WiFi信号全覆盖。

    实现的功能:WiFi模块能连接路由器,然后手机能连接WiFi模块,然后手机通过WiFi模块连接到路由器,能通过WiFi模块上网。
    在这里插入图片描述

    配置宏

    # Set up wifi hotspot and share network
    CONFIG_LWIP_IP_FORWARD=y
    CONFIG_LWIP_IPV4_NAPT=y
    
    • 1
    • 2
    • 3

    demo.c代码如下:

    #include 
    #include 
    
    #include "esp_event.h"
    #include "esp_log.h"
    #include "esp_system.h"
    
    #include "nvs_flash.h"
    #include "esp_wifi.h"
    #include "esp_netif.h"
    #include "lwip/inet.h"
    #include "lwip/lwip_napt.h"
    
    // 热点名称 密码  可连接数量
    #define AP_WIFI_SSID		"lisun"
    #define AP_WIFI_PASS		"xjq12345"
    #define AP_MAX_STA_CONN		4
    
    // 路由器wifi名称 密码
    #define STA_WIFI_SSID		"xjq"
    #define STA_WIFI_PASS		"xjq12345"
    
    static const char *TAG = "LiSun";
    
    static esp_netif_t* _esp_netif_sta = NULL;
    static esp_netif_t* _esp_netif_ap = NULL;
    
    static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
    	if (event_id == WIFI_EVENT_AP_STACONNECTED) {
    		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);
    	} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
    		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 sta_start_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
    	esp_wifi_connect();
    }
    
    static void got_ip_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
    	//No need to log, wifi driver logs automatically
    	esp_netif_dns_info_t dns;
    	if (esp_netif_get_dns_info(_esp_netif_sta, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK) {
    		dhcps_dns_setserver((const ip_addr_t *)&dns.ip);
    		ESP_LOGI(TAG, "set dns to:" IPSTR, IP2STR(&dns.ip.u_addr.ip4));
    	}
    }
    
    static void wifi_init_softap(void)
    {
    	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    	ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    
    	ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
    
    	wifi_config_t wifi_config = {
    		.ap = {
    			.ssid = AP_WIFI_SSID,
    			.ssid_len = strlen(AP_WIFI_SSID),
    			.password = AP_WIFI_PASS,
    			.max_connection = AP_MAX_STA_CONN,
    			.authmode = WIFI_AUTH_WPA_WPA2_PSK
    		},
    	};
    	if (strlen(AP_WIFI_PASS) == 0)
    		wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    
    	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
    	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
    
    	wifi_config_t cfg1 = {
    		.sta = {
    			.ssid = STA_WIFI_SSID,
    			.password = STA_WIFI_PASS,
    			.threshold = {.rssi=0, .authmode = WIFI_AUTH_WPA2_PSK},
    			.pmf_cfg = {
    				.capable = true,
    				.required = false
    			},
    		},
    	};
    	ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &cfg1));
    	ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, sta_start_handler, NULL));
    	ESP_ERROR_CHECK(esp_wifi_start());
    
    	esp_netif_ip_info_t ip_info;
    	esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ip_info);
    
    	char ip_addr[16];
    	inet_ntoa_r(ip_info.ip.addr, ip_addr, 16);
    	ESP_LOGI(TAG, "Set up softAP with IP: %s", ip_addr);
    
    	ESP_LOGI(TAG, "wifi_init_softap finished. SSID:'%s' password:'%s'", AP_WIFI_SSID, AP_WIFI_PASS);
    }
    
    
    void app_main(void)
    {
    	// Initialize networking stack
    	ESP_ERROR_CHECK(esp_netif_init());
    
    	// Create default event loop needed by the  main app
    	ESP_ERROR_CHECK(esp_event_loop_create_default());
    
    	// Initialize NVS needed by Wi-Fi
    	ESP_ERROR_CHECK(nvs_flash_init());
    
    	// Initialize Wi-Fi including netif with default config
    	_esp_netif_ap = esp_netif_create_default_wifi_ap();
    	_esp_netif_sta = esp_netif_create_default_wifi_sta();
    
    	// Initialise ESP32 in SoftAP mode
    	wifi_init_softap();
    
    	ip_addr_t dnsserver;
    	// 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 默认跟随路由器 【推荐换成国内DNS】
    	dnsserver.u_addr.ip4.addr = htonl(0xC0A80301);
    	dnsserver.type = IPADDR_TYPE_V4;
    	dhcps_dns_setserver(&dnsserver);
    
    #if IP_NAPT
    	// !!! 必须启动sta后再设置,不然ap无网络 !!! Set to ip address of softAP netif (Default is 192.168.4.1)
    	u32_t napt_netif_ip = 0xC0A80401;
    	ip_napt_enable(htonl(napt_netif_ip), 1);
    #endif
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
  • 相关阅读:
    218. 扑克牌 - 记忆化概率dp
    nginx热备配置
    春秋云境靶场CVE-2022-32991漏洞复现(sql手工注入)
    C专家编程 第7章 对内存的思考 7.6 内存泄漏
    【计算机网络】介质访问控制
    自定义映射resultMap
    vscode高效之代码片段
    JSP总结
    MongoDB语言命令
    如何才能做好单元测试优化?
  • 原文地址:https://blog.csdn.net/qq_29246181/article/details/126007905