写在前面:

本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。

本篇就来分析 TCP程序的实现,以及添加自己的接口

/** ESPRSSIF MIT License** Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>** Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,* it is free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,* and/or sell copies of the Software, and to permit persons to whom the Software is furnished* to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or* substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**/#include "esp_common.h"#include "tcp.h"#include "bsp_tcp.h"LOCAL struct espconn esp_conn;
LOCAL esp_tcp esptcp;/******************************************************************************* FunctionName : tcp_server_sent_cb* Description  : data sent callback.* Parameters   : arg -- Additional argument to pass to the callback function* Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR tcp_sent_cb(void *arg)
{//data sent successfullyos_printf(">>>>> tcp sent succeed !!! \r\n");user_TCP_Send();
}/******************************************************************************* FunctionName : tcp_server_recv_cb* Description  : receive callback.* Parameters   : arg -- Additional argument to pass to the callback function*                pusrdata -- The received data (or NULL when the connection has been closed!)*                length -- The length of received data* Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
{//received some data from tcp connectionstruct espconn *pespconn = arg;os_printf(">>>>> tcp recv : %s \r\n", pusrdata);user_TCP_Reveive();//   espconn_send(pespconn, pusrdata, length);}/******************************************************************************* FunctionName : tcp_server_discon_cb* Description  : disconnect callback.* Parameters   : arg -- Additional argument to pass to the callback function* Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR tcp_discon_cb(void *arg)
{//tcp disconnect successfullyos_printf(">>>>> tcp disconnect succeed !!! \r\n");
}/******************************************************************************* FunctionName : tcp_server_recon_cb* Description  : reconnect callback, error occured in TCP connection.* Parameters   : arg -- Additional argument to pass to the callback function* Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR tcp_recon_cb(void *arg, sint8 err)
{//error occured , tcp connection broke.os_printf(">>>>> reconnect callback, error code %d !!! \r\n",err);user_TCP_Abnormal();
}/******************************************************************************* FunctionName : tcp_server_multi_send* Description  : TCP server multi send* Parameters   : none* Returns      : none
*******************************************************************************/
LOCAL void tcp_server_multi_send(void)
{struct espconn *pesp_conn = &esp_conn;remot_info *premot = NULL;uint8 count = 0;sint8 value = ESPCONN_OK;if (espconn_get_connection_info(pesp_conn,&premot,0) == ESPCONN_OK){char *pbuf = "tcp_server_multi_send\n";for (count = 0; count < pesp_conn->link_cnt; count ++){pesp_conn->proto.tcp->remote_port = premot[count].remote_port;pesp_conn->proto.tcp->remote_ip[0] = premot[count].remote_ip[0];pesp_conn->proto.tcp->remote_ip[1] = premot[count].remote_ip[1];pesp_conn->proto.tcp->remote_ip[2] = premot[count].remote_ip[2];pesp_conn->proto.tcp->remote_ip[3] = premot[count].remote_ip[3];espconn_sent(pesp_conn, pbuf, os_strlen(pbuf));}}
}/******************************************************************************* FunctionName : tcp_server_listen_cb* Description  : TCP server listened a connection successfully* Parameters   : arg -- Additional argument to pass to the callback function* Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR tcp_listen_cb(void *arg)
{struct espconn *pesp_conn = arg;os_printf(">>>>> tcp_listen !!! \r\n");espconn_regist_recvcb(pesp_conn, tcp_recv_cb);        // 设置接收回调espconn_regist_sentcb(pesp_conn, tcp_sent_cb);     // 设置发送回调espconn_regist_disconcb(pesp_conn, tcp_discon_cb); // 设置断开连接回调//   tcp_server_multi_send();// 多服务器发送
}/******************************************************************************* FunctionName : tcp_init* Description  : parameter initialize as a TCP server/client* Parameters   : mode -- server/client, remote_ip -- remote ip addr, port -- server/client port* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_tcp_init(TCP_MODE_TYPE mode, struct ip_addr *remote_ip, uint32 port)
{struct ip_info info;esp_conn.proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));  // 分配空间esp_conn.type = ESPCONN_TCP;            // 创建TCPesp_conn.state = ESPCONN_NONE;         // 一开始的状态,空闲状态esp_conn.proto.tcp = &esptcp;            // 设置TCP的IP和回调函数存储用esp_conn.proto.tcp->local_port = port;   // 监听的端口号/* 读取 station IP信息 */wifi_get_ip_info(STATION_IF,&info);esp_conn.proto.tcp->local_ip[0] = info.ip.addr;esp_conn.proto.tcp->local_ip[1] = info.ip.addr >> 8;esp_conn.proto.tcp->local_ip[2] = info.ip.addr >> 16;esp_conn.proto.tcp->local_ip[3] = info.ip.addr >> 24;os_printf("\n>>>>> TCP Local IP: %d.%d.%d.%d\n\n", esp_conn.proto.tcp->local_ip[0], \esp_conn.proto.tcp->local_ip[1], esp_conn.proto.tcp->local_ip[2], \esp_conn.proto.tcp->local_ip[3]);if(ESPCONN_TCP_CLIENT == mode){memcpy(esp_conn.proto.tcp->remote_ip, remote_ip, 4);esp_conn.proto.tcp->remote_port = port;           // 远程的端口号esp_conn.proto.tcp->local_port += 1;          // 监听的端口号}espconn_regist_connectcb(&esp_conn, tcp_listen_cb);   // 注册 TCP 连接成功建立后的回调函数espconn_regist_reconcb(&esp_conn, tcp_recon_cb);  // 注册 TCP 连接发生异常断开时的回调函数,可以在回调函数中进行重连if(ESPCONN_TCP_SERVER == mode){espconn_regist_time(&esp_conn, 180, 0);        // 设置超时断开时间 单位:秒,最大值:7200 秒espconn_accept(&esp_conn);                      // 创建 TCP server,建立侦听os_printf("\n>>>>> tcp server setup successful\n");}else if(ESPCONN_TCP_CLIENT == mode){espconn_connect(&esp_conn);                      // 创建 TCP client,启用连接os_printf("\n>>>>> tcp client setup successful\n");}
}

以上代码是基于官方社区提供的代码进行简单的分析和修改的,好了,为方便自己管理代码,我们编写自己的接口代码,上面的 user_xxxx()函数就是我们自己引出的函数

/** bsp_tcp.c**  Created on: 2019年9月4日*      Author: liziyuan*/#include "esp_common.h"#include "bsp_tcp.h"/******************************************************************************* FunctionName : user_TCP_Abnormal* Description  : tcp异常回调用户处理* Parameters   : none* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_TCP_Abnormal(void)
{}/******************************************************************************* FunctionName : user_TCP_Send* Description  : tcp发送回调用户处理* Parameters   : none* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_TCP_Send(void)
{}/******************************************************************************* FunctionName : user_TCP_Reveive* Description  : tcp接收回调用户处理* Parameters   : none* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_TCP_Reveive(void)
{}/******************************************************************************* FunctionName : user_TCP_Client_Init* Description  : tcp client初始化* Parameters   : none* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_TCP_Client_Init(void)
{const uint8 remote_ip[4] = {192, 168, 1, 1};user_tcp_init(ESPCONN_TCP_CLIENT, (struct ip_addr *)remote_ip, USER_TCP_CLIENT_PORT);
}/******************************************************************************* FunctionName : user_TCP_Server_Init* Description  : tcp server初始化* Parameters   : none* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_TCP_Server_Init(void)
{user_tcp_init(ESPCONN_TCP_SERVER, (struct ip_addr *)NULL, USER_TCP_SERVER_PORT);
}/******************************************************************************* FunctionName : tcp_communication_task* Description  : tcp通讯任务* Parameters   : pvParameters* Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR tcp_communication_task(void *pvParameters)
{os_printf("\n>>>>> TCP communication is being created.....\n");while(1){if (wifi_station_get_connect_status() == STATION_GOT_IP)    // 等待连接到 AP{break;}vTaskDelay(300 / portTICK_RATE_MS);}#if 1user_TCP_Server_Init();#elseuser_TCP_Client_Init();#endifvTaskDelete(NULL);
}/*------------------------------- END OF FILE -------------------------------*/

至此,简单的 tcp代码分析就结束了,附上社区上面的 TCP参考代码吧

ESP8266 as TCP client

ESP8266 as TCP server

ESP8266 RTOS SDK学习之 TCP相关推荐

  1. ESP8266 RTOS SDK学习之 UDP

    写在前面: 本文章旨在总结备份.方便以后查询,由于是个人总结,如有不对,欢迎指正:另外,内容大部分来自网络.书籍.和各类手册,如若侵权请告知,马上删帖致歉. 同样的,跟上一篇 TCP分析一样,本篇是分 ...

  2. IoT开发——WIFI模块ESP8266 RTOS SDK V3.0.0环境搭建

    目录 1. 环境概览 2. 安装Ubuntu操作系统 3.搭建编译环境 3.2 环境准备 3.3 环境配置 3.4 设置串口,进行编译 3.5 配置elipse编译器 (1)安装eclipse (2) ...

  3. 安信可1.5---编译下载乐鑫ESP8266 RTOS SDK库

    一.安装安信可一体化工具 参考安信可官方博客:安信可IDE1.5 二.下载乐鑫ESP8266 RTOS SDK库 因为github下载太慢,经常下载不下来,这里使用gitee进行下载,请自行安装git ...

  4. ESP8266 RTOS SDK 开发环境搭建

    一.工具链的设置 参考乐鑫官网文档 Get Started - ESP8266 RTOS SDK Programming Guide documentation 二.获取ESP8266_RTOS_SD ...

  5. esp8266 rtos sdk在小黄板上的使用

    2019独角兽企业重金招聘Python工程师标准>>> ##1. 下载RTOS SDK代码 git clone https://github.com/espressif/esp_io ...

  6. 乐鑫esp8266学习rtos3.0笔记:分享在 esp8266 C SDK实现冷暖光色温平滑调节的封装,轻松集成到您的项目去。(附带Demo)

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,不做开发板.仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 基于C SDK的ESP8266开发技术全系列笔记 一.N ...

  7. Esp8266 进阶之路36【外设篇】乐鑫esp8266芯片SDK编程驱动时间芯片 ds1302,同步网络时间到本地,再也不怕掉电断网也可以同步时间了!(附带Demo)

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1. Esp8266之 搭建开发环境,开始一个"hello ...

  8. ESP8266 Non-OS SDK 开发之旅 基础篇① 初识 Non-OS SDK,史上超级详细手把手教小白20分钟快速搭建SDK软件开发环境,完成第一个例子Hello World!

    文章目录 1.前言 2. SDK概述 2.1 SDK使用流程 2.2 ESP8266 HDK -- 硬件开发工具 2.3 ESP8266 SDK -- 软件开发工具包 2.3.1 Non-OS SDK ...

  9. 5. ESP8266固件的编译(RTOS SDK固件)

    在RTOS SDK下,除了用户程序入口函数名字是user_init()以外, 整个的编程感觉很像linux(当然具体是非常不一样的)下编程,也有tcp/ip协议栈,就像传统的C开发. 1)固件代码准备 ...

最新文章

  1. stm32机器学习_STM32机器学习开发实战
  2. WebKit如何加载web页面
  3. wyh 的 Code Style
  4. react typescript 子组件调用父组件
  5. 【转】关于字符编码,你所需要知道的
  6. cmd炫酷代码_基本操作!在VS 代码中如何使用Jupyter Notebook
  7. PL-SLAM: a Stereo SLAM System through the Combination of Points and Line Segments
  8. O-矩阵相乘-Warshall算法详解
  9. 5.7 C和C++的关系
  10. IT基础架构现代化,未来企业的“标配”是什么?
  11. 安装制作 基础篇(一) 基本概念
  12. Android APK反编译得到Java源代码和资源文件
  13. 火遍全网的「蚂蚁呀嘿」教程开源了!
  14. 2021国防科技大学计算机学院无军籍考研经验贴
  15. DiR细胞膜染料,CAS:100068-60-8,DiR iodide
  16. 神马笔记 版本1.3.0
  17. 逻辑回归(吴恩达机器学习笔记)
  18. 云服务案例分析 BB平台 Quiz5
  19. 各种手机刷机包 救砖包 root工具
  20. 南京工程学院 实用软件工程 复习总结

热门文章

  1. quartus altera FIFO使用问题总结:rd_usedw和wr_usedw、rd_rst和wr_rst
  2. C语言进阶——likely和unlikely
  3. Unity- 游戏结束以及重启游戏
  4. 如何重定向到另一个网页? [英]How do I redirect to another webpage?
  5. 内网通过nginx发送邮件
  6. VB.NET版机房收费系统---报表
  7. 乔布斯致敬过的他,开启了商业太空旅行时代
  8. php 按key降序排序,PHP程序按升序和降序对整数数组进行排序
  9. 利用ffmpeg将微信speex格式转为wav或mp3
  10. 【酷熊科技】工作积累 ----------- Unity3D grid 显示问题