引言:无线中继的主要作用是扩展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 #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 }
上一篇:[ESP32][esp-idf] https和mqtt同时签名冲突解决
下一篇:[ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败
推荐阅读最新更新时间:2024-11-20 10:53
设计资源 培训 开发板 精华推荐
- LT1910 的典型应用 - 受保护的高侧 MOSFET 驱动器
- ESP8266
- 使用 MaxLinear, Inc 的 SPX29303U 的参考设计
- CPU复位电路AZ7033ZTR电压检测器典型应用电路
- TA78DL05BS低压降稳压器典型应用
- 调光灯STM8
- DM163003,PIC16C9XX 系列微控制器的 PICDEM 3 演示板
- DER-957 - 使用基于 PowiGaN 的 InnoSwitch4-CZ、ClampZero 和 HiperPFS-5 的 130 W USB PD 3.1 扩展功率范围充电器
- 使用 ams AG 的 AS1312-BTDT-50 的参考设计
- 【创意PCB】月亮网络时钟