Eclipse Paho:MQTT Client C的使用

简介: Eclipse Paho:MQTT Client C的使用

https://www.eclipse.org/paho/downloads.php


eclipse/paho.mqtt.c库(使用v1.3.0)的下载地址是:https://github.com/eclipse/paho.mqtt.c/tree/v1.3.0


1、paho.mqtt.c-1.3.0\CMakeLists.txt,使能PAHO_BUILD_STATIC 和PAHO_BUILD_SAMPLES


## build options

SET(PAHO_WITH_SSL FALSE CACHE BOOL "Flag that defines whether to build ssl-enabled binaries too. ")

SET(PAHO_BUILD_STATIC TRUE CACHE BOOL "Build static library") #firecat modify

SET(PAHO_BUILD_DOCUMENTATION FALSE CACHE BOOL "Create and install the HTML based API documentation (requires Doxygen)")

SET(PAHO_BUILD_SAMPLES TRUE CACHE BOOL "Build sample programs") #firecat modify

SET(PAHO_BUILD_DEB_PACKAGE FALSE CACHE BOOL "Build debian package")

SET(PAHO_ENABLE_TESTING TRUE CACHE BOOL "Build tests and run")

SET(PAHO_ENABLE_CPACK TRUE CACHE BOOL "Enable CPack")


2、paho.mqtt.c-1.3.0\src\samples\CMakeLists.txt

#*******************************************************************************
#  Copyright (c) 2015, 2017 logi.cals GmbH and others
#
#  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:
#     Rainer Poisel - initial version
#     Ian Craggs - update sample names
#*******************************************************************************/
# Note: on OS X you should install XCode and the associated command-line tools
## compilation/linkage settings
INCLUDE_DIRECTORIES(
    .
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_BINARY_DIR}
    )
IF (WIN32)
    ADD_DEFINITIONS(/DCMAKE_BUILD /D_CRT_SECURE_NO_DEPRECATE)
ENDIF()
# sample files c
#ADD_EXECUTABLE(paho_c_pub paho_c_pub.c pubsub_opts.c) #firecat
#ADD_EXECUTABLE(paho_c_sub paho_c_sub.c pubsub_opts.c)
#ADD_EXECUTABLE(paho_cs_pub paho_cs_pub.c pubsub_opts.c)
#ADD_EXECUTABLE(paho_cs_sub paho_cs_sub.c pubsub_opts.c)
#TARGET_LINK_LIBRARIES(paho_c_pub paho-mqtt3as)
#TARGET_LINK_LIBRARIES(paho_c_sub paho-mqtt3as)
#TARGET_LINK_LIBRARIES(paho_cs_pub paho-mqtt3cs)
#TARGET_LINK_LIBRARIES(paho_cs_sub paho-mqtt3cs)
ADD_EXECUTABLE(MQTTAsync_subscribe MQTTAsync_subscribe.c)
ADD_EXECUTABLE(MQTTAsync_publish MQTTAsync_publish.c)
ADD_EXECUTABLE(MQTTClient_subscribe MQTTClient_subscribe.c)
ADD_EXECUTABLE(MQTTClient_publish MQTTClient_publish.c)
ADD_EXECUTABLE(MQTTClient_publish_async MQTTClient_publish_async.c)
TARGET_LINK_LIBRARIES(MQTTAsync_subscribe paho-mqtt3a)
TARGET_LINK_LIBRARIES(MQTTAsync_publish paho-mqtt3a)
TARGET_LINK_LIBRARIES(MQTTClient_subscribe paho-mqtt3c)
TARGET_LINK_LIBRARIES(MQTTClient_publish paho-mqtt3c)
TARGET_LINK_LIBRARIES(MQTTClient_publish_async paho-mqtt3c)
#INSTALL(TARGETS paho_c_sub #firecat
#                paho_c_pub
#                paho_cs_sub
#                paho_cs_pub
#                MQTTAsync_subscribe
#                MQTTAsync_publish
#                MQTTClient_subscribe
#                MQTTClient_publish
#                MQTTClient_publish_async
#    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
#    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

3、使用cmake release编译,生成以下的动态库和静态库文件:


The Paho C client comprises four shared libraries:

libmqttv3a.so - asynchronous

libmqttv3as.so - asynchronous with SSL

libmqttv3c.so - "classic" / synchronous

libmqttv3cs.so - "classic" / synchronous with SSL

Optionally, using the CMake build, you can build static versions of those libraries.

libpaho-mqtt3a.so


libpaho-mqtt3a.so.1


libpaho-mqtt3a.so.1.3.0


libpaho-mqtt3a-static.a


libpaho-mqtt3c.so


libpaho-mqtt3c.so.1


libpaho-mqtt3c.so.1.3.0


libpaho-mqtt3c-static.a


把它们一起拷贝到路径:/usr/local/lib/


此时需要在/etc/ld.so.conf中加入库文件所在的目录:/usr/local/lib/


然后在终端执行命令,使之生效:


[root@localhost etc]# ldconfig


注意,/usr/local/lib/每次有库文件更新,都需要终端重新运行一次ldconfig这条命令。



4、使用案例

/*******************************************************************************
 * 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"
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/resource.h>    /*setrlimit */
#include <fcntl.h> //daemonize
#define ADDRESS     "tcp://192.168.83.128:1883"
#define CLIENTID    "id001"
#define TOPIC       "keepalived"
#define PAYLOAD     "Hello World!"
#define QOS         0
#define USERNAME    "inTerm"
#define PASSWORD    "in2018"
static int run = 1;
volatile MQTTClient_deliveryToken deliveredtoken;
void daemonize(void) { //come from /redis/server.c/daemonize()
    int fd;
    if (fork() != 0) exit(0); /* parent exits */
    setsid(); /* create a new session */
    /* Every output goes to /dev/null. If Redis is daemonized but
     * the 'logfile' is set to 'stdout' in the configuration file
     * it will not log at all. */
    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
        if (fd > STDERR_FILENO) close(fd);
    }
}
void signal_exit_func(int signo)
{
    run = 0;
    printf("exit signo is %d\n", signo);
}
void signal_exit_handler()
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_exit_func;
    sigaction(SIGINT, &sa, NULL);//当按下ctrl+c时,它的效果就是发送SIGINT信号
    sigaction(SIGTERM, &sa, NULL);//kill pid
    sigaction(SIGQUIT, &sa, NULL);//ctrl+\代表退出SIGQUIT
    //SIGSTOP和SIGKILL信号是不可捕获的,所以下面两句话写了等于没有写
    sigaction(SIGKILL, &sa, NULL);//kill -9 pid
    sigaction(SIGSTOP, &sa, NULL);//ctrl+z代表停止
    //#define    SIGTERM        15
    //#define    SIGKILL        9
    //kill和kill -9,两个命令在linux中都有杀死进程的效果,然而两命令的执行过程却大有不同,在程序中如果用错了,可能会造成莫名其妙的现象。
    //执行kill pid命令,系统会发送一个SIGTERM信号给对应的程序。
    //执行kill -9 pid命令,系统给对应程序发送的信号是SIGKILL,即exit。exit信号不会被系统阻塞,所以kill -9能顺利杀掉进程。
}
void delivered(void *context, MQTTClient_deliveryToken dt)
{
    printf("Message with token value %d delivery confirmed\n", dt);
    deliveredtoken = dt;
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");
    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
void connlost(void *context, char *cause)//心跳时间达到,如果没有收到服务器的reponse,客户端的socket会自动关闭
{
    printf("\nConnection lost\n");
    printf("     cause: %s\n", cause);
    exit(EXIT_FAILURE); //firecat
}
//./MQTTClient_firecat "tcp://192.168.83.128:1883" "id001"
int main(int argc, char* argv[])
{
    int background = 1;//firecat add
    if (background)
    {
        daemonize();
    }
    //signal(SIGHUP, SIG_IGN); //开启的话,就捕获不到终端窗口关闭的信号了。即窗口关闭,进程仍然进行。
    signal(SIGPIPE, SIG_IGN);
    char *serverURI;
    char *clientId;
    if (argc == 1)
    {
        serverURI = ADDRESS;
        clientId = CLIENTID;
    }
    else if (argc == 3)
    {
        serverURI = argv[1];
        clientId = argv[2];
    }
    else
    {
        printf("argc error\n");
        exit(EXIT_FAILURE);
    }
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;
    int ch;
    MQTTClient_create(&client, serverURI, clientId,
                      MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 60;
    conn_opts.cleansession = 1;
    conn_opts.MQTTVersion = MQTTVERSION_3_1_1;
    conn_opts.username = USERNAME;
    conn_opts.password = PASSWORD;
    MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
    MQTTClient_subscribe(client, TOPIC, QOS);
    if (background)
    {
        while (run)
        {
            sleep(1);
        }
    }
    else
    {
        do
        {
            ch = getchar();
        } while(ch!='Q' && ch != 'q');
    }
    MQTTClient_unsubscribe(client, TOPIC);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}
相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
物联网 Java Linux
Linux安装与配置Eclipse Paho库:实现MQTT通信
Eclipse Paho是一个开源的MQTT(Message Queuing Telemetry Transport)实现,提供了多种编程语言的客户端库,包括C、C++、Java、Python等。在Linux系统中,通过安装和配置Eclipse Paho库,我们可以方便地实现MQTT通信功能。本文将详细介绍在Linux系统中安装和配置Eclipse Paho库的步骤,以便于开发者在物联网项目中使用MQTT协议进行通信。
1312 0
|
6月前
|
消息中间件 存储 开发工具
消息队列 MQ产品使用合集之C++如何使用Paho MQTT库进行连接、发布和订阅消息
消息队列(MQ)是一种用于异步通信和解耦的应用程序间消息传递的服务,广泛应用于分布式系统中。针对不同的MQ产品,如阿里云的RocketMQ、RabbitMQ等,它们在实现上述场景时可能会有不同的特性和优势,比如RocketMQ强调高吞吐量、低延迟和高可用性,适合大规模分布式系统;而RabbitMQ则以其灵活的路由规则和丰富的协议支持受到青睐。下面是一些常见的消息队列MQ产品的使用场景合集,这些场景涵盖了多种行业和业务需求。
|
物联网 Python
如何通过示例在Python中使用Paho MQTT客户端?
如何通过示例在Python中使用Paho MQTT客户端?
379 2
如何通过示例在Python中使用Paho MQTT客户端?
|
安全 Java Android开发
Eclipse Paho MQTT客户端Java源码分析
Eclipse Paho MQTT客户端Java源码分析
457 0
Eclipse Paho MQTT客户端Java源码分析
|
消息中间件 JavaScript 前端开发
Paho JavaScript Client 基于websocket实现 mqtt客户端
Paho JavaScript Client 基于websocket实现 mqtt客户端
280 0
|
传感器 存储 网络协议
Paho MQTT 客户端接入阿里云物联网平台(4)| 学习笔记
快速学习 Paho MQTT 客户端接入阿里云物联网平台(4)
694 0
Paho MQTT 客户端接入阿里云物联网平台(4)| 学习笔记
|
缓存 监控 Go
记录一次MQTT client反复上下线问题
# 问题描述 有两个使用go语言编写的MQTT agent A 和 B 同时运行,监听和使用不同的topic,其中一个agent A反复上线下线,导致cmdb反复更新和查询设备,进而导致mongo负载高。关掉 A之后负载恢复正常 ![](https://ata2-img.oss-cn-zhangjiakou.aliyuncs.com/neweditor/23fbb14a-70e4-48f7-a4a
862 0
记录一次MQTT client反复上下线问题
|
物联网 Python
开源paho mqtt接入物联网平台【python】
通过开源的paho mqtt库,接入阿里云物联网平台,通过python语言实现。
925 1
|
7月前
|
Java Maven Android开发
在Eclipse里配置Maven插件
Maven是一款比较常用的Java开发拓展包,它相当于一个全自动jar包管理器,会导入用户开发时需要使用的相应jar包。使用Maven开发Java程序,可以极大提升开发者的开发效率。下面我就跟大家介绍一下如何在Eclipse里安装和配置Maven插件。
154 0
|
7月前
|
XML Java Maven
eclipse 、idea 安装activiti插件
eclipse 、idea 安装activiti插件
167 0

推荐镜像

更多