当前位置:网站首页>[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号
2022-07-28 09:35:00 【jianqiang.xue】
引言:无线中继的主要作用是扩展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
demo.c代码如下:
#include <string.h>
#include <sys/param.h>
#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
}
边栏推荐
- SeekTiger生态通证STI 新进展,4月14日登录 ZB
- Symbolic operation of MATLAB
- 超级原始人系列盲盒即将上线,PlatoFarm赋能超多权益
- PHP连接mysql原生代码
- Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
- Window源码解析(二):Window的添加机制
- 今天和大家聊一聊mysql数据库的数据类型
- 3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities
- 在Plato Farm新经济模型下,如何在游戏中获取更多MARK
- Opencv installation configuration test
猜你喜欢

MySQL master-slave architecture. After the master database is suspended and restarted, how can the slave database automatically connect to the master database

软件测试与质量学习笔记2----黑盒测试

OSS直连上传-Rails服务实践

老板:公司系统太多,能不能实现账号互通?

Seektiger eco pass STI new progress, log in to ZB on April 14

2022-7-27周报

Net 3 lines of code to realize the function of text to speech

3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力

C# 读写文件从用户态切到内核态,到底是个什么流程?

Pycharm uses CONDA to call the remote server
随机推荐
Introduction to SD card (based on spec3.0)
Software testing and quality learning notes 2 - black box testing
Word segmentation results of ES query index fields
Several innovative economic models of platofarm have inspired the current metacosmic market
Method parameter transfer mechanism of C #
Detailed explanation of various types of files in MySQL
在Plato Farm新经济模型下,如何在游戏中获取更多MARK
常用工具函数 持续更新
Array method of J S, loop
C countdown tool
3分钟带你了解微信小程序开发
How to learn so many conceptual things in database? Seeking method
PlatoFarm几大创新经济模型,给予当下元宇宙市场的启发
The secret behind three salary increases a year
Real time editor of MATLAB
Window source code analysis (I): things with decorview
Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble
Window source code analysis (IV): window deletion mechanism
备受关注的Bit.Store,最新动态一览
Sequence and limit operation of MATLAB