基于ESP8266_NONOS_SDK-2.2.0

//ap dhcp
struct espconn ST_NetCon;
esp_udp ST_UDP;
#define ESP8266_STA_SSID "wifiname"
#define ESP8266_STA_PASS "123456"

void ICACHE_FLASH_ATTR delay_ms(u32 ms)
{
  for(;0<ms;ms--)
  {
	  os_delay_us(1000);
  }
}

void ICACHE_FLASH_ATTR ESP8266_WIFI_Send_cb(void * arg)
{
	os_printf("esp8266 send OK!\r\n");
}

void ICACHE_FLASH_ATTR ESP8266_WIFI_Rev_cb(void * arg,char * pdata,unsigned short len)
{
	struct espconn *T_arg=arg;
	remot_info *P_port_info=NULL;
	if(pdata[0]=='k'||pdata[0]=='K')gpio16_output_set(0);//开
	else if(pdata[0]=='g'||pdata[0]=='G')gpio16_output_set(1);//关
	os_printf("esp8266 rev data =%s\r\n",pdata);

	if(espconn_get_connection_info(T_arg,&P_port_info,0)==ESPCONN_OK)//获取远端主机的信息
	{
		T_arg->proto.udp->remote_port=P_port_info->remote_port;
		T_arg->proto.udp->remote_ip[0]=P_port_info->remote_ip[0];
		T_arg->proto.udp->remote_ip[1]=P_port_info->remote_ip[1];
		T_arg->proto.udp->remote_ip[2]=P_port_info->remote_ip[2];
		T_arg->proto.udp->remote_ip[3]=P_port_info->remote_ip[3];
	}
	os_printf("remot rev cb ip add=%s\r\n",T_arg->proto.udp->remote_ip);
	espconn_send(T_arg,"ESP8266 wifi rev TCP OK",os_strlen("ESP8266 wifi rev TCP OK"));//向对方发送应答
}
void ICACHE_FLASH_ATTR ESP8266_NetCon_init(void)
{
	ST_NetCon.type=ESPCONN_UDP;//通信协议UDP
	ST_NetCon.proto.udp=&ST_UDP;
	//esp8266作为服务端,不需要先知道client的ip地址
	ST_NetCon.proto.udp->local_port=8266;//设置本地端口,客户端
//	ST_NetCon.proto.udp->remote_port=8888;//设置远程端口,服务端
//	ST_NetCon.proto.udp->remote_ip[0]=192;//设置远程ip端口,服务端
//	ST_NetCon.proto.udp->remote_ip[1]=168;
//	ST_NetCon.proto.udp->remote_ip[2]=4;
//	ST_NetCon.proto.udp->remote_ip[3]=2;

	espconn_regist_sentcb(&ST_NetCon,ESP8266_WIFI_Send_cb);
	espconn_regist_recvcb(&ST_NetCon,ESP8266_WIFI_Rev_cb);
	espconn_create(&ST_NetCon);//初始化UDP通信

}
void ICACHE_FLASH_ATTR ESP8266_STA_init(void)
{
	struct station_config STA_config;//STA参数结构体
	struct ip_info ST_ESP8266_IP;//STA信息结构体

	wifi_set_opmode(0x01);//设置STA模式,并保存到FLASH

//	//设置STA模式下的IP地址,ESP8266默认试DHCP模式,接入WIFI会自动分配IP地址
//	wifi_station_dhcpc_stop();//关闭DHCP
//	IP4_ADDR(&ST_ESP8266_IP.ip,192,168,1,8);//配置IP地址
//	IP4_ADDR(&ST_ESP8266_IP.netmask,255,255,255,0);//配置子网掩码
//	IP4_ADDR(&ST_ESP8266_IP.gw,192,168,1,1);//配置网关地址
//	wifi_set_ip_info(STATION_IF,&ST_ESP8266_IP);//设置STA模式下的IP地址

	os_memset(&STA_config,0,sizeof(struct station_config));//STA参数结构体清零
	os_strcpy(STA_config.ssid,ESP8266_STA_SSID);//设置wifi名
	os_strcpy(STA_config.password,ESP8266_STA_PASS);//设置wifi密码
	wifi_station_set_config(&STA_config);//设置STA参数,并保存到flash
}
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)//软件定时器的回调函数
{
    u8 C_LED_flash=0;//led闪烁计数
	u8 S_WIFI_STA_connect; //wifi接入状态标志
	struct ip_info ST_ESP8266_ip;//esp8266 ip 信息
	u8 esp8266_ip[4];//esp8266 ip地址
	S_WIFI_STA_connect=wifi_station_get_connect_status();//查询STA接入wifi状态
	switch(S_WIFI_STA_connect)
	{
		case 0: os_printf("\nSTA IDLE\n");break;
		case 1: os_printf("\nSTA connecting...\n");break;
		case 2: os_printf("\nSTA password error...\n");break;
		case 3: os_printf("\nSTA no AP found...\n");break;
		case 4: os_printf("\nSTA connect fail...\n");break;
		case 5: os_printf("\nSTA geted IP,suss!\n");break;
	}
	if(S_WIFI_STA_connect==STATION_GOT_IP)//判断是否获取IP
	{
		wifi_get_ip_info(STATION_IF,&ST_ESP8266_ip);//获取STATION_IF下的ip地址
		if(ST_ESP8266_ip.ip.addr!=0)
		{
			esp8266_ip[0]=ST_ESP8266_ip.ip.addr;
			esp8266_ip[1]=ST_ESP8266_ip.ip.addr>>8;
			esp8266_ip[2]=ST_ESP8266_ip.ip.addr>>16;
			esp8266_ip[3]=ST_ESP8266_ip.ip.addr>>24;
			os_printf("ESP8266 ip=%d.%d.%d.%d\r\n",esp8266_ip[0],esp8266_ip[1],esp8266_ip[2],esp8266_ip[3]);
		}
		for(;C_LED_flash<=5;C_LED_flash++)//接入WIFI,LED快闪3次
		{
			gpio16_output_set(C_LED_flash%2);
			delay_ms(100);
		}
		os_timer_disarm(&OS_Timer_1);//关闭软件定时器
		ESP8266_NetCon_init();//初始化网路UDP通信
	}
}
void ICACHE_FLASH_ATTR OS_Timer_1_Init(u32 timemsm,bool time_repetitive)//软件定时器初始化
{
	os_timer_disarm(&OS_Timer_1);//关闭软件定时器
	os_timer_setfn(&OS_Timer_1,(os_timer_func_t *)OS_Timer_1_cb,NULL);//设置定时器,设置回调函数
	os_timer_arm(&OS_Timer_1,timemsm, time_repetitive);//使能定时器
}


//主函数执行如下
gpio16_output_conf();
gpio16_output_set(1);//关闭
ESP8266_STA_init();
OS_Timer_1_Init(1000,1);