bool wifi_station_set_config (struct station_config *config)
struct station_config *config
:Wi-Fi Station 接⼝配置参数指针void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
char ssid[32] = SSID;
char password[64] = PASSWORD;
struct station_config stationConf;
stationConf.bssid_set = 0; //need not check MAC address of AP
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 64);
wifi_station_set_config(&stationConf);
}
void user_init(void)
{
wifi_set_opmode(STATIONAP_MODE); //Set softAP + station mode
user_set_station_config();
}
wifi_station_set_config
在 user_init
中调用,则 ESP8266 Station 接口会在系统初始化完成后,自动连接 AP(路由),无需再调用 wifi_station_connect
wifi_station_connect
连接 AP(路由)。station_config.bssid_set
⼀般设置为 0 ,仅当需要检查 AP 的 MAC 地址时(多用于有重名 AP 的情况下)设置为 1。bool wifi_station_set_config_current (struct station_config *config)
struct station_config *config
:Wi-Fi Station 接⼝配置参数指针bool wifi_station_connect (void)
bool wifi_station_disconnect (void)
wifi_station_set_reconnect_policy
关闭重连功能,且未调用wifi_set_event_handler_cb
注册 Wi-Fi 事件回调,则本接口失效,无法准确获得连接状态。uint8 wifi_station_get_connect_status (void)
enum{
STATION_IDLE = 0,
STATION_CONNECTING,
STATION_WRONG_PASSWORD,
STATION_NO_AP_FOUND,
STATION_CONNECT_FAIL,
STATION_GOT_IP
};
user_init
中调⽤本接口,本接口必须在系统初始化完成后,并且 ESP8266 Station 接口使能的情况下调用。bool wifi_station_scan (struct scan_config *config, scan_done_cb_t cb);
struct scan_config {
uint8 *ssid; // AP’s ssid
uint8 *bssid; // AP’s bssid
uint8 channel; //scan a specific channel
uint8 show_hidden; //scan APs of which ssid is hidden.
wifi_scan_type_t scan_type; // scan type, active or passive
wifi_scan_time_t scan_time; // scan time per channel
};
参数:
• struct scan_config *config:扫描 AP 的配置参数
wifi_station_scan
的回调函数user_init
中调⽤本接口,本接口必须在系统初始化完成后,并且 ESP8266 Station 接口使能的情况下调用。void scan_done_cb_t (void *arg, STATUS status)
void *arg
:扫描获取到的 AP 信息指针,以链表形式存储,数据结构 struct bss_info
STATUS status
:扫描结果wifi_station_scan(&config, scan_done);
static void ICACHE_FLASH_ATTR scan_done(void *arg, STATUS status) {
if (status == OK) {
struct bss_info *bss_link = (struct bss_info *)arg;
...
}
}
bool wifi_station_ap_number_set (uint8 ap_number)
uint8 ap_number
:记录 AP 信息的最⼤数⽬(最⼤值为 5)uint8 wifi_station_get_ap_info(struct station_config config[])
struct station_config config[]
:AP 的信息,数组大小必须为 5 struct station_config config[5];
int i = wifi_station_get_ap_info(config);
uint8 wifi_station_get_current_ap_id ();
bool wifi_station_set_auto_connect(uint8 set)
// OLED初始化
OLED_ Init();
OLED_ ShowStr ing(0, 0, "ESP8266 = AP") ;
OLED_ ShowString(0, 2, "IP:") ;
void ICACHE_ FLASH_ ATTR ESP8266 AP Init JX()
{
struct softap_ config AP_ Config;// AP参数结构体
wifi_ set_ opmode (0x02) ;//设置为AP模式,并保存到Flash
//结构体赋值(注意: [ 服务集标识符/密码]须设为字符串形式)
os_ memset (&AP_ Config, 0,sizeof (struct softap_ config)); // AP参数结构体= 0
os_ strcpy (AP_ Config. ssid, ESP8266_ AP_ SSID); // 设置SSID (将字符串复制到ssid数组)
os_ strcpy (AP_ Conf ig. password, ESP8266_ AP_ PASS); // 设置密码(将字符串复制到passwor d数组)
AP_ Config. ssid_ len=os_ str len (ESP8266_ AP_ SSID); // 设置ss id长度(和SS ID的长度一致)
AP_ Config. channel=1;//通道号1~13
AP_ Config. authmode=AUTH WPA2_ PSK;//通道号1~13
AP_ Config. ssid_ bidden=0;//不隐藏SSID
AP_ Conf ig. max_ cmnnection=4;//最大连接数
AP_ Conf ig. beacon_ interval=100;//信标间隔时槽100~60000 ms
wifi_ softap_ set_ config (&AP_ Config) ;//设置soft-AP,并保存到Flash
}
结构体得成员:
struct softap config
{
uint8 ssid[32];//存放wifi名
uint8 password [64] ;//wifi密码
uint8 ssid_len;//wifi名长度
uint8 channel ;//通道号
AUTH_ MODE authmode ;//加密方式
uint8 ssid_ hidden;//是否隐藏wifi名
uint8 max_ connection;//最大连接数
uint16 beacon_ interval ;//信标间隔时槽
}
注意:
wifi名和wifi密码使用宏定义
sizeof(…):返回参数所占空间的大小(以字节为单位)
os memset(void *s,int ch, size_ t n)在一段内存块中填充某个指定值
void *s 内存块指针
int ch 填充值
size_t n.填充大小
char os_ strcpy(char *s1, const char *s2)将参数2指向的”字符串”复制到参数1指向的地址处
void ICACHE_ FLASH_ ATTR 0S_ Timer_ 1_ cb(void)
{
struct ip_ info ST_ ESP8266_ IP; // IP信息结构体
u8 ESP8266_ IP[4]; //点分十进制形式保存IP
//查询并打印ESP8266的工作模式
switch (wifi_ get_ _opmode()) // 输出工作模式
{
case 0x01: os_ pr intf ("\nESP8266_ Mode = Stat ion\n") ;
case 0x02: os_ _pr intf ("\nESP8266_ Mode = SoftAP\n") ;
case 0x03: os_ pr intf ("\nESP8266_ Mode = Stat i on+SoftAP\n") ; break;
}
//获取ESP8266_ AP模式下的IP地址
// [AP模式下,如果开启DHCP (默认),并且未设置IP相关参数,ESP8266的 IP地址=192.168.4.1]
wifi_ get_ ip_ info (SOFTAP_ IF, &ST_ ESP8266_ IP); //参数2: IP信息结构体指针
// ESP8266_ AP_ IP. ip. addr==32位二进制IP地址,将它转换为点分十进制的形式。
ESP8266_ IP[0] = ST_ ESP8266_ IP. ip. addr ;//点分十进制IP的第- -个数<==> addr低八位
ESP8266_ IP[1] = ST_ ESP8266_ IP. ip. addr>>8;//点分十进制IP的第二个数<==> addr 次低八位
ESP8266_ IP[2] = ST_ ESP8266_ IP. ip. addr>>16;//点分十进制IP的第三个数<==> addr次高八位
ESP8266_ IP[3] = ST_ ESP8266_ IP. ip. addr>>24;//点分十进制IP的第四个数<==> addr高八位
//打印ESP8266的IP地址
os_ pr intf ("ESP8266_ IP = %d. %d. %d. %d\n", ESP8266_ IP[0], ESP8266_ IP[1], ESP8266_ IP[2], ESP8266_ IP[3]);
OLED_ ShowIP (24, 2, ESP8266_ IP);//显示ESP8266的IP地址
//查询并打印接入此WIFI的设备数量
os_ pr intf ("Number of devices connected to this WIFI = %d\n" ,wifi_ softap_ get_ stat ion_ num()) ;