配色: 字号:
NONSDK-LED-UDP例程
2017-01-23 | 阅:  转:  |  分享 
  
#include"os_type.h"//#include"driver/gpio16.h"#include"gpio.h"//#in
clude"driver/uar.h"//串口外设驱动库#include"user_interface.h"#include
"spi_flash.h"#include"espconn.h"#include"user_config.h"#include
"smartconfig.h"#include"ets_sys.h"#include"osapi.h"structespc
onnPhoneConn;//定义结构体esp_udpPhoneConnUdp;voidICACHE_FLASH_ATTRu
dpclient_recv(voidarg,charpdata,unsignedshortlen){if(pda
ta[0]==''L''&&pdata[1]==''E''&&pdata[2]==''D''&&pdata[3]==''0''){GPIO_OU
TPUT_SET(GPIO_ID_PIN(12),0);//espconn_sent(&PhoneConn,"打开LED成功!"
,13);//接收数据}if(pdata[0]==''L''&&pdata[1]==''E''&&pdata[2]==''D''&&p
data[3]==''1''){GPIO_OUTPUT_SET(GPIO_ID_PIN(12),1);}if(GPIO_INPU
T_GET(GPIO_ID_PIN(12))==0){espconn_sent(&PhoneConn,"LED打开!",9);}e
lse{espconn_sent(&PhoneConn,"LED关闭!",9);}}LOCALos_timer_ttimer;
//定义结构体用于定时器voidICACHE_FLASH_ATTRuser_set_softap_config(void)//设
置wifi的名称和密码{structsoftap_configconfig;os_memset(config.ssid,0,
32);os_memset(config.password,0,64);os_memcpy(config.ssid,"ESPES
P22",9);//SSID账号os_memcpy(config.password,"99999999",8);//密码con
fig.authmode=AUTH_WPA_WPA2_PSK;config.ssid_len=0;config.max_conne
ction=5;//允许接入点数量wifi_softap_set_config(&config);//设置WiFisoft-A
P接?配置,并保存到flash}charzt=1;voidtimer_callback()//回调的函数{if(zt
==1){GPIO_OUTPUT_SET(GPIO_ID_PIN(15),0);//将GPIO2设置为低电平zt=0
;}else{GPIO_OUTPUT_SET(GPIO_ID_PIN(15),1);//将GPIO2设置为低电平zt=1
;}}voiduser_init(void){wifi_set_opmode(0x02);//设置WiFi模式为APuser_
set_softap_config();PhoneConn.type=ESPCONN_UDP;PhoneConn.proto.ud
p=&PhoneConnUdp;PhoneConn.proto.udp->local_port=9999;//本地端口PhoneC
onn.proto.udp->local_port=9999;//远程端口PhoneConn.proto.udp->remote_
ip[0]=255;//指定IP地址PhoneConn.proto.udp->remote_ip[1]=255;PhoneConn
.proto.udp->remote_ip[2]=255;PhoneConn.proto.udp->remote_ip[3]=25
5;PhoneConn.proto.udp->remote_ip[0]=255;PhoneConn.proto.udp->remo
te_ip[1]=255;PhoneConn.proto.udp->remote_ip[2]=255;PhoneConn.prot
o.udp->remote_ip[3]=255;espconn_regist_recvcb(&PhoneConn,udpclien
t_recv);//注册一个UDP数据包接收回调espconn_create(&PhoneConn);//建立UDP回调//os
_printf("SDKversion:%s\n",system_get_sdk_version());PIN_FUNC_S
ELECT(PERIPHS_IO_MUX_MTDI_U,FUNC_GPIO12);//初始化GPIO2PIN_FUNC_SELE
CT(PERIPHS_IO_MUX_MTDI_U,FUNC_GPIO15);//初始化GPIO2os_timer_disarm(
&timer);//关闭定时器,相当于计时器的计数os_timer_setfn(&timer,(os_timer_func_t
)timer_callback,NULL);//初始化定时器os_timer_arm(&timer,5000,1);//开始定时器,1000毫秒后,回调前面的callback函数(后面的0表示只运行为一次1表示循环运行)}voiduser_rf_pre_init(){}
献花(0)
+1
(本文系风雲再现首藏)