ESP8266 RTOS SDK学习之 TCP
写在前面:
本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。
本篇就来分析 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相关推荐
- ESP8266 RTOS SDK学习之 UDP
写在前面: 本文章旨在总结备份.方便以后查询,由于是个人总结,如有不对,欢迎指正:另外,内容大部分来自网络.书籍.和各类手册,如若侵权请告知,马上删帖致歉. 同样的,跟上一篇 TCP分析一样,本篇是分 ...
- IoT开发——WIFI模块ESP8266 RTOS SDK V3.0.0环境搭建
目录 1. 环境概览 2. 安装Ubuntu操作系统 3.搭建编译环境 3.2 环境准备 3.3 环境配置 3.4 设置串口,进行编译 3.5 配置elipse编译器 (1)安装eclipse (2) ...
- 安信可1.5---编译下载乐鑫ESP8266 RTOS SDK库
一.安装安信可一体化工具 参考安信可官方博客:安信可IDE1.5 二.下载乐鑫ESP8266 RTOS SDK库 因为github下载太慢,经常下载不下来,这里使用gitee进行下载,请自行安装git ...
- ESP8266 RTOS SDK 开发环境搭建
一.工具链的设置 参考乐鑫官网文档 Get Started - ESP8266 RTOS SDK Programming Guide documentation 二.获取ESP8266_RTOS_SD ...
- esp8266 rtos sdk在小黄板上的使用
2019独角兽企业重金招聘Python工程师标准>>> ##1. 下载RTOS SDK代码 git clone https://github.com/espressif/esp_io ...
- 乐鑫esp8266学习rtos3.0笔记:分享在 esp8266 C SDK实现冷暖光色温平滑调节的封装,轻松集成到您的项目去。(附带Demo)
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,不做开发板.仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 基于C SDK的ESP8266开发技术全系列笔记 一.N ...
- Esp8266 进阶之路36【外设篇】乐鑫esp8266芯片SDK编程驱动时间芯片 ds1302,同步网络时间到本地,再也不怕掉电断网也可以同步时间了!(附带Demo)
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1. Esp8266之 搭建开发环境,开始一个"hello ...
- 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 ...
- 5. ESP8266固件的编译(RTOS SDK固件)
在RTOS SDK下,除了用户程序入口函数名字是user_init()以外, 整个的编程感觉很像linux(当然具体是非常不一样的)下编程,也有tcp/ip协议栈,就像传统的C开发. 1)固件代码准备 ...
最新文章
- OpenCV参考手册之Mat类详解1
- 计算机网络基础 单选题) 作业,南开大学《计算机网络基础》在线作业及答案
- 14.深度学习练习:Face Recognition for the Happy House
- 分步表单_表单设计-掌握表单设计方法(表单体验篇)
- uC/OS 的任务调度解析
- 微软官网真的是一个神奇的地方,高清壁纸,直接下载
- OSG仿真案例(9)——JY61陀螺仪控制飞机姿态
- 拓端tecdat|适用于NLP自然语言处理的Python:使用Facebook FastText库
- java定义vip顾客继承顾客_Java初级教频教程 - JavaSE - Java - 私塾在线 - 只做精品视频课程服务...
- Android studio配置Google play服务
- 三星c7 linux驱动,三星c7驱动|三星c7手机驱动下载 v1.5.55.0 官方版 - 比克尔下载
- PLC的PNP和NPN概念
- 如何将电脑上的音乐导入iphone,怎样将电脑音乐导入苹果手机中
- 简述计算机的含义是什么,输入法全拼和双拼是什么意思?有什么区别?
- 如何集成支付宝到电脑网站
- 阿里云 vps云监控插件已停止状态解决方法
- 如何免费恢复电脑上误删除的视频
- html5文本溢出应该怎么处理?
- python量化策略——大类资产配置模型(最小方差模型)
- xinxin - 初步学习tkinter
热门文章
- MySQL中的limit分页优化
- 关于IBV_WR_RDMA_WRITE_WITH_IMM的理解
- 爱前端视频课程全套 初级+中级+高级
- 徐小明:周三操作策略
- Python(面向对象5——高级)
- Freemake Video Converter 免费优秀的万能视频格式转换工具 (支持CUDA/DXVA显卡加速)
- mk16i android 8,以柔克刚 索爱MK16i/摩托罗拉XT883对比
- 如何在NS2中产生和使用Poisson Traffic
- 新星计划Day7【数据结构与算法】 栈Part1
- TQ2440nand flashi浅谈