当前位置:网站首页>ESP8266 AP_TCP_Client
ESP8266 AP_TCP_Client
2022-07-26 22:44:00 【Paradise_Violet】
目录
如果8266作为tcp_client成功的与tcp_server建立了tcp连接的话,就会进入到tcp连接建立成功的回调函数
前言
8266在ap模式下,创建tcp_client,与tcp_server建立tcp连接
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // 初始化串口波特率
os_delay_us(10000); // 等待串口稳定
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// OLED显示初始化
//------------------------------------------------------------
OLED_Init(); // OLED初始化
OLED_ShowString(0,0,"ESP8266 = Client"); // ESP8266模式
OLED_ShowString(0,2,"IP:"); // ESP8266_IP地址
OLED_ShowString(0,4,"Remote = Server"); // 远端主机模式
OLED_ShowString(0,6,"IP:"); // 远端主机IP地址
//------------------------------------------------------------
LED_Init_JX(); // LED初始化
ESP8266_AP_Init_JX(); // 初始化ESP8266_AP模式
OS_Timer_1_Init_JX(30000,0); // 30秒定时(一次)
}说一下这里的30s定时,是为了给笔记本接入到8266创建的WiFi局域网,并且为网络调试助手创建tcp_server预留时间
初始化网络连接(TCP通信)
// 初始化网络连接(TCP通信)
//========================================================================================================
void ICACHE_FLASH_ATTR ESP8266_NetCon_Init_JX()
{
// 结构体赋值
//--------------------------------------------------------------------------
ST_NetCon.type = ESPCONN_TCP ; // 设置为TCP协议
ST_NetCon.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); // 开辟内存
// 此处需要设置目标IP/端口(ESP8266作为Client,需要预先知道Server的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;
//u8 remote_ip[4] = {192,168,4,2}; // 目标ip地址
//os_memcpy(ST_NetCon.proto.udp->remote_ip,remote_ip,4); // 拷贝内存
// 注册连接成功回调函数、异常断开回调函数
//--------------------------------------------------------------------------------------------------
espconn_regist_connectcb(&ST_NetCon, ESP8266_TCP_Connect_Cb_JX); // 注册TCP连接成功建立的回调函数
espconn_regist_reconcb(&ST_NetCon, ESP8266_TCP_Break_Cb_JX); // 注册TCP连接异常断开的回调函数
// 连接 TCP server
//----------------------------------------------------------
espconn_connect(&ST_NetCon); // 连接TCP-server
}如果8266作为tcp_client成功的与tcp_server建立了tcp连接的话,就会进入到tcp连接建立成功的回调函数
// TCP连接建立成功的回调函数
//====================================================================================================================
void ICACHE_FLASH_ATTR ESP8266_TCP_Connect_Cb_JX(void *arg)
{
espconn_regist_sentcb((struct espconn *)arg, ESP8266_WIFI_Send_Cb_JX); // 注册网络数据发送成功的回调函数
espconn_regist_recvcb((struct espconn *)arg, ESP8266_WIFI_Recv_Cb_JX); // 注册网络数据接收成功的回调函数
espconn_regist_disconcb((struct espconn *)arg,ESP8266_TCP_Disconnect_Cb_JX); // 注册成功断开TCP连接的回调函数
espconn_send((struct espconn *)arg,"Hello,I am ESP8266",os_strlen("Hello,I am ESP8266")); // 向Server发起通信
os_printf("\n--------------- ESP8266_TCP_Connect_OK ---------------\n");
}成功接收网络数据的回调函数
// 成功接收网络数据的回调函数【参数1:网络传输结构体espconn指针、参数2:网络传输数据指针、参数3:数据长度】
//=========================================================================================================
void ICACHE_FLASH_ATTR ESP8266_WIFI_Recv_Cb_JX(void * arg, char * pdata, unsigned short len)
{
struct espconn * T_arg = arg; // 缓存网络连接结构体指针
// 根据数据设置LED的亮/灭
//-------------------------------------------------------------------------------
if(pdata[0] == 'k' || pdata[0] == 'K') LED_ON; // 首字母为'k'/'K',灯亮
else if(pdata[0] == 'g' || pdata[0] == 'G') LED_OFF; // 首字母为'g'/'G',灯灭
os_printf("\nESP8266_Receive_Data = %s\n",pdata); // 串口打印接收到的数据
/*
// 获取远端信息
//------------------------------------------------------------------------------------
remot_info * P_port_info = NULL; // 定义远端连接信息指针
if(espconn_get_connection_info(T_arg, &P_port_info, 0)==ESPCONN_OK) // 获取远端信息
{
T_arg->proto.tcp->remote_port = P_port_info->remote_port; // 获取对方端口号
T_arg->proto.tcp->remote_ip[0] = P_port_info->remote_ip[0]; // 获取对方的IP地址
T_arg->proto.tcp->remote_ip[1] = P_port_info->remote_ip[1];
T_arg->proto.tcp->remote_ip[2] = P_port_info->remote_ip[2];
T_arg->proto.tcp->remote_ip[3] = P_port_info->remote_ip[3];
//os_memcpy(T_arg->proto.tcp->remote_ip,P_port_info->remote_ip,4); // 内存拷贝
}
*/
//--------------------------------------------------------------------
OLED_ShowIP(24,6,T_arg->proto.tcp->remote_ip); // 显示远端主机IP地址
//--------------------------------------------------------------------
//【TCP通信是面向连接的,向远端主机回应时可直接使用T_arg结构体指针指向的IP信息】
//-----------------------------------------------------------------------------------------------
espconn_send(T_arg,"ESP8266_WIFI_Recv_OK",os_strlen("ESP8266_WIFI_Recv_OK")); // 向对方发送应答
}TCP连接断开成功的回调函数
// TCP连接断开成功的回调函数
//================================================================
void ICACHE_FLASH_ATTR ESP8266_TCP_Disconnect_Cb_JX(void *arg)
{
os_printf("\nESP8266_TCP_Disconnect_OK\n");
}TCP连接异常断开时的回调函数
// TCP连接异常断开时的回调函数
//====================================================================
void ICACHE_FLASH_ATTR ESP8266_TCP_Break_Cb_JX(void *arg,sint8 err)
{
os_printf("\nESP8266_TCP_Break\n");
espconn_connect(&ST_NetCon); // 连接TCP-server
}
说明我们的8266作为tcp_client已经成功地与网络调试助手作为tcp_server建立了tcp连接,并且成功地向tcp_server发送了数据,并且tcp_server也可以向8266通过tcp通讯。
边栏推荐
- 以赛促练-力扣第303场周赛反思
- 5. Xshell connection server denied access, password error
- What is the digital economy and how does it change the business model?
- 【无标题】
- 5.xshell连接服务器拒绝访问,密码错误
- Jenkins--基础--5.3--系统配置--全局安全配置
- Database interim (I)
- Verilog procedure assignment statement
- SQL learning (1) - table related operations
- Esp8266 --- JSON data exchange format
猜你喜欢
随机推荐
Surrounded area
Jenkins--基础--02--安装
The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code
if 与 else if 的区别
4. Root user login
What is the digital economy and how does it change the business model?
c语言实现动态顺序表的增删查改
Iptables detailed explanation and practical cases
Keil开发环境的搭建送安装包
基于485总线的评分系统
深度学习笔记
什么是数字经济,它是如何改变商业模式的?
Unity 一个好用的UI灰度Shader
MySQL关闭连接事务自动提交的问题
RS485信号的测量
Are you ready for the Internet of things to revolutionize manufacturing?
Unity twitter login access
Compile Darknet under vscode2015 to generate darknet Ext error msb3721: XXX has exited with a return code of 1.
最长公共子串
Jenkins--基础--5.3--系统配置--全局安全配置







![【unity】Unity界面scene视图[1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
