当前位置:网站首页>Transplant MQTT source code to STM32F407 development board
Transplant MQTT source code to STM32F407 development board
2022-08-01 21:03:00 【luobeihai】
1. 准备工作
注意:The article doesn't cover porting yetmqttclientCode for parts related to hardware device networking,So after this article is ported,Can't connect via network yet.The main introduction is to put firstmqttclientAll code except for the network is ported to FreeRTOS项目中,And it can be compiled.
1.1 获取 mqttclient 源码
Our device side is just asMQTT的客户端运行,So we just need to portMQTTClient source code can be.
mqttclient The source code here uses the open source code written by a big guy in China,You can get it from the open source repository address below.

1.2 可以运行FreeRTOS系统的MDK工程模板
这里我使用STM32CubeMXGenerate a runnableFreeRTOSSystem engineering template.You can refer to the following blog post to generate one yourselfFreeRTOS系统模板.
2. 合并mqttclient源码到FreeRTOS
2.1 复制源码到FreeRTOS工程
首先把mqttclient源码解压出来,It contains the following source code directories:
Introduction to the main source code directory:
- common:Common source files,Such as the processing of linked lists,Error code handling, etc.
- mqtt:paho mqtt库文件
- mqttclient:实现mqttclientThe main file for the function
- network:网络抽象层
- platform:平台抽象层.This directory is mainly the operating system platform,And some related platform hardware initialization code,Most closely related to our transplant process.The current porting of this article does not implement the code related to the platform network for the time being.
That's what we're going to transplant5source files in a directory(testTest routine directory file,We can't use it yet,So don't transplant first).
We are creating it ourselvesFreeRTOS工程目录下,新建MQTTClient目录,Then put this on top5copy directory files to thisMQTTClient目录.
2.2 Add relevant code to MDK中
在Keil分组下面,创建和MQTTThe source code is grouped in the same directory.
We first put each source code directory,first-level subdirectorycThe files are added to the group,Then the files in the subfolders inside will not be added for the time being,You can add it later if you need to use it later.

As for the code below the platform directory,我们添加FreeRTOS文件夹里面的代码.Other platform-dependent code does not need to be added.
3. 编译源码解决错误
The general compilation errors are probably in the following categories:
- 找不到头文件,这个非常常见
- Some data types say undefined(Possibly someone else wrote a library that depends on some other file-defined data type,And you just didn't get in)
- Just a grammatical error
- 链接错误,For example, if you use a function,But you didn't define it
Below I record my own compilation process,Some bugs resolved(Not all errors are logged).
找不到头文件
..\MQTTClient\mqttclient\mqttclient.h(16): error: #5: cannot open source input file "mqtt_list.h": No such file or directory这里报错mqtt_list.hThis header file cannot be found,The file is actuallycommon目录下(The source code can be usedsource insight建立工程,Then go inside and find which directory the header file is in).我们在keilJust add the search path of this header file directory inside.

mqtt_config.hThe header files really don't
..\MQTTClient\mqttclient\mqtt_defconfig.h(12): error: #5: cannot open source input file "mqtt_config.h": No such file or directorymqtt_config.hThe files are actually in the source directorytest目录下,But we don't need to add it for nowtest目录下相关的文件,So block the code that includes this header file.

Block code related to network transmission security
..\MQTTClient\mqttclient\mqtt_defconfig.h(77): error: #5: cannot open source input file "mbedtls/config.h": No such file or directory报错找不到
mbedtls/config.h这个头文件,This is the code related to secure transmission,Let's port the simplest code first,Just define a macro mask.
屏蔽lwip相关代码
..\MQTTClient\platform\FreeRTOS\platform_net_socket.h(15): error: #5: cannot open source input file "lwip/opt.h": No such file or directory因为后面会使用ESP8266WIFIThe module conducts network connection experiments,这个模块是使用ATThe command can operate the connection,不需要使用lwip相关代码,So just block itlwipRelevant header file code.

把platform_net_socket.cAll code related to the platform network in the file is deleted,只返回0即可

will be used thenATThe directory implements network operations,Here in order to compile through,Return the function first0.
socklen_t、size_t数据类型没有定义
size_tData type tracking code discovery,在mqtt_log.h头文件中,Including another header file is definedsize_t数据类型的,So we need to make the following adjustments.
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M4lyLjzU-1659238247407)(../picture/image-20220731003537762.png)]](/img/dd/cd0ae5803b77650ec3a836ae575c90.png)
socklen_tIndeed no definition was found for this data type,So we just add the code definition ourselves.![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qh3GOMDQ-1659238247407)(../picture/image-20220731003834348.png)]](/img/c4/ed08cc6f78244f895f2137bd6347d9.png)
语法错误
..\MQTTClient\platform\FreeRTOS\platform_timer.c(15): error: #18: expected a ")" #if (configTICK_RATE_HZ == 1000)![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Oid4iw8B-1659238247407)(../picture/image-20220731102251091.png)]](/img/7f/1c498a036aa07b5b138e872cfa02d2.png)
This statement is really not sure why this error is reported,configTICK_RATE_HZ 这个宏是FreeRTOSThe heartbeat frequency defined in the configuration file.Finally can't figure out why,然后就把FreeRTOSConfig.hThis macro is defined in the file,Just don't use coercion,Change it for now.#define configTICK_RATE_HZ 1000 //((TickType_t)1000)Anonymous struct syntax is not supported
..\MQTTClient\mqttclient\mqttclient.h(79): error: #3093: anonymous structs are only supported in --gnu mode, or when enabled with #pragma anon_unions这个报错说,The syntax for this anonymous struct isgun才支持的.
方法1:可以把keil的 gun The extended syntax is added.
方法2:修改源码.追踪代码发现,The wrong place will enter a branch defined by a macro,We can find a way to let him enter another branch.
在plooc_class.hAdd the following macro definition in front of the header file:
# define PLOOC_CFG_REMOVE_MEMORY_LAYOUT_BOUNDARY___USE_WITH_CAUTION___ // lbh add
After the compilation errors are resolved,As for the compilation passed(Warning not resolved first).
编译通过之后,这个MQTTThe program doesn't work yet,Because the network-related code is written as an empty function,接下来把 platform_net_socket.c 这个cThe network functions in the file are usedESP8266模块,使用ATDirectives port each function.
边栏推荐
猜你喜欢

测试的意义并不是能找到全部的缺陷

30+的女性测试人面试经验分享

图的邻接矩阵存储

Buttons with good user experience should not have hover state on mobile phones

人工智能可信安全与评测

vant实现Select效果--单选和多选

Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data

excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列

【Kaggle】House Prices

Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
随机推荐
[译] 容器和 Kubernetes 中的退出码完整指南
tiup mirror
C陷阱与缺陷 第8章 建议与答案 8.2 答案
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
New graduate students, great experience in reading English literature, worthy of your collection
WeChat applet cloud development | personal blog applet
LinkedList源码分享
图片识别商品接口 API:天猫淘宝
封装一个管理 url 状态的 hook
分类接口,淘宝分类详情 API
图的邻接矩阵存储
淘宝获取收货地址列表的 API
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
Interview assault 70: what is the glue bag and a bag?How to solve?
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
JS提升:如何中断Promise的链式调用
这些 hook 更优雅的管理你的状态
关键字搜索:“淘宝商品 API ”
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
Go Atomic