ESP8266 NONOS SDK入门——station+tcp cilent

该程序学习于https://blog.csdn.net/qq_41068712/article/details/86154620的博客
调用如下头文件
在这里插入图片描述
其中还需要在espconn.h中include ip_addr.h
代码如下

os_timer_t checktimer_wifestate;//定时器结构体
struct espconn user_tcp_conn;//tcp结构体
void ICACHE_FLASH_ATTR user_tcp_sent_cb(void *arg)//发送数据成功回调函数
{
	os_printf("发送数据成功!\r\n");
}
void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg)//连接正常断开回调函数
{
	os_printf("断开连接成功!\r\n");
}
void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg,char *pdata,unsigned short len)//接收数据成功回调函数
{
	os_printf("收到数据:%s\r\n",pdata);
	espconn_sent((struct espconn *)arg,"收到成功!",strlen("收到数据!"));
}
void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg,sint8 err)//tcp发生异常断开回调函数
{
	os_printf("连接错误,错误代码为:%d",err);
	espconn_connect((struct espconn *)arg);//重新连接
}
void ICACHE_FLASH_ATTR user_tcp_connent_cb(void *arg)//tcp连接建立成功回调函数
{
	struct espconn *pespconn = arg;
	os_printf("tcp连接成功");
	espconn_regist_recvcb(pespconn,user_tcp_recv_cb);//注册接收成功回调函数
	espconn_regist_sentcb(pespconn,user_tcp_sent_cb);//注册发送成功回调函数
	espconn_regist_disconcb(pespconn,user_tcp_discon_cb);//注册连接正常断开回调函数
//	espconn_send(pespconn,"8266",strlen("8266"));//
}

void ICACHE_FLASH_ATTR my_station_init(struct ip_addr *remote_ip,
		struct ip_addr *local_ip, int remote_port) {
	user_tcp_conn.proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));  //分配空间
	user_tcp_conn.type = ESPCONN_TCP;  //设置类型为TCP协议
	os_memcpy(user_tcp_conn.proto.tcp->local_ip, local_ip, 4);
	os_memcpy(user_tcp_conn.proto.tcp->remote_ip, remote_ip, 4);
	user_tcp_conn.proto.tcp->local_port = espconn_port();  //本地端口
	user_tcp_conn.proto.tcp->remote_port = remote_port;  //目标端口
	//注册连接成功回调函数和重新连接回调函数
	espconn_regist_connectcb(&user_tcp_conn, user_tcp_connent_cb);//注册 TCP 连接成功建立后的回调函数
	espconn_regist_reconcb(&user_tcp_conn, user_tcp_recon_cb);//注册 TCP 连接发生异常断开时的回调函数,可以在回调函数中进行重连
	//启用连接
	espconn_connect(&user_tcp_conn);
}
void check_WIFIstate(void)
{
	uint8 getstate;//定义连接状态变量
	getstate=wifi_station_get_connect_status();//获取sta连接状态
	if(getstate == STATION_GOT_IP)//查看sta是否连接ap
	{
		os_printf("WIFI连接成功!");
		os_timer_disarm(&checktimer_wifestate);//关闭软件定时器
		struct ip_info info;
		const char remote_ip[4]={10,145,90,3};//tcp服务端ip地址,需服务端先用此ip开启
		wifi_get_ip_info(STATION_IF,&info);//查询模块station的ip
		my_station_init((struct ip_addr *)remote_ip,&info.ip,8000);//连接到目标服务器的8000端口
	}
}
void tcp_client_init()//tcp cilent初始化
{
	wifi_set_opmode(0x01);//设置为station模式保存flash
	struct station_config stationconf;//stationa结构体
	os_strcpy(stationconf.ssid,"0xff");//设置连入路由的的用户名
	os_strcpy(stationconf.password,"66666666");//设置连入路由器的密码
	wifi_station_set_config(&stationconf);//设置wifi接口配置并保存到flash
	wifi_station_connect();//连接路由器
	os_timer_disarm(&checktimer_wifestate);//取消定时器定时
	os_timer_setfn(&checktimer_wifestate,(os_timer_func_t *)check_WIFIstate,NULL);//设置定时器回调函数
	os_timer_arm(&checktimer_wifestate,200,1);//开启软件定时器500ms
}
/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
	tcp_client_init();
}

与我的参考博客代码相同
其中要注意的是需要在程序中先定义好连入wifi的用户名和密码以及tcp服务端的ip与端口
注意先开启tcp服务端在上电esp8266
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值