首页 > 分享 > MQTT C Cient的实现

MQTT C Cient的实现

  在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译。俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过程。

安装

  本文是在Linux下安装的,推荐直接进行克隆并安装即可。

git clone https://github.com/eclipse/paho.mqtt.c.git cd paho.mqtt.c make sudo make install1234

  在make完之后,在paho.mqtt.c/build/output下可以找到如下的输出文件:

输出的文件

  而make install则是将生成的库文件移动到系统路径之下。在MQTT Client library for C 这个翻译的文章中,Paho给出的创建一个客户端有如下类似的步骤:

  1.创建一个客户端对象;
  2.设置连接MQTT服务器的选项;
  3.如果多线程(异步模式)操作被使用则设置回调函数(详见 Asynchronous >vs synchronous client applications);
  4.订阅客户端需要接收的任意话题;
  5.重复以下操作直到结束:
    a.发布客户端需要的任意信息;
    b.处理所有接收到的信息;
  6.断开客户端连接;
  7.释放客户端使用的所有内存。

  为了简单起见,我们使用Paho自带的示例程序。打开paho.mqtt.c/src/samples下的MQTTClient_publish .c文件。将以下的代码更改:

#define ADDRESS “tcp://m2m.eclipse.org:1883”
#define CLIENTID “ExampleClientPub”
#define TOPIC “MQTT Examples”
#define PAYLOAD “Hello World!”

  如果你的MQTT服务器不允许匿名访问,则还需要添加姓名和密码:

char *username= "test_user"; //添加的用户名 char *password = "aaa777"; //添加的密码12

  并将用户名和密码写入连接选项中:

conn_opts.username = username; //将用户名写入连接选项中 conn_opts.password = password; //将密码写入连接选项中12

  添加的代码具体位置详细查看代码,更改后的代码如下所示:

/******************************************************************************* * Copyright (c) 2012, 2017 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Ian Craggs - initial contribution *******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "MQTTClient.h" #if !defined(WIN32) #include <unistd.h> #else #include <windows.h> #endif #define ADDRESS "tcp://localhost:1883" //更改此处地址 #define CLIENTID "aaabbbccc" //更改此处客户端ID #define TOPIC "topic01" //更改发送的话题 #define PAYLOAD "Hello Man, Can you see me ?!" //更改信息内容 #define QOS 1 #define TIMEOUT 10000L int main(int argc, char* argv[]) { //声明一个MQTTClient MQTTClient client; char *username= "test_user"; //添加的用户名 char *password = "aaa777"; //添加的密码 //初始化MQTT Client选项 MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; //#define MQTTClient_message_initializer { {'M', 'Q', 'T', 'M'}, 0, 0, NULL, 0, 0, 0, 0 } MQTTClient_message pubmsg = MQTTClient_message_initializer; //声明消息token MQTTClient_deliveryToken token; int rc; //使用参数创建一个client,并将其赋值给之前声明的client MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; conn_opts.username = username; //将用户名写入连接选项中 conn_opts.password = password;//将密码写入连接选项中 //使用MQTTClient_connect将client连接到服务器,使用指定的连接选项。成功则返回MQTTCLIENT_SUCCESS if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { printf("Failed to connect, return code %dn", rc); exit(EXIT_FAILURE); } pubmsg.payload = PAYLOAD; pubmsg.payloadlen = strlen(PAYLOAD); pubmsg.qos = QOS; pubmsg.retained = 0; MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); printf("Waiting for up to %d seconds for publication of %sn" "on topic %s for client with ClientID: %sn", (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID); rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); printf("Message with delivery token %d deliveredn", token); MQTTClient_disconnect(client, 10000); MQTTClient_destroy(&client); return rc; }

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374

  更改完成之后回到paho.mqtt.c目录,执行make,输出一下结果:
输出结果

  打开paho.mqtt.c/build/output/samples目录,刚刚我们修改的MQTTClient_publish .c文件生成了MQTTClient_publish ,执行以下命令:

./MQTTClient_publish1

  输出以下结果:

make输出结果

  为了确认发送的信息已经到达客户端,我打开了mqtt.fx客户端,并连接到MQTT服务器,订阅了topic01的话题,此时可以看到发送过来的信息:
mqtt.fx收到的信息
  至此,Paho - MQTT C 发送Cient已经实现了,后续我会详细的讲解一下这个过程。

相关知识

基于MQTT的智能宠物追踪系统
MeowBot:ESP32 语音控制宠物猫 DIY 教程——玩转语音识别与 MQTT 智能家居控制 (附代码解析)
基于STM32设计的智能鱼缸
七彩神仙鱼智能养殖系统设计与实现
基于M5stack远程宠物投喂器
基于STM32设计的智能鱼缸(华为云IOT)(200)
【物联网毕设】智能宠物投喂器
基于STM32的可编程自动宠物喂食系统设计
一种爬宠饲养管理系统
x==y相等

网址: MQTT C Cient的实现 https://m.mcbbbk.com/newsview1338205.html

所属分类:萌宠日常
上一篇: Apache安装apr和apr
下一篇: 阿斌宠龟