1. 前言
随着物联网技术的快速发展,MQTT(Message Queuing Telemetry Transport)协议已成为一种广泛使用的通讯协议,它适用于设备间低带宽、高延迟、不可靠的网络通信。W5500是一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,同时也是一颗工业级以太网控制芯片。
在以太网应用中使用 W5500 + MQTT应用协议让用户可以更加方便地在设备之间实现远程连接和通信。本教程将介绍W5500以太网MQTT应用的基本原理、使用步骤、应用实例以及注意事项,帮助读者更好地掌握这一技术。
2. 简介
2.1 什么是MQTT?
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建于TCP/IP协议上。MQTT最大优点在于,用极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。
2.2 特点
- 轻量级:MQTT协议非常轻量,它的开销很小,适用于低带宽、低功耗设备和网络。
- 发布/订阅模式:MQTT采用发布/订阅模式,客户端可以订阅一个或多个主题(Topic),并且可以在不知道其他客户端的情况下发布消息到主题。
- QoS支持:MQTT支持三种服务质量(QoS)等级,可以在发布和订阅时指定。 这些等级为0、1、2,提供不同的消息传递保证。
- 遗嘱机制:MQTT支持遗嘱(Will)机制,当客户端异常断开连接时,可以通过遗嘱机制通知其他客户端。
- 保留消息:MQTT支持保留消息(Retained Message),允许客户端发布一个保留的消息到主题,而后连接到该主题的新订阅者可以立即接收到该消息。
- 连接控制:MQTT支持连接控制机制,允许客户端在连接时指定自己的客户端ID、用户名和密码。
- 安全性:MQTT支持TLS/SSL加密通信协议,以确保数据的安全传输。
- 基于 TCP/IP 网络连接
2.3 MQTT协议实现方式
实现MQTT协议需要客户端和服务器端通讯完成,在通讯过程中,MQTT协议中有三种身份:发布者(Publish)、服务器、订阅者(Subscribe)。其中,消息的发布者和订阅者都是客户端,消息代理是服务器,消息发布者可以同时是订阅者。
MQTT传输的消息分为:主题(Topic)和负载(payload)两部分:
(1)Topic,可以理解为消息的类型,订阅者订阅(Subscribe)后,就会收到该主题的消息内容(payload);
(2)payload,可以理解为消息的内容,是指订阅者具体要使用的内容。
2.4 遗嘱消息
客户端的遗嘱只在意外断线时才会发布,如果客户端正常的断开了与服务端的连接,这个遗嘱机制是不会启动的,服务端也不会将客户端的遗嘱公布。
遗嘱消息可以看作是一个简化版的 PUBLISH 消息,他也包含 Topic, Payload, QoS 等字段。遗嘱消息会在设备与服务端连接时,通过 CONNECT 报文指定,然后在设备意外断线时由服务端将该遗嘱消息发布到连接时指定的遗嘱主题(Will Topic)上。
一般建议设置遗嘱消息内容为client offline!在我们的客户端连接上服务器时,也向遗嘱主题发布一条client online消息。
2.5 应用场景
- 科学研究:在科学研究中,需要对实验数据进行采集和传输,使用MQTT协议可以有效地解决数据传输过程中的丢包问题,保证数据传输的稳定性和完整性。
- 金融行业:在金融行业中,需要对交易信息进行实时监控和传输,使用MQTT协议可以实现实时监控和传输,并保证数据的稳定性和安全性。
- 医疗行业:在医疗行业中,需要对患者的生命体征进行采集和传输,使用MQTT协议可以有效地解决数据传输过程中的丢包问题,保证数据传输的稳定性和完整性。
- 能源行业:在能源行业中,需要对能源设备进行监控和控制,MQTT协议正是满足这一需求而设计的,它具有简单、可靠、灵活等特点,在能源领域中得到了广泛应用。
3 WIZnet以太网芯片
WIZnet 主流硬件协议栈以太网芯片参数对比
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max.25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max.25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 15Mbps |
- W5100S/W6100 支持 8bit数据总线接口,网络传输速度会优于W5500。
- W6100 支持IPv6,与W5100S 硬件兼容,若已使用W5100S的用户需要支持IPv6,可以Pin to Pin兼容。
- W5500 拥有比 W5100S更多的 socket数量以及发送与接收缓存。
4 MQTTClient网络设置示例概述以及使用
4.1 流程图
程序的运行框图如下所示:
4.2 准备工作核心
软件
- Visual Studio Code
- WIZnet UartTool
- MQTTX
硬件
- W5100S IO模块 + RP2040 树莓派Pico开发板 或者 WIZnet W5100S-EVB-Pico开发板
- Micro USB 接口的数据线
- TTL 转 USB
- 网线
4.3 连接方式
-
通过数据线连接PC的USB口(主要用于烧录程序,也可以虚拟出串口使用)
-
通过TTL串口转USB,连接UART0 的默认引脚:
- RP2040 GPIO0(UART0 TX) <----> USB_TTL_RX
- RP2040 GPIO1(UART0 RX) <----> USB_TTL_TX
-
使用模块连接RP2040 进行接线时
- RP2040 GPIO16 <----> W5100S MISO
- RP2040 GPIO17 <----> W5100S CS
- RP2040 GPIO18 <----> W5100S SCK
- RP2040 GPIO19 <----> W5100S MOSI
- RP2040 GPIO20 <----> W5100S RST
-
通过PC和设备都通过网线连接路由器LAN口
4.4 主要代码概述
我们使用的是WIZnet官方的ioLibrary_Driver库。该库支持的协议丰富,操作简单,芯片在硬件上集成了TCP/IP协议栈,该库又封装好了TCP/IP层之上的协议,我们只需简单调用相应函数即可完成协议的应用。
第一步:mqtt_client.c文件中加入对应的.h文件。
第二步:定义DHCP所要的宏和MQTT收发缓存buff的宏。
第三步:定义一个mqtt连接参数的结构体并进行定义。
第四步: 初始化了库内部的参数。
第五步:网络信息的配置和mqtt初始的参数,以及标志位。
第六步:编写定时器回调处理函数,用于DHCP和MQTT 1S滴答定时器处理函数。
第七步:主函数先是对串口和SPI的初始化,然后写入W5100S的网络配置参数,初始化DHCP后开始DHCP获取IP,获取到就打印获取到的IP,获取次数超过最大获取次数时就使用静态IP,之后初始化MQTT,然后主循环是一个状态机的轮询,状态机先是进入到连接状态,当连接成功之后状态才开始进行发布和订阅,然后到订阅遗嘱消息,最后进行心跳包的处理,防止掉线。
#include "wizchip_conf.h"
#include "w5100s.h"
#include "bsp_spi.h"
#include "socket.h"
#include "loopback.h"
#include "MQTTClient.h"
#include "mqtt_interface.h"
#include "dns.h"
#include "string.h"
#include "dhcp.h"
#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
#define MQTT_SEND_BUFF_SIZE 2048 /*Send cache size*/
#define MQTT_RECV_BUFF_SIZE 2048 /*Recv cache size*/
#define DHCP_RETRY_COUNT 5 // DHCP retry times
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
enum status
{
CONN= 0,
SUB,
PUB_ONLINE,
KEEPALIVE,
ERROR = 255,
} run_status;
uint8_t mqtt_send_buff[MQTT_SEND_BUFF_SIZE] = {0};
uint8_t mqtt_recv_buff[MQTT_RECV_BUFF_SIZE] = {0};
typedef struct MQTTCONNECTION
{
char mqttHostUrl[1024]; /*Server URL*/
int port; /*Server port number*/
char clientid[1024]; /*client ID*/
char username[1024]; /*user name*/
char passwd[1024]; /*user passwords*/
uint8_t server_ip[4]; /*Server IP*/
char pubtopic[255]; /*publication*/
char subtopic[255]; /*subscription*/
int pubQoS; /* publishing messages*/
int subQoS; /* subscription messages*/
char willtopic[255]; /*Will topic */
int willQoS; /*Will message */
char willmsg[255]; /*Will */
} mqttconn;
mqttconn mqtt_params = {
.server_ip = {54,244,173,190}, /*Define the Connection Server IP*/
.port = 1883, /*Define the connection service port number*/
.clientid = "hxrzyBEzRJz.MQTT1|securemode=2,signmethod=hmacsha256,timestamp=1698648840189|", /*Define the client ID*/
.username = "MQTT1&hxrzyBEzRJz", /*Define the user name*/
.passwd = "21733721029051630f5f86efa6860403cbc68cda27223f83ec9175f69243ee93", /*Define user passwords*/
.pubtopic = "W5100S_pubtopic", /*Define the publication message*/
.subtopic= "W5100S_subtopic", /* vDefine subscription messages*/
.pubQoS = 0, /*Defines the class of service for publishing messages*/
.willtopic = "W5100S_will", /*Define the topic of the will*/
.willQoS = 0, /*Defines the class of service for Will messages*/
.willmsg = "W5100S offline!", /*Define a will message*/
.subQoS = 0, /*Defines the class of service for subscription messages*/
};
MQTTMessage pubmessage = {
.qos = 0,
.retained = 0,
.dup = 0,
.id = 0,
};
MQTTPacket_willOptions willdata = MQTTPacket_willOptions_initializer; /* Will subject struct initialization */
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; /*Define the parameters of the MQTT connection*/
unsigned char *data_ptr = NULL;
/**
* @brief Initialization of chip network information
* @param conf_info :Static configuration information
* @return none
*/
void network_init(wiz_NetInfo *conf_info);
wiz_NetInfo net_info = {
.mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e}, /*Define the W5100S default mac address*/
.ip = {192, 168, 1, 123}, /*Define the W5100S default IP*/
.sn = {255, 255, 255, 0}, /*Define the W5100S default subnet mask*/
.gw = {192, 168, 1, 1}, /*Define the W5100S default gateway*/
.dns = {8, 8, 8, 8}, /*Define the W5100S default DNS server*/
.dhcp = NETINFO_DHCP}; /*Define the W5100S as statically acquiring IP mode*/
MQTTClient c = {0};
Network n = {0};
wiz_NetInfo get_info;
static uint8_t breakout_flag = 0; // Define the DHCP acquisition flag
int connOK;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
}; // Send and receive cachestatic uint8_t destip[4]={192, 168, 1, 2}; // udp destination ip
/*
@brief Callback processing after triggering the timer.
@param Timer struct.
@return True.
*/
bool repeating_timer_mqtt_callback(struct repeating_timer *t);
/*
@brief Callback processing after triggering the timer.
@param Timer struct.
@return True.
*/
bool repeating_timer_dhcp_callback(struct repeating_timer *t);
/*
@brief MQTT client and connection initialization functions.
@param None.
@return None.
*/
void mqtt_init(void);
/*
@brief Subscribe to the topic callback function.
@param Message queue.
@return None.
*/
void messageArrived(MessageData *md);
int main()
{
int ret;
struct repeating_timer timer_mqtt;
struct repeating_timer timer_dhcp;
MQTTMessage pubmessage = {0};
/*mcu init*/
stdio_init_all(); /*Initialize the serial port*/
wizchip_initialize(); /*Initialize the SPI*/
/*dhcp init*/
DHCP_init(SOCKET_ID, ethernet_buf); // DHCP initialization
add_repeating_timer_ms(1000, repeating_timer_mqtt_callback, NULL, &timer_mqtt); // Add DHCP 1s Tick Timer handler
printf("wiznet chip mqtt_client example.\r\n");
network_init(&net_info); // Configuring Network Information
print_network_information(&get_info); // Read back the configuration information and print it
/*mqtt init*/
add_repeating_timer_ms(1, repeating_timer_dhcp_callback, NULL, &timer_dhcp); /*Set a timer for one second*/ /*Initialize MQTT*/
mqtt_init();
while (true)
{
switch (run_status)
{
case CONN:
{
ret = MQTTConnect(&c, &data); /* Connect to the MQTT server */
printf("Connect to the MQTT server: %d.%d.%d.%d:%d\r\n", mqtt_params.server_ip[0], mqtt_params.server_ip[1], mqtt_params.server_ip[2], mqtt_params.server_ip[3], mqtt_params.port);
printf("Connected:%s\r\n\r\n", ret == SUCCESSS ? "success" : "failed");
if (ret != SUCCESSS)
{
run_status = ERROR;
}
else
{
run_status = SUB;
}
break;
}
case SUB:
{
ret = MQTTSubscribe(&c, mqtt_params.subtopic, mqtt_params.subQoS, messageArrived); /* Subscribe to Topics */
printf("Subscribing to %s\r\n", mqtt_params.subtopic);
printf("Subscribed:%s\r\n\r\n", ret == SUCCESSS ? "success" : "failed");
if (ret != SUCCESSS)
{
run_status = ERROR;
}
else
{
run_status = PUB_ONLINE;
}
run_status = PUB_ONLINE;
break;
}
case PUB_ONLINE:
{
pubmessage.qos = 0;
pubmessage.payload = "W5100S online!";
pubmessage.payloadlen = strlen(pubmessage.payload);
ret = MQTTPublish(&c, mqtt_params.willtopic, &pubmessage); /* Publish message */
if (ret != SUCCESSS)
{
run_status = ERROR;
}
else
{
printf("publish:%s,%s\r\n\r\n", mqtt_params.willtopic, pubmessage.payload);
run_status = KEEPALIVE;
}
break;
}
case KEEPALIVE:
{
if (MQTTYield(&c, 30) != SUCCESSS) /* keepalive MQTT */
{
run_status = ERROR;
}
sleep_ms(100);
break;
}
case ERROR: /* Running error */
printf("system ERROR!");
sleep_ms(1000);
break;
default:
break;
}
}
}
4.5 结果演示
1.打开WIZ UartTool,填入参数:选择串口对应的com port,波特率115200,8位数据位,1位停止位,无校验位,无流控,填完参数后点击open打开。
2.打开MQTTX软件,创建一个客户端,与我们的设备连上同一个MQTT服务器,将订阅设置为我们设备的发布,发布设置为订阅,然后进行发布消息,发现串口会收到mqttx发送的消息,并将消息重新发布给mqttx。
5 注意事项
- 发布和订阅不要弄反了 ,弄反会导致收不到消息。
- 发布和消息的主题不要过于简单,这样很可能导致其他使用mqttx的客户乱入。
- 如果想用WIZnet的W5500来实现本章的示例,我们只需修改两个地方即可:
(1)在library/ioLibrary_Driver/Ethernet/下找到wizchip_conf.h这个头文件,将_WIZCHIP_ 宏定义修改为W5500。
(2)在library下找到CMakeLists.txt文件,将COMPILE_SEL设置为ON即可,OFF为W5100S,ON为W5500。
6 相关链接
想了解更多,评论留言哦!