基于ESP8266_NONOS_SDK-2.2.0
os_timer_t OS_Timer_1;//定义软件定时器变量
struct espconn ST_NetCon;
esp_udp ST_UDP;
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];//得到对方IP地址
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 OK",os_strlen("ESP8266 wifi rev OK"));//向对方发送应答
}
void ICACHE_FLASH_ATTR ESP8266_NetCon_init(void)
{
ST_NetCon.type=ESPCONN_UDP;//通信协议UDP
ST_NetCon.proto.udp=&ST_UDP;//申请内存
ST_NetCon.proto.udp->local_port=8266;//设置本地端口
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 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();//初始化网络连接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);//使能定时器
}
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);
}
//主函数里调用如下
gpio16_output_conf();
gpio16_output_set(1);//关闭
ESP8266_AP_init();
OS_Timer_1_Init(1000,1);
然后用电脑或者手机连接8266模块的wifi
使用网络调试助手发送命令到192.168.4.1:8266,灯会亮灭,命令行会收到信息。
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
走着的小站
本文地址: esp8266 NONOS SDK AP UDP服务端
本文地址: esp8266 NONOS SDK AP UDP服务端