基于ESP8266_NONOS_SDK-2.2.0

os_timer_t OS_Timer_1;//定义软件定时器变量
//ap dhcp
struct espconn ST_NetCon;
esp_tcp ST_TCP;


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);
	os_printf("remot rev cb ip add=%s\r\n",T_arg->proto.tcp->remote_ip);
	//tcp是面向连接的,直接T_arg信息
	espconn_send(T_arg,"ESP8266 wifi rev TCP OK",os_strlen("ESP8266 wifi rev TCP OK"));//向对方发送应答
}
void ICACHE_FLASH_ATTR ESP8266_TCP_Dissconnect_cb(void *arg)
{
	os_printf("----ESP_TCP_Dissconnect CB!----\r\n");
}
void ICACHE_FLASH_ATTR ESP_TCP_Connect_cb(void *arg)
{
	espconn_regist_sentcb((struct espconn *)arg,ESP8266_WIFI_Send_cb);
	espconn_regist_recvcb((struct espconn *)arg,ESP8266_WIFI_Rev_cb);
	espconn_regist_disconcb((struct espconn *)arg,ESP8266_TCP_Dissconnect_cb);
	os_printf("----ESP_TCP_CONNECT OK!----\r\n");
}
void ICACHE_FLASH_ATTR ESP_TCP_Break_cb(void *arg,sint8 err)
{
	os_printf("----ESP_TCP_BREAK!----\r\n");
}
void ICACHE_FLASH_ATTR ESP8266_NetCon_init(void)
{
	ST_NetCon.type=ESPCONN_TCP;//通信协议TCP
	ST_NetCon.proto.tcp=&ST_TCP;
	//esp8266作为服务端,不需要先知道client的ip地址
	ST_NetCon.proto.tcp->local_port=8266;//设置本地端口,客户端
//	ST_NetCon.proto.tcp->remote_port=8888;//设置远程端口,服务端
//	ST_NetCon.proto.tcp->remote_ip[0]=192;//设置远程ip端口,服务端
//	ST_NetCon.proto.tcp->remote_ip[1]=168;
//	ST_NetCon.proto.tcp->remote_ip[2]=4;
//	ST_NetCon.proto.tcp->remote_ip[3]=2;

	espconn_regist_connectcb(&ST_NetCon,ESP_TCP_Connect_cb);//tcp连接成功建立后回调函数
	espconn_regist_reconcb(&ST_NetCon,ESP_TCP_Break_cb);//tcp连接异常断开的回调函数

	espconn_accept(&ST_NetCon);//创建TCP服务,建立侦听
	espconn_regist_time(&ST_NetCon,300,0);//设置超时断开时间,最大7200秒
}
void ICACHE_FLASH_ATTR ESP8266_AP_init(void)
{
	struct softap_config AP_Config;
	wifi_set_opmode(0x02);
	os_memset(&AP_Config,0,sizeof(struct softap_config));
	os_strcpy(AP_Config.ssid,"wifissid");
	os_strcpy(AP_Config.password,"1234");
	AP_Config.ssid_len=os_strlen("wifissid");
	AP_Config.channel=1;
	AP_Config.authmode=AUTH_WPA2_PSK;
	AP_Config.ssid_hidden=0;
	AP_Config.max_connection=4;
	AP_Config.beacon_interval=100;
	wifi_softap_set_config(&AP_Config);
}
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)//软件定时器的回调函数
{
    struct ip_info ST_ESP8266_ip;
	u8 esp8266_ip[4];
	wifi_get_ip_info(SOFTAP_IF,&ST_ESP8266_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]);

		os_timer_disarm(&OS_Timer_1);//关闭软件定时器
		ESP8266_NetCon_init();//初始化网络通信TCP
	}
}
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);//使能定时器
}




//主函数调用
ESP8266_AP_init();
OS_Timer_1_Init(1000,1);