一、相关函数
WIFI 接口位于 ESP8266_NONOS_SDK/include/user_interface.h。
二、直接设置IP
struct ip_info info;
IP4_ADDR(&info.ip,192,168,0,18);
IP4_ADDR(&info.gw,192,168,0,1);
IP4_ADDR(&info.netmask,255,255,255,0);
wifi_station_dhcpc_stop();
wifi_set_ip_info(STATION_IF,&info); // 设置sta模式的IP
注意: 设置静态 IP,请先关闭对应 DHCP 功能 wifi_station_dhcpc_stop 或者
wifi_softap_dhcps_stop 因为DHCP开启,则静态IP失效,而DHCP默认开启。
二、从Flash中读取
2.1 初次DHCP分配IP
首先连接AP,由DHCP动态分配IP地址
/**
@brief 配置STA模式属性
@param pSsid -[in] 要接入的WIFI名字
@param pPassword -[in] 要连入的WIFI密码
@return 无
*/
void ICACHE_FLASH_ATTR
ConfigStationMode(char *pSsid, char *pPassword)
{
if(!pSsid || !pPassword)
{
return ;
}
struct station_config station;
uint8 wifiMode = wifi_get_opmode();
if((wifiMode == STATION_MODE) || (wifiMode == STATIONAP_MODE))
{
os_strcpy(station.ssid, pSsid);
os_strcpy(station.password, pPassword);
wifi_station_set_config(&station);
wifi_station_disconnect();
wifi_station_connect();
}
}
char ssid[32] = "test";
char password = "12345678";
ConfigStationMode(ssid, password);
2.2 保存IP地址
开一个定时器 startCheckWifiModeTimer() 检测网络连接状态,并保存IP地址
typedef struct dhcpClientInfo_t
{
ip_addr_t ip;
ip_addr_t netmask;
ip_addr_t gw;
uint8 flag;
uint8 pad[3];
} DhcpClientInfo_t;
const uint16 CHECK_WIFI_MODE_PERIOD = 1000;
static os_timer_t s_checkWifiModeTime; // 检查WIFI工作模式的定时器
/**
@brief 开始检查WIFI工作模式的定时器
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
startCheckWifiModeTimer(void)
{
os_timer_disarm(&s_checkWifiModeTimer);
os_timer_setfn(&s_checkWifiModeTimer, (os_timer_func_t *) checkWifiModeTimerCallback, NULL);
os_timer_arm(&s_checkWifiModeTimer, CHECK_WIFI_MODE_PERIOD, true);
}
/**
@brief 检查WIFI工作模式定时器的回调函数
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
checkWifiModeTimerCallback(void)
{
struct ip_info ipInfo;
wifi_get_ip_info(STATION_IF, &ipInfo);
uint8 wifiStationConnectStatus = wifi_station_get_connect_status(); // WIFI STA模式连接状态
if(wifiStationConnectStatus == STATION_GOT_IP && ipInfo.ip.addr != 0)
{
dhcpClientSave(ipInfo);
}
}
/**
@brief DHCP客户端保存
@param ipInfo - [in] IP信息
@return 无
*/
static void ICACHE_FLASH_ATTR
dhcpClientSave(struct ip_info ipInfo)
{
static DhcpClientInfo_t s_dhcpInfo;
if(s_dhcpInfo.flag != 0x01)
{
s_dhcpInfo.flag = 0x01;
s_dhcpInfo.ip = ipInfo.ip;
s_dhcpInfo.netmask = ipInfo.netmask;
s_dhcpInfo.gw = ipInfo.gw;
system_param_save_with_protect(priv_param_start_sec + 1, &s_dhcpInfo, sizeof(s_dhcpInfo));
}
}
2.3 再次启动,读取IP设为静态IP
struct ip_info ipInfo;
wifi_get_ip_info(STATION_IF, &ipInfo);
uint8 wifiStationConnectStatus = wifi_station_get_connect_status(); // WIFI STA模式连接状态
static bool s_dhcpClientInitFlag;
if(wifiStationConnectStatus == STATION_GOT_IP && ipInfo.ip.addr != 0)
{
if(s_dhcpClientInitFlag == false)
{
dhcpClientInit();
s_dhcpClientInitFlag = true;
}
}
/**
@brief DHCP客户端初始化
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
dhcpClientInit(void)
{
DhcpClientInfo_t dhcpInfo;
struct ip_info ipInfo;
system_param_load(priv_param_start_sec + 1, 0, &dhcpInfo, sizeof(dhcpInfo));;
if(dhcpInfo.flag == 0x01)
{
if(wifi_station_dhcpc_status() == DHCP_STARTED)
{
wifi_station_dhcpc_stop();
}
ipInfo.ip = dhcpInfo.ip;
ipInfo.gw = dhcpInfo.gw;
ipInfo.netmask = dhcpInfo.netmask;
os_printf("default ip:"IPSTR"\n",IP2STR(&ipInfo.ip));
os_printf("default netmask:"IPSTR"\n",IP2STR(&ipInfo.netmask));
os_printf("default gw:"IPSTR"\n",IP2STR(&ipInfo.gw));
if(wifi_set_ip_info(STATION_IF, &ipInfo) == false)
{
os_printf("Err:set default ip wrong\n");
}
}
}
2.4 注意
开机由于 wifi_station_set_auto_connect(1) 会自动连接上次路由,然后开启DHCP客户端动态获取IP,在此之前调用 wifi_station_dhcpc_stop() 无效,所以 wifi_set_ip_info() 也会无效。
应该在打印dhcp client start…之后再设置静态IP
• 由 Leung 写于 2019 年 8 月 8 日
• 参考:ESP8266 Non-OS SDK API参考[7qq6]