ESP-Mesh-Lite 可以使用静态IP模式吗?
Posted: Tue Nov 28, 2023 8:08 am
在esp_bridge_create_station_netif函数中关闭DHCP,设置IP后,无法进行组网,只有一个节点可以连接到服务器上。如果想使用静态IP的方式连接WiFi应该怎样处理?谢谢!
还有在多节点同时上电连接路由器时,会出现无法组成一个整体网络的情况,会有多个节点直接连接路由器,这个情况应该怎样避免?
esp_netif_t* esp_bridge_create_station_netif(esp_netif_ip_info_t* ip_info, uint8_t mac[6], bool data_forwarding, bool enable_dhcps)
{
esp_netif_t* wifi_netif = NULL;
wifi_mode_t mode = WIFI_MODE_NULL;
if (data_forwarding || enable_dhcps) {
return wifi_netif;
}
esp_bridge_wifi_init();
wifi_netif = esp_netif_create_default_wifi_sta();
esp_netif_dhcpc_stop(wifi_netif);
esp_netif_ip_info_t ip_infos;
memset(&ip_infos, 0, sizeof(esp_netif_ip_info_t));
char static_ip_add[17] = {"192.168.3.149"};
char static_gw_add[17] = {"192.168.3.1"};
char static_netmask_add[17] = {"255.255.255.0"};
ip_infos.ip.addr = ipaddr_addr(static_ip_add);
ip_infos.gw.addr = ipaddr_addr(static_gw_add);
ip_infos.netmask.addr = ipaddr_addr(static_netmask_add);
esp_netif_set_ip_info(wifi_netif, &ip_infos);
esp_bridge_netif_list_add(wifi_netif, NULL);
esp_wifi_get_mode(&mode);
mode |= WIFI_MODE_STA;
ESP_ERROR_CHECK(esp_wifi_set_mode(mode));
wifi_sta_config_t router_config;
/* Get WiFi Station configuration */
esp_wifi_get_config(WIFI_IF_STA, (wifi_config_t*)&router_config);
/* Get Wi-Fi Station ssid success */
if (strlen((const char*)router_config.ssid)) {
ESP_LOGI(TAG, "Found ssid %s", (const char*)router_config.ssid);
ESP_LOGI(TAG, "Found password %s", (const char*)router_config.password);
}
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &wifi_event_sta_disconnected_handler, NULL, NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_sta_got_ip_handler, NULL, NULL));
return wifi_netif;
}
还有在多节点同时上电连接路由器时,会出现无法组成一个整体网络的情况,会有多个节点直接连接路由器,这个情况应该怎样避免?
esp_netif_t* esp_bridge_create_station_netif(esp_netif_ip_info_t* ip_info, uint8_t mac[6], bool data_forwarding, bool enable_dhcps)
{
esp_netif_t* wifi_netif = NULL;
wifi_mode_t mode = WIFI_MODE_NULL;
if (data_forwarding || enable_dhcps) {
return wifi_netif;
}
esp_bridge_wifi_init();
wifi_netif = esp_netif_create_default_wifi_sta();
esp_netif_dhcpc_stop(wifi_netif);
esp_netif_ip_info_t ip_infos;
memset(&ip_infos, 0, sizeof(esp_netif_ip_info_t));
char static_ip_add[17] = {"192.168.3.149"};
char static_gw_add[17] = {"192.168.3.1"};
char static_netmask_add[17] = {"255.255.255.0"};
ip_infos.ip.addr = ipaddr_addr(static_ip_add);
ip_infos.gw.addr = ipaddr_addr(static_gw_add);
ip_infos.netmask.addr = ipaddr_addr(static_netmask_add);
esp_netif_set_ip_info(wifi_netif, &ip_infos);
esp_bridge_netif_list_add(wifi_netif, NULL);
esp_wifi_get_mode(&mode);
mode |= WIFI_MODE_STA;
ESP_ERROR_CHECK(esp_wifi_set_mode(mode));
wifi_sta_config_t router_config;
/* Get WiFi Station configuration */
esp_wifi_get_config(WIFI_IF_STA, (wifi_config_t*)&router_config);
/* Get Wi-Fi Station ssid success */
if (strlen((const char*)router_config.ssid)) {
ESP_LOGI(TAG, "Found ssid %s", (const char*)router_config.ssid);
ESP_LOGI(TAG, "Found password %s", (const char*)router_config.password);
}
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &wifi_event_sta_disconnected_handler, NULL, NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_sta_got_ip_handler, NULL, NULL));
return wifi_netif;
}