stm32移植paho_[嵌入式开发模块]MQTT开源库Paho嵌入式C/C++版本的移植及使用(基于W5500 io库)-Go语言中文社区...

本文介绍了如何在STM32上移植和使用Paho MQTT开源库的嵌入式C/C++版本。首先,概述了Paho库的不同版本及其特性,并指出嵌入式版本仅支持阻塞API。接着,详细描述了从GitHub获取最新库文件并替换现有文件的过程,以及移植过程中需要修改的接口文件`mqtt_interface.h`和`mqtt_interface.c`。最后,文章提供了一个简单的移植示例,包括网络结构体`Network`和定时器`Timer`的实现,以及如何将这些接口与STM32和W5500芯片相结合进行MQTT通信。
摘要由CSDN通过智能技术生成

前言

最近成功使用W5500实现了MQTT客户端,进行一个记录。

W5500的io库下载下来后在Internet文件夹下有MQTT文件夹。

里头差不多就长这个样子。

其中,mqtt_interface两个文件是移植用的接口文件。而其他的文件实际上就是Paho开源库的Embedded C/C++ 版本的文件。

Paho

Paho是MQTT的官方开源库,其有很多版本,各版本之间的特性比较如下:

可以看到,我们要讲的嵌入式版本(最后一行)是其中特性最少的;这很正常,受限于设备的能力,肯定要精简掉一些特性。注意,虽然这张图上把非阻塞API那一项勾上了,但实际上嵌入式版本只有阻塞式API,可能这里是个笔误。

在进一步研究之前,请先下载Embedded C的最新版库;即使你用的是W5500的io库,里头已经有相关文件了,也最好更新成最新版的。稍微有点差别。

https://github.com/eclipse/paho.mqtt.embedded-c

点进去上面的链接,下载下来一堆文件,我们只需要MQTTPacket/src这个文件夹以及MQTTClient-C/src里的MQTTClient.h和MQTTClient.c。

记得在MQTTClient.h里略做修改。在

#include “MQTTPacket.h”

后面加上一行:

#include “mqtt_interface.h”

网上对C和C++版本的讲解有很多,也很全面,但是对Embedded版本的讲解却几乎没有。下面由我进行讲解。

接口文件

方便起见,我们这样:先添加进去以下两个文件,这两文件是在我的环境下移植好的,但是在你的环境下会有一点点小错误的;但是不打紧,之后我们对其进行修改以完成移植。

mqtt_interface.h

//*****************************************************************************

//! file mqtt_interface.h

//! brief Paho MQTT to WIZnet Chip interface Header file.

//! details The process of porting an interface to use paho MQTT.

//! version 1.0.0

//! date 2016/12/06

//! par Revision history

//! <2016/12/06> 1st Release

//!

//! author Peter Bang & Justin Kim

//! copyright

//!

//! Copyright (c) 2016, WIZnet Co., LTD.

//! All rights reserved.

//!

//! Redistribution and use in source and binary forms, with or without

//! modification, are permitted provided that the following conditions

//! are met:

//!

//! * Redistributions of source code must retain the above copyright

//! notice, this list of conditions and the following disclaimer.

//! * Redistributions in binary form must reproduce the above copyright

//! notice, this list of conditions and the following disclaimer in the

//! documentation and/or other materials provided with the distribution.

//! * Neither the name of the nor the names of its

//! contributors may be used to endorse or promote products derived

//! from this software without specific prior written permission.

//!

//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE

//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF

//! THE POSSIBILITY OF SUCH DAMAGE.

//

//*****************************************************************************

/* MQTT subscribe Example.... W5500 + STM32F103(IoT board)

//Include: Board configuration

#include "IoTEVB.h"

//Include: MCU peripheral Library

#include "stm32f10x_rcc.h"

#include "stm32f10x.h"

//Include: W5500 iolibrary

#include "w5500.h"

#include "wizchip_conf.h"

#include "misc.h"

//Include: Internet iolibrary

#include "MQTTClient.h"

//Include: MCU Specific W5500 driver

#include "W5500HardwareDriver.h"

//Include: Standard IO Library

#include

//Socket number defines

#define TCP_SOCKET0

//Receive Buffer Size define

#define BUFFER_SIZE2048

//Global variables

unsigned char targetIP[4] = {}; // mqtt server IP

unsigned int targetPort = 1883; // mqtt server port

uint8_t mac_address[6] = {};

wiz_NetInfo gWIZNETINFO = { .mac = {}, //user MAC

.ip = {}, //user IP

.sn = {},

.gw = {},

.dns = {},

.dhcp = NETINFO_STATIC};

unsigned char tempBuffer[BUFFER_SIZE] = {};

struct opts_struct

{

char* clientid;

int nodelimiter;

char* delimiter;

enum QoS qos;

char* username;

char* password;

char* host;

int port;

int showtopics;

} opts ={ (char*)"stdout-subscriber", 0, (char*)"n", QOS0, NULL, NULL, targetIP, targetPort, 0 };

// @brief messageArrived callback function

void messageArrived(MessageData* md)

{

unsigned char testbuffer[100];

MQTTMessage* message = md->message;

if (opts.showtopics)

{

memcpy(testbuffer,(char*)message->payload,(int)message->payloadlen);

*(testbuffer + (int)message->payloadlen + 1) = "n";

printf("%srn",testbuffer);

}

if (opts.nodelimiter)

printf("%.*s", (int)message->payloadlen, (char*)message->payload);

else

printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);

}

// @brief 1 millisecond Tick Timer setting

void NVIC_configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

SysTick_Config(72000);

NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Highest priority

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

// @brief 1 millisecond Tick Timer Handler setting

void SysTick_Handler(void)

{

MilliTimer_Handler();

}

int main(void)

{

led_ctrl led1,led2;

int i;

int rc = 0;

unsigned char buf[100];

//Usart initialization for Debug.

USART1Initialze();

printf("USART initialized.nr");

I2C1Initialize();

printf("I2C initialized.nr");

MACEEP_Read(mac_address,0xfa,6);

printf("Mac addressnr");

for(i = 0 ; i < 6 ; i++)

{

printf("%02x ",mac_address[i]);

}

printf("nr");

//LED initialization.

led_initialize();

led1 = led2 = ON;

led2Ctrl(led2);

led1Ctrl(led1);

//W5500 initialization.

W5500HardwareInitilize();

printf("W5500 hardware interface initialized.nr");

W5500Initialze();

printf("W5500 IC initialized.nr");

//Set network informations

wizchip_setnetinfo(&gWIZNETINFO);

setSHAR(mac_address);

print_network_information();

Network n;

MQTTClient c;

NewNetwork(&n, TCP_SOCKET);

ConnectNetwork(&n, targetIP, targetPort);

MQTTClientInit(&c,&n,1000,buf,100,tempBuffer,2048);

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;

data.willFlag = 0;

data.MQTTVersion = 3;

data.clientID.cstring = opts.clientid;

data.username.cstring = opts.username;

data.password.cstring = opts.password;

data.keepAliveInterval = 60;

data.cleansession = 1;

rc = MQTTConnect(&c, &data);

printf("Connected %drn", rc);

opts.showtopics = 1;

printf("Subscribing to %srn", "hello/wiznet");

rc = MQTTSubscribe(&c, "hello/wiznet", opts.qos, messageArrived);

printf("Subscribed %drn", rc);

while(1)

{

MQTTYield(&c, data.keepAliveInterval);

}

}

*/

#ifndef __MQTT_INTERFACE_H_

#define __MQTT_INTERFACE_H_

/*

* @brief MQTT MilliTimer handler

* @note MUST BE register to your system 1m Tick timer handler

*/

void MilliTimer_Handler(void);

/*

* @brief Timer structure

*/

typedef struct Timer Timer;

struct Timer {

unsigned long systick_period;

unsigned long end_time;

};

/*

* @brief Network structure

*/

typedef struct Network Network;

struct Network

{

int my_socket;

int (*mqttread) (Network*, unsigned char*, int, int);

int (*mqttwrite) (Network*, unsigned char*, int, int);

void (*disconnect) (Network*);

};

/*

* @brief Timer function

*/

void TimerInit(Timer*);

char TimerIsExpired(Timer*);

void TimerCountdownMS(Timer*, unsigned int);

void TimerCountdown(Timer*, unsigned int);

int TimerLeftMS(Timer*);

/*

* @brief Network interface porting

*/

int w5x00_read(Network*, unsigned char*, int, int);

int w5x00_write(Network*, unsigned char*, int, int);

void w5x00_disconnect(Network*);

void NewNetwork(Network* n, int sn);

int ConnectNetwork(Network*, char*, int);

#endif//__MQTT_INTERFACE_H_

mqtt_interface.c

//*****************************************************************************

//! file mqtt_interface.c

//! brief Paho MQTT to WIZnet Chip interface implement file.

//! details The process of porting an interface to use paho MQTT.

//! version 1.0.0

//! date 2016/12/06

//! par Revision history

//! <2016/12/06> 1st Release

//!

//! author Peter Bang & Justin Kim

//! copyright

//!

//! Copyright (c) 2016, WIZnet Co., LTD.

//! All rights reserved.

//!

//! Redistribution and use in source and binary forms, with or without

//! modification, are permitted provided that the following conditions

//! are met:

//!

//! * Redistributions of source code must retain the above copyright

//! notice, this list of conditions and the following disclaimer.

//! * Redistributions in binary form must reproduce the above copyright

//! notice, this list of conditions and the following disclaimer in the

//! documentation and/or other materials provided with the distribution.

//! * Neither the name of the nor the names of its

//! contributors may be used to endorse or promote products derived

//! from this software without specific prior written permission.

//!

//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE

//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF

//! THE POSSIBILITY OF SUCH DAMAGE.

//

//*****************************************************************************

#include "mqtt_interface.h"

#include "socket.h"

#include "MyOS.h"

unsigned long MilliTimer;

/*

* @brief MQTT MilliTimer handler

* @note MUST BE register to your system 1m Tick timer handler.

*/

void MilliTimer_Handler(void) {

MilliTimer++;

}

/*

* @brief Timer Initialize

* @param timer : pointer to a Timer structure

* that contains the configuration information for the Timer.

*/

void TimerInit(Timer* timer) {

timer->end_time = 0;

}

/*

* @brief expired Timer

* @param timer : pointer to a Timer structure

* that contains the configuration information for the Timer.

*/

char TimerIsExpired(Timer* timer) {

long left = timer->end_time - MilliTimer;

return (left < 0);

}

/*

* @brief Countdown millisecond Timer

* @param timer : pointer to a Timer structure

* that contains the configuration information for the Timer.

* timeout : setting timeout millisecond.

*/

void TimerCountdownMS(Timer* timer, unsigned int timeout) {

timer->end_time = MilliTimer + timeout;

}

/*

* @brief Countdown second Timer

* @param timer : pointer to a Timer structure

* that contains the configuration information for the Timer.

* timeout : setting timeout millisecond.

*/

void TimerCountdown(Timer* timer, unsigned int timeout) {

timer->end_time = MilliTimer + (timeout * 1000);

}

/*

* @brief left millisecond Timer

* @param timer : pointer to a Timer structure

* that contains the configuration information for the Timer.

*/

int TimerLeftMS(Timer* timer) {

long left = timer->end_time - MilliTimer;

return (left < 0) ? 0 : left;

}

/*

* @brief New network setting

* @param n : pointer to a Network structure

* that contains the configuration information for the Network.

* sn : socket number where x can be (0..7).

* @retval None

*/

void NewNetwork(Network* n, int sn) {

n->my_socket = sn;

n->mqttread = w5x00_read;

n->mqttwrite = w5x00_write;

n->disconnect = w5x00_disconnect;

}

/*

* @brief read function

* @param n : pointer to a Network structure

* that contains the configuration information for the Network.

* buffer : pointer to a read buffer.

* len : buffer length.

*/

int w5x00_read(Network* n, unsigned char* buffer, int len, int timeout_ms){

Timer tmr;

TimerInit(&tmr);

TimerCountdownMS(&tmr, timeout_ms);

while(!TimerIsExpired(&tmr)){

if(getSn_SR(n->my_socket) != SOCK_ESTABLISHED)

return -1;

if(getSn_RX_RSR(n->my_socket)>0)

return recv(n->my_socket, buffer, len);

MyOS_DlyHMSM(0,0,0,30);

}

return 0;

}

/*

* @brief write function

* @param n : pointer to a Network structure

* that contains the configuration information for the Network.

* buffer : pointer to a read buffer.

* len : buffer length.

*/

int w5x00_write(Network* n, unsigned char* buffer, int len, int timeout_ms){

return send(n->my_socket, buffer, len);

}

/*

* @brief disconnect function

* @param n : pointer to a Network structure

* that contains the configuration information for the Network.

*/

void w5x00_disconnect(Network* n){

disconnect(n->my_socket);

}

/*

* @brief connect network function

* @param n : pointer to a Network structure

* that contains the configuration information for the Network.

* ip : server iP.

* port : server port.

*/

int ConnectNetwork(Network* n, char* ip, int port)

{

uint8_t myport = 12345;

socket(n->my_socket,Sn_MR_TCP,myport,0);

connect(n->my_socket,ip,port);

}

以上两个文件mqtt_interface.h和mqtt_interface.c不完全是io库自带的那个,是基于我的环境进行了一定的修改后移植完成后的版本。原来的版本有一些根本性的问题,所以不要嫌麻烦,动动小手指 唱跳rap篮球+cv 一下,先替换掉原来的文件。

移植

下面我们来看看怎么把这个文件移植到你的嵌入式系统中。移植的主要工作就是为MQTT库实现一个定时器Timer类以及一个网络Network类。

定时器Timer类

打开MQTTClient.h可以看到库对Timer类的要求:

/* The Timer structure must be defined in the platform specific header,

* and have the following functions to operate on it. */

extern void TimerInit(Timer*);

extern char TimerIsExpired(Timer*);

extern void TimerCountdownMS(Timer*, unsigned int);

extern void TimerCountdown(Timer*, unsigned int);

extern int TimerLeftMS(Timer*);

这几个接口的作用分别是:

TimerInit:初始化一个Timer实例

TimerIsExpired:返回定时器是否超时

TimerCountdownMS:设定过多少ms超时

TimerCountdown:设定过多少s超时

TimerLeftMS:返回还剩多少ms超时

上面的接口文件已经实现了这些接口,其原理是内部用了一个计时用的全局变量,每1ms应该加1,而Timer内部成员则保存计时的起始时间和超时时间。通过内部变量与Timer内部成员的比较,算出是否超时等。这个实现与操作系统等无关,所以应该可以直接用。

但是要记得实现每1ms调用一次以下接口。比如用一个计时中断。

/*

* @brief MQTT MilliTimer handler

* @note MUST BE register to your system 1m Tick timer handler

*/

void MilliTimer_Handler(void);

网络Network类

同样在MQTTClient.h中可以看到库对Network类的要求:

/* The Platform specific header must define the Network and Timer structures and functions

* which operate on them.

*

typedef struct Network

{

int (*mqttread)(Network*, unsigned char* read_buffer, int, int);

int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int);

} Network;*/

简单来说就是MQTT库将网络接口抽象为了Network类。我要好好夸夸这个接口设计,它非常成功地将MQTT实现与网络的具体实现解耦了。网络相关的信息由用户封装到自己定义的Network类之中,库只管在需要的时候回调Network类的几个方法来实现网络功能。如果你用纯C做过面向对象编程的话应该会比较好理解。

我们来看看Network结构体在这个移植版本中的定义:

typedef struct Network Network;

struct Network

{

int my_socket;

int (*mqttread) (Network*, unsigned char*, int, int);

int (*mqttwrite) (Network*, unsigned char*, int, int);

void (*disconnect) (Network*);

};

可以看到它在要求的结构体上还进行了一定的扩展。

第一个成员my_socket用于存储当前TCP链接的socket号,对应于io库中各个接口的第一个参数。

后面三个则定义了网络的读、写以及断连接口。MQTT库在内部会调用Network实例的对应接口,并把Network实例自身作为第一个参数传入,以实现对应的功能,而接口则可以通过实例的第一个成员来区分要操作的是哪一个socket。

于是我们可以看到NewNetwork的实现是这样的:

void NewNetwork(Network* n, int sn) {

n->my_socket = sn;

n->mqttread 版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/lin_strong/article/details/92410440

站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

发表于 2020-05-11 22:54

阅读 ( 484 )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值