9

玩转LiteOS组件:玩转Librws

 2 years ago
source link: https://juejin.cn/post/7088521318961774600
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文分享自华为云社区《LiteOS组件尝鲜-玩转Librws》,作者: W922 。

本期为大家带来LiteOS开源版本Librws 组件的使用说明,请跟随小编的步伐一起进入探索之旅吧!

Librws是一个跨平台的websocket客户端,使用C语言编写。它有如下特点:

  • 无其他依赖关系
  • 带有公共方法的单独的公共方法库librws.h
  • 后台线程中的发送/接收逻辑

详情参考:github.com/OlehKulykov…

WebSocket是一种在单个TCP连接上进行全双工通信的协议。

详情参考:baike.baidu.com/item/WebSoc…

使能Librws Demo

  • 在LiteOS源码根目录下根据实际使用的开发板,拷贝tools/build/config/目录下的默认配置文件${platform}.config到根目录,并重命名为**.config。**

​ 注意:根据Librws所需资源,暂时只支持在STM32F769开发板上运行LibrwsDemo。

  • 在LiteOS源码根目录下执行make menuconfig命令,按如下菜单路径使能Librws Demo
Demos  --->
   Librws Demo  --->
          [*] Enable Librws Demo
复制代码

​ 注意:使能Librws Demo后会自动使能Librws组件和LWIP。

  • 保存退出后,会从github上自动下载Librws源代码,并从gitee上下载适配于LiteOS系统的patch包,并打入patch。详细流程在components/download.sh文件中。

​ 注意: 下载需要Linux主机可以访问互联网,并安装git。

编译运行Librws Demo

服务端配置

服务端代码路径为LiteOS/demos/librws/websocketserver.py,需要python3环境可运行。启动服务端步骤如下(命令行操作):

  • 修改服务端代码中的变量的ip为服务器ip地址。
# creat websockets server, please change ip as your ip address.
ip = ''
startServer = websockets.serve(MainLogic, ip, 8000)
复制代码
  • 通过python pip命令安装websocket包。
pip install websockets
复制代码
  • 运行websocketserver.py脚本。
python websocketserver.py
复制代码

客户端配置

Librws Demo需要网络连接,在STM32F769开发板插入网线,并确保网络连接。客户端操作步骤如下:

  • 修改LiteOS/demos/librws/librws_demo.c中的WEBSOCKET_DEMO_SERVER为服务端IP或域名。
#define TEXT_BUFF_LEN                   1024
#define BIN_BUFF_LEN                    1024
#define WEBSOCKET_DEMO_SERVER           "your.ip.address"
#define WEBSOCKET_DEMO_PORT             8000
#define LIBRWS_TASK_PRIORITY            6
#define LIBRWS_TASK_STACK_SIZE          0x1400
复制代码

注意: 改为域名需要开启LWIP_DHCP(DNS需要DHCP来初始化,在Linux下编译,使能LWIP 会默认开启DHCP)。

  • 修改Kernel中默认任务栈大小。在LiteOS源码根目录下执行make menuconfig命令,按如下菜单路径设置,修改为4096
Kernel  --->
    Basic Config  --->
        Task  --->
            (4096) Task Defalut Stack Size
复制代码

编译烧录

  • 在LiteOS源码根目录下执行make clean; make -j命令编译LiteOS工程,编译成功后会在out/{platform}/lib路径下生成liblibrws.aliblibrws_demo.a文件,系统镜像文件为Huawei_LiteOS.bin。
  • 烧录操作请参考gitee.com/LiteOS/Lite…
  • 启动LiteOS后会先初始化网络,开启DHCP,然后运行Librws Demo,建立websocket连接并向服务端发送数据,服务端接收到数据后会返回数据,运行结果如下:
********Hello Huawei LiteOS********
LiteOS Kernel Version : 5.1.0
build data : Jul 14 2021 17:15:39
**********************************
OsAppInit
cpu 0 entering scheduler
app init!
Hello, welcome to liteos demo!
lwip_init OK!!
dhcp start...
Librws demo start to run.
Huawei LiteOS #
Websocket connected
Websocket receive text:
your submit context: LiteOS Websocket demo data
复制代码

以上为“Librws”使用说明的全部内容,欢迎大家学习交流。

未来我们还会持续新增更多组件、开发板、架构、特性等。

感谢您的阅读,有任何问题、建议,都可以留言给我们,让我们一起进步:

gitee.com/LiteOS/Lite…

为了更容易找到“LiteOS”代码仓,建议访问**gitee.com/LiteOS/Lite… Watch”、点赞“Star”、并“Fork”到自己账号下,如图所示。

11.jpg

点击关注,第一时间了解华为云新鲜技术~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK