# pahomqtt **Repository Path**: soonliao/pahomqtt ## Basic Information - **Project Name**: pahomqtt - **Description**: 支持鸿蒙系统的MQTT库,在paho mqtt基础上实现,在openharmony3.0上编译测试通过 - **Primary Language**: C - **License**: EPL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 28 - **Created**: 2021-10-10 - **Last Updated**: 2021-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README - ### 基于Openharmony3.0 Hi3861 的 MQTT库 该库是在[连志安老师 / harmony_mqtt](https://gitee.com/qidiyun/harmony_mqtt)的基础上,进行修改的,在Openharmony3.0 代码中编译测试通过。 原始的 paho mqtt源码:https://github.com/eclipse/paho.mqtt.embedded-c 鸿蒙系统相关移植文件:MQTTClient-C\src\liteOS\MQTTLiteOS.c - ### 使用方法 - 1.将本项目git clone 到Openharmony3.0 的third_party目录下; - 2.修改device\hisilicon\hispark_pegasus\sdk_liteos\BUILD.gn sdk的deps中加入"//third_party/pahomqtt:pahomqtt_static",; ``` lite_component("sdk") { features = [] deps = [ "//build/lite/config/component/cJSON:cjson_static", "//device/hisilicon/hispark_pegasus/hi3861_adapter/kal", "//third_party/pahomqtt:pahomqtt_static", ] } ``` - 3.需要用到MQTT的applications 的BUILD.gn 中include_dirs加入以下如pahomqtt相关的三行: ``` static_library("car") { sources = [ ...... ] include_dirs = [ ....... "//third_party/pahomqtt:pahomqtt_static", "//third_party/pahomqtt/MQTTPacket/src", "//third_party/pahomqtt/MQTTClient-C/src", ] } ``` - 4.参考代码 ``` #include "MQTTClient.h" #define MQTT_BROKER "192.168.123.230" #define MQTT_PORT 1883 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", QOS2, NULL, NULL, (char*)MQTT_BROKER, MQTT_PORT, 1 }; void messageArrived(MessageData* md) { MQTTMessage* message = md->message; if (opts.showtopics) printf("%.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data); if (opts.nodelimiter) printf("%.*s", (int)message->payloadlen, (char*)message->payload); else printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter); //fflush(stdout); } unsigned char buf[100]; unsigned char readbuf[100]; int mqtt_test(void) { int rc = 0; MQTTMessage pubmsg; char* topic = "test"; if (strchr(topic, '#') || strchr(topic, '+')) opts.showtopics = 1; if (opts.showtopics) printf("topic is %s\n", topic); Network n; MQTTClient c; NetworkInit(&n); NetworkConnect(&n, opts.host, opts.port); MQTTClientInit(&c, &n, 1000, buf, 100, readbuf, 100); MQTTStartTask(&c); 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 = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived); printf("Subscribed %d\n", rc); memset(&pubmsg, '\0', sizeof(pubmsg)); pubmsg.payload = (void*)"hello harmonyOS !"; pubmsg.payloadlen = strlen((char*)pubmsg.payload); pubmsg.qos = QOS0; pubmsg.retained = 0; pubmsg.dup = 0; while (1) { MQTTPublish(&c, "pubtest", &pubmsg); sleep(1); } printf("Stopping\n"); MQTTDisconnect(&c); NetworkDisconnect(&n); return 0; } ``` 代码移植说明请移步51CTO 连志安老师[主页](https://harmonyos.51cto.com/user/posts/14630655?tabIndex=0) [在鸿蒙系统上使用MQTT编程](https://harmonyos.51cto.com/posts/1780) [MQTT 协议开发入门](https://harmonyos.51cto.com/posts/1381) [如何在鸿蒙系统中移植 Paho-MQTT 实现MQTT协议 ](https://harmonyos.51cto.com/posts/1384) # Eclipse Paho MQTT C/C++ client for Embedded platforms This repository contains the source code for the [Eclipse Paho](http://eclipse.org/paho) MQTT C/C++ client library for Embedded platorms. It is dual licensed under the EPL and EDL (see about.html and notice.html for more details). You can choose which of these licenses you want to use the code under. The EDL allows you to embed the code into your application, and distribute your application in binary or source form without contributing any of your code, or any changes you make back to Paho. See the EDL for the exact conditions. There are three sub-projects: 1. MQTTPacket - simple de/serialization of MQTT packets, plus helper functions 2. MQTTClient - high(er) level C++ client, plus 3. MQTTClient-C - high(er) level C client (pretty much a clone of the C++ client) The *MQTTPacket* directory contains the lowest level C library with the smallest requirements. This supplies simple serialization and deserialization routines. They serve as a base for the higher level libraries, but can also be used on their own It is mainly up to you to write and read to and from the network. The *MQTTClient* directory contains the next level C++ library. This networking code is contained in separate classes so that you can plugin the network of your choice. Currently there are implementations for Linux, Arduino and mbed. ARM mbed was the first platform for which this was written, where the conventional language choice is C++, which explains the language choice. I have written a starter [Porting Guide](http://modelbasedtesting.co.uk/2014/08/25/porting-a-paho-embedded-c-client/). The *MQTTClient-C* directory contains a C equivalent of MQTTClient, for those platforms where C++ is not supported or the convention. As far as possible it is a direct translation from *MQTTClient*. ## Build requirements / compilation CMake builds for the various packages have been introduced, along with Travis-CI configuration for automated build & testing. The basic method of building on Linux is: ``` mkdir build.paho cd build.paho cmake .. make ``` The travis-build.sh file has the full build and test sequence for Linux. ## Usage and API See the samples directories for examples of intended use. Doxygen config files for each package are available in the doc directory. ## Runtime tracing The *MQTTClient* API has debug tracing for MQTT packets sent and received - turn this on by setting the MQTT_DEBUG preprocessor definition. ## Reporting bugs This project uses GitHub Issues here: [github.com/eclipse/paho.mqtt.embedded-c/issues](https://github.com/eclipse/paho.mqtt.embedded-c/issues) to track ongoing development and issues. ## More information Discussion of the Paho clients takes place on the [Eclipse Mattermost Paho channel](https://mattermost.eclipse.org/eclipse/channels/paho) and the [Eclipse paho-dev mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev). General questions about the MQTT protocol are discussed in the [MQTT Google Group](https://groups.google.com/forum/?hl=en-US&fromgroups#!forum/mqtt). More information is available via the [MQTT community](http://mqtt.org).