前言
對於在軟件調試過程中不想通過手機APP來操縱ESP8266連接路由器,可以通過配置ESP8266相關station參數來實現這一點。
相關數據類型介紹
一、station_config
成員名稱
數據類型
功能
ssid
uint8類型的數組
SSID
password
uint8類型的數組
密碼
bssid_set
uint8
-
bssid
uint8類型的數組
-
結構體原型
struct station_config
{
uint8 ssid[32];
uint8 password[64];
uint8 bssid_set; // Note: If bssid_set is 1, station will just connect to the router
// with both ssid[] and bssid[] matched. Please check about this.
uint8 bssid[6];
};
相關API介紹
一、wifi_station_get_config
功能
查詢 ESP8266 station 接口當前的配置
-
函數原型
bool wifi_station_get_config(struct station_config *config)
-
參數
*config
獲取到的station當前的配置
返回值
true
獲取配置成功
-
false
獲取配置失敗
二、wifi_station_set_config
功能
配置 ESP8266 station並保存到flash中
-
函數原型
bool wifi_station_set_config(struct station_config *config)
-
參數
*config
station的配置
返回值
true
配置成功
-
false
配置失敗
注意
需要在 ESP8266 station使能的情況下調用此API
-
三、 wifi_station_set_config_current
功能
配置 ESP8266 station,配置信息不保存到flash中
-
函數原型
bool wifi_station_set_config_current(struct station_config *config)
-
參數
*config
station的配置
返回值
true
配置成功
-
false
配置失敗
注意
需要在 ESP8266 station使能的情況下調用此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"
#define LINK_WIFI_SSID "WIFI_NAME"
#define LINK_WIFI_PASSWORD "WIFI_PASSWORD"
void ICACHE_FLASH_ATTR station_run( void )
{
struct station_config station_info;
uint8_t status = wifi_station_get_connect_status();
if ( status != STATION_CONNECTING
&& status != STATION_GOT_IP )
{
wifi_set_opmode( 0x03 );
wifi_station_get_config( &station_info );
os_memcpy( station_info.ssid, LINK_WIFI_SSID, os_strlen( LINK_WIFI_SSID ) );
os_memcpy( station_info.password, "WIFI_PASSWORD",os_strlen( "WIFI_PASSWORD" ) );
wifi_station_set_config( &station_info );
wifi_station_connect();
}
os_printf( " station status is %d \r\n!",status );
}
void user_init( void )
{
static os_timer_t station_timer;
os_timer_disarm( &station_timer );
os_timer_setfn( &station_timer, &station_run, NULL );
os_timer_arm( &station_timer, 10000, 1 );
}
參考資料
[1]. ESP8266Non-OS SDK API參考