ESP32/ESP8266中查看并修改DHCP的配置信息

1.DHCP 简介

DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理、分配IP地址,使网络环境中的主机动态的获得IP地址、Gateway地址、DNS服务器地址等信息,并能够提升IP地址的使用率。

简单来说,DHCP就是一个不需要账号密码登录的、自动给内网机器分配IP地址等信息的协议。它遵循服务器-客户端模型,在WiFi架构中,AP(即热点,提供连网功能的设备)中运行DHCPS(即DHCP-Server),而STA(即需要接入AP的设备)中运行DHCPC(即DHCP-Client)。通过如下图所示的连接过程,DHCPS将提供给DHCPC一个暂时租用的IP,以使得运行DHCPC的设备具备上网的能力。(注意,这个IP通常是以“租用”的方式给DHCPC使用,过段时间可能会回收再利用)

2.在ESP32中查看、修改DHCP的配置信息

DHCP的功能对于联网设备非常重要,ESP32的开发框架中提供了灵活的查看DHCP相关配置的接口,今天就来聊一聊如何使用这些接口。

不多说,上代码。(使用开发板为ESP32,SDK为ESP-IDF v3.1.7,开发模板为examples目录下的wifi/simple_wifi)

/* Simple WiFi Example*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.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;

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;

static const char *TAG = "simple wifi";

/* Use for DHCP */
tcpip_adapter_dhcp_status_t dhcp_get_status;
tcpip_adapter_option_id_t dhcp_op_id;
uint32_t op_len;
uint32_t op_val = -1;

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        ESP_LOGI(TAG, "got ip:%s",
                 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
        xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    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);
        /* Attention:you must call tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP) to start dhcps when the dhcps
        is stoped or it has not started yet,otherwise,you can not get the current option.Besides,you had better
        call the sizeof() to get the current op_len,otherwise,you also can not get the currect option.*/
        op_len = sizeof(dhcps_time_t);
        /* get the option*/
        tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME, &op_val, op_len);
        ESP_LOGI(TAG, "dhcp_get_option = %d",
             op_val);
        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:
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    default:
        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);
    /*DHCP option,1.get_option 2.get_status.3.get_option.4.set_option.5.get_option*/
    /*1.*/
    op_len = sizeof(dhcps_time_t);
    tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME, &op_val, op_len);
    ESP_LOGI(TAG, "dhcp_get_option = %d",
             op_val);
    /*2.*/
    ESP_ERROR_CHECK(tcpip_adapter_dhcps_get_status(TCPIP_ADAPTER_IF_AP, &dhcp_get_status));
    ESP_LOGI(TAG, "dhcp_get_status = %d",
             dhcp_get_status);
    /*stop dhcps*/
    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
    /*2.*/
    ESP_ERROR_CHECK(tcpip_adapter_dhcps_get_status(TCPIP_ADAPTER_IF_AP, &dhcp_get_status));
    ESP_LOGI(TAG, "dhcp_get_status = %d",
             dhcp_get_status);

    /*3.get the option*/
    /* 3.1 Attention:you must call tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP) to start dhcps when the dhcps
    is stoped or it has not started yet,otherwise,you can not get the current option.Besides,you had better
    call the sizeof() to get the current op_len,otherwise,you also can not get the currect option.*/
    op_len = sizeof(dhcps_time_t);
    tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
    /* 3.2 now ,you can get the option*/
    tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME, &op_val, op_len);
    ESP_LOGI(TAG, "dhcp_get_option = %d",
             op_val);
    /* 4.change the option,and set the option*/
    /*4.1 change the option value*/
    op_val = 110;
    op_len = sizeof(dhcps_time_t);
    /*4.2 Attention:you must ensure that dhcps has been stoped,and then you can set the option*/
    tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
    tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_SET, TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME, &op_val, op_len);
    /*5.get the option after changing the option,please start the dhcps before get the option*/
    tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
    tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME, &op_val, op_len);
    ESP_LOGI(TAG, "dhcp_get_option = %d",
             op_val);
    /*you cann't try to get the Domain_name,because it's nothing*/
    // char op_dhcp_name[64];
    // op_len = sizeof(dhcps_offer_t);
    // tcpip_adapter_dhcps_option(TCPIP_ADAPTER_OP_GET, TCPIP_ADAPTER_DOMAIN_NAME_SERVER, op_dhcp_name, op_len);
    // ESP_LOGI(TAG, "dhcp_get_option = %s",op_dhcp_name);
   
}

void wifi_init_sta()
{
    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 = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS
        },
    };

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG, "wifi_init_sta finished.");
    ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}

void app_main()
{
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      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();
#else
    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/

}

代码实现上,主要是在wifi_init_softap()中添加一系列操作DHCPS的代码,这些代码我添加了注释,可以帮助理解。特别提醒,ESP32应工作在AP模式下(因为,如你所见,我仅在AP的初始化代码wifi_init_softap()中添加了对DHCP的操作),因此在编译时请通过menuconfig将ESP32开发板配置为AP工作模式。

需要提醒的是:

在读取DHCPS的option(即配置选项)时,必须在DHCPS启动状态下读取,并且传入的option_len(即选项长度)要获取正确,再传入,否则会读取失败。

在设置DHCPS的option时,必须在关闭DHCPS的状态下,进行设置,同样的,必须传入正确的option_len(即选项长度)参数,否则设置失败。

设置成功后应当先使用tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);重新启动DHCPS,才能进行查询。

3.实验结果与分析

WiFi初始化完成后,将自动启动DHCP,实验中获取的option(即配置参数)为 TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME,即DHCPS分配给DHCPC的IP的租用时间(当然,你也可以获取DHCP的其他配置参数,原理是一样的,这里是举个例子)。

上面的打印信息依次是:

1.原来的租用时间为120min(是的,我查了原文件,初始化配置为120分钟的有效租用时间)

2.DHCPS当前状态为1,即启动状态(可以查看源文件:1代表的宏是Start状态)

3.DHCPS当前状态为2,即停止状态(因为我前面调用了Stop()函数)

4.再次查看修改前的option为120min

5.查看修改后的option为110min,即完成修改。

(最后,ESP8266提供了上述操作DHCP的接口,但函数的名字和传的参数类型与ESP32有所不同,因此,若是使用ESP8266作为实验平台,请注意调整对应的函数名和传入参数。原理与注意事项是一模一样的)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值