更改ESP8266相关路由信息

前言

ESP8266上电的时候可以通过手机的WIFI看到ESP8266的SSID,但是因为加了密码而连接不上,用户可以通过对ESP8266编程来更改ESP8266的SSID以及密码。

相关数据类型介绍

一、softap_config

成员名称 数据类型 功能
ssid uint8大小的数组 WIFI AP SSID
password uint8大小的数组 WIFI AP 密码
ssid_len uint8 SSID 长度
channel uint8 通道
authmode AUTH_MODE -
ssid_hidden uint8 -
max_connection uint8 -
beacon_interval uint16 -

结构体原型

struct softap_config 
{
    uint8 ssid[32];
    uint8 password[64];
    uint8 ssid_len; // Note: Recommend to set it according to your ssid
    uint8 channel;  // Note: support 1 ~ 13
    AUTH_MODE authmode; // Note: Don't support AUTH_WEP in softAP mode.
    uint8 ssid_hidden;  // Note: default 0
    uint8 max_connection;   // Note: default 4, max 4
    uint16 beacon_interval; // Note: support 100 ~ 60000 ms, default 100
};

相关API介绍

一、wifi_softap_get_config

功能 查询 ESP8266 WiFi soft-AP 接口当前的配置 -
函数原型 bool wifi_softap_get_config(struct softap_config *config) -
参数 *config 获取到的soft-ap当前的配置
返回值 true 获取配置成功
- false 获取配置失败

二、wifi_softap_set_config

功能 配置 ESP8266 WiFi soft-AP并保存到flash中 -
函数原型 bool wifi_softap_set_config(struct softap_config *config) -
参数 *config soft-ap的配置
返回值 true 配置成功
- false 配置失败
注意 需要在 ESP8266 soft_AP使能的情况下调用此API -

三、 wifi_softap_set_config_current

功能 配置 ESP8266 WiFi soft-AP不保存到flash中 -
函数原型 bool wifi_softap_set_config_current(struct softap_config *config) -
参数 *config soft-ap的配置
返回值 true 配置成功
- false 配置失败
注意 需要在 ESP8266 soft_AP使能的情况下调用此API -

相关例程

#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "uart.h"
#include "gpio.h"
#include "app_socket.h"
#include "string.h"

void user_init( void )
{
    struct softap_config config;

    /** 初始化ESP8266工作模式 */
    if ( wifi_get_opmode() != 0x03 )
    {
        wifi_set_opmode( 0x03 );
    }

    /** 获取当前soft-AP配置 */
    wifi_softap_get_config( &config );

    /** 清零参数 */
    os_memset( config.ssid, 0, 32 );
    os_memset( config.password, 0, 64 );

    /** 写入数据 */
    os_memcpy( config.ssid, "SMART_SOCKET_ID", os_strlen( "SMART_SOCKET_ID" ) );
    os_memcpy( config.password, "12345678", os_strlen( "12345678" ) );

    config.authmode = AUTH_WPA_WPA2_PSK;
    config.ssid_len = 0;  // or its actual length
    config.beacon_interval = 100;
    config.max_connection = 4;  // how many stations can connect to ESP8266 softAP at most.

    /** 写入配置 */
    wifi_softap_set_config( &config );
}

参考资料

[1]. ESP8266Non-OS SDK API参考

猜你喜欢

转载自blog.csdn.net/qq_15647227/article/details/52349853
今日推荐