esp8266 wifi AP模式下获取IP地址并修改为新的IP

设置/获取 SoftAP IP

/* Simple WiFi Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "rom/ets_sys.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

/* The examples use simple WiFi configuration that you can set via
   'make menuconfig'.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_MODE_AP   CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA
#define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_MAX_STA_CONN       CONFIG_MAX_STA_CONN

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t wifi_event_group;

tcpip_adapter_ip_info_t local_ip;
int res_ap_get;
int res_ap_set;
//struct tcpip_adapter_ip_info_t info_AP;
//static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX];
static tcpip_adapter_ip_info_t info_AP[TCPIP_ADAPTER_IF_MAX];

const int WIFI_CONNECTED_BIT = BIT0;

static const char *TAG = "wifi_ap";

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
  
    case SYSTEM_EVENT_AP_STACONNECTED:
        
        ESP_LOGI(TAG, "=========station:"MACSTR" join, AID=%d",
                 MAC2STR(event->event_info.sta_connected.mac),
                 event->event_info.sta_connected.aid);
        break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:
        ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d",
                 MAC2STR(event->event_info.sta_disconnected.mac),
                 event->event_info.sta_disconnected.aid);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        printf("SYSTEM_EVENT_STA_DISCONNECTED==%d",event->event_id);
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    default:
        printf("default==%d",event->event_id);
        break;
    }
    return ESP_OK;
}

void wifi_init_softap()
{
    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));
    wifi_config_t wifi_config={
        .ap={
            .ssid=EXAMPLE_ESP_WIFI_SSID,
            .ssid_len=strlen(EXAMPLE_ESP_WIFI_SSID),
            .password=EXAMPLE_ESP_WIFI_PASS,
            .max_connection=EXAMPLE_MAX_STA_CONN,
            .authmode=WIFI_AUTH_WPA_WPA2_PSK
        },
    };
    if(strlen(EXAMPLE_ESP_WIFI_PASS)==0){
        wifi_config.ap.authmode=WIFI_AUTH_OPEN;
    }
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP,&wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);

    res_ap_get = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);
    printf("self:"IPSTR"\n",IP2STR(&local_ip.ip));
    printf("self:"IPSTR"\n",IP2STR(&local_ip.netmask));
    printf("self:"IPSTR"\n",IP2STR(&local_ip.gw));

    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); 
    //memset(local_ip, 0, sizeof(tcpip_adapter_ip_info_t));

    IP4_ADDR(&local_ip.ip, 192, 168 , 5, 1);
    IP4_ADDR(&local_ip.gw, 192, 168 , 5, 1);
    IP4_ADDR(&local_ip.netmask, 255, 255 , 255, 0);

    printf("set:"IPSTR"\n",IP2STR(&local_ip.ip));
    printf("set:"IPSTR"\n",IP2STR(&local_ip.netmask));
    printf("set:"IPSTR"\n",IP2STR(&local_ip.gw));

    res_ap_set = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);


    tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
}


void app_main()
{
    
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    
#if EXAMPLE_ESP_WIFI_MODE_AP
    ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
    wifi_init_softap();

// Free it by calling functions
#else
    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/

}

(1)读取AP的ip地址

tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);

(2)关闭dhcps

tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); 

(3)修改IP地址

tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);

(4)启动dhcps

 tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);

流程为:(完整代码见上方)

   res_ap_get = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);
    printf("self:"IPSTR"\n",IP2STR(&local_ip.ip));
    printf("self:"IPSTR"\n",IP2STR(&local_ip.netmask));
    printf("self:"IPSTR"\n",IP2STR(&local_ip.gw));

    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); 
    //memset(local_ip, 0, sizeof(tcpip_adapter_ip_info_t));

    IP4_ADDR(&local_ip.ip, 192, 168 , 5, 1);
    IP4_ADDR(&local_ip.gw, 192, 168 , 5, 1);
    IP4_ADDR(&local_ip.netmask, 255, 255 , 255, 0);

    printf("set:"IPSTR"\n",IP2STR(&local_ip.ip));
    printf("set:"IPSTR"\n",IP2STR(&local_ip.netmask));
    printf("set:"IPSTR"\n",IP2STR(&local_ip.gw));

    res_ap_set = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &local_ip);


    tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);

你可能感兴趣的:(嵌入式)