前言
最近成功使用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"