paho.mqtt.c
paho.mqtt.c
https://github.com/eclipse/paho.mqtt.c
https://docs.emqx.com/zh/cloud/latest/connect_to_deployments/c_sdk.html#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81
1、
sudo apt-get update
sudo apt-get -y install build-essential git cmake
2、
git clone https://github.com/eclipse/paho.mqtt.c.git
cd /opt/openAI/software/paho.mqtt.c
3、修改Makefile 指向openssl-3.3.1 lib,include文件位置
4、
cmake .
make
sudo make install
stevenroc@stevenroc:/opt/openAI/software/paho.mqtt.c $ sudo make install
[ 38%] Built target common_obj
[ 43%] Built target paho-mqtt3a
[ 46%] Built target paho-mqtt3c
[ 50%] Built target MQTTVersion
[ 54%] Built target thread
[ 58%] Built target test1
[ 61%] Built target test15
[ 64%] Built target test2
[ 67%] Built target test4
[ 70%] Built target test45
[ 74%] Built target test6
[ 77%] Built target test8
[ 80%] Built target test9
[ 83%] Built target test95
[ 87%] Built target test10
[ 90%] Built target test11
[ 93%] Built target test_issue373
[ 96%] Built target test_sync_session_present
[100%] Built target test_connect_destroy
Install the project…
— Install configuration: “”
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTAsync_publish.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTAsync_publish_time.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTAsync_subscribe.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTClient_publish.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTClient_publish_async.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/MQTTClient_subscribe.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/paho_c_pub.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/paho_c_sub.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/paho_cs_pub.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/paho_cs_sub.c
— Installing: /usr/local/share/doc/Eclipse Paho C/samples/pubsub_opts.c
— Installing: /usr/local/share/doc/Eclipse Paho C/CONTRIBUTING.md
— Installing: /usr/local/share/doc/Eclipse Paho C/epl-v20
— Installing: /usr/local/share/doc/Eclipse Paho C/edl-v10
— Installing: /usr/local/share/doc/Eclipse Paho C/README.md
— Installing: /usr/local/share/doc/Eclipse Paho C/notice.html
— Installing: /usr/local/lib/libpaho-mqtt3c.so.1.3.13
— Installing: /usr/local/lib/libpaho-mqtt3c.so.1
— Installing: /usr/local/lib/libpaho-mqtt3c.so
— Installing: /usr/local/lib/libpaho-mqtt3a.so.1.3.13
— Installing: /usr/local/lib/libpaho-mqtt3a.so.1
— Installing: /usr/local/lib/libpaho-mqtt3a.so
— Installing: /usr/local/bin/MQTTVersion
— Set runtime path of “/usr/local/bin/MQTTVersion” to “”
— Installing: /usr/local/include/MQTTAsync.h
— Installing: /usr/local/include/MQTTClient.h
— Installing: /usr/local/include/MQTTClientPersistence.h
— Installing: /usr/local/include/MQTTProperties.h
— Installing: /usr/local/include/MQTTReasonCodes.h
— Installing: /usr/local/include/MQTTSubscribeOpts.h
— Installing: /usr/local/include/MQTTExportDeclarations.h
— Installing: /usr/local/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig.cmake
— Installing: /usr/local/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig-noconfig.cmake
— Installing: /usr/local/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfigVersion.cmake
四、编译main.c 订阅pub,发布sub
#include “stdlib.h”
#include “string.h”
#include “unistd.h”
#include “MQTTClient.h”
// Enable or disable SSL/TLS connection (1 for SSL/TLS, 0 for TCP)
#define USE_SSL 1
#if USE_SSL
#define ADDRESS “ssl://broker.emqx.io:8883”
#else
#define ADDRESS “tcp://192.168.110.15:1883”
#endif
#define ADDRESS “tcp://192.168.110.15:1883”
#define USERNAME “steven_roc” //emqx
#define PASSWORD “xiaoniu098=-” //public
#define CLIENTID “c-client”
#define QOS 0
#define TOPIC “sub/1234/CORX00001122”
#define TIMEOUT 10000L
MQTTClient_SSLOptions configureSSLOptions() {
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
ssl_opts.enableServerCertAuth = 1;
// ssl_opts.trustStore = CA_CERTIFICATE_FILE;
return ssl_opts;
}
void publish(MQTTClient client, char *topic, char *payload) {
MQTTClient_message message = MQTTClient_message_initializer;
message.payload = payload;
message.payloadlen = strlen(payload);
message.qos = QOS;
message.retained = 0;
MQTTClient_deliveryToken token;
MQTTClient_publishMessage(client, topic, &message, &token);
MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf(“Send `%s` to topic `%s` \n”, payload, TOPIC);
}
int on_message(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
char *payload = message->payload;
printf(“Received `%s` from `%s` topic \n”, payload, topicName);
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
int main(int argc, char *argv[]) {
int rc;
MQTTClient client;
MQTTClient_create(&client, ADDRESS, CLIENTID, 0, NULL);
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
#if USE_SSL
MQTTClient_SSLOptions ssl_opts = configureSSLOptions();
conn_opts.ssl = &ssl_opts;
#endif
MQTTClient_setCallbacks(client, NULL, NULL, on_message, NULL);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
printf(“Failed to connect, return code %d\n”, rc);
exit(-1);
} else {
printf(“Connected to MQTT Broker!\n”);
}
// subscribe topic
MQTTClient_subscribe(client, TOPIC, QOS);
char payload[16];
for (int i = 0; i < 10; i += 1) {
// publish message to broker
snprintf(payload, 16, “message-%d”, i);
publish(client, TOPIC, payload);
sleep(1);
}
MQTTClient_unsubscribe(client, TOPIC);
MQTTClient_disconnect(client, TIMEOUT);
MQTTClient_destroy(&client);
return rc;
}
一、编译,执行
sudo nano CMakeLists.txt
编写 CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
find_package(eclipse-paho-mqtt-c 1.3.9 REQUIRED)
project(mqtt_c C)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
set(CMAKE_C_STANDARD 99)
add_executable(mqtt_c main.c)
target_link_libraries(mqtt_c paho-mqtt3c)
二、
cmake .
三、
sudo make
./mqtt_c
四、
sudo nano main.c
五、补充
1、编译错误
stevenroc@stevenroc:/opt/openAI/project/003.paho.mqtt $ make
/usr/bin/cmake: /opt/openAI/Lib64U/lib/libcurl.so.4: no version information available (required by /usr/bin/cmake)
/usr/bin/cmake: /lib/aarch64-linux-gnu/libssl.so.3: version `OPENSSL_3.2.0′ not found (required by /opt/openAI/Lib64U/lib/libcurl.so.4)
make: *** [Makefile:179: cmake_check_build_system] Error 1

解决如下

自适应IP