From f534aea3812cf01672e3145a2492207866752fef Mon Sep 17 00:00:00 2001 From: chang <15294300261@163.com> Date: Tue, 24 Oct 2023 17:19:13 +0800 Subject: [PATCH] how to use mqtt --- how_to_use_mqtt/pub1.c | 35 ++++++++++++++++ how_to_use_mqtt/pub1_text.c | 7 ++++ how_to_use_mqtt/sub1.c | 81 +++++++++++++++++++++++++++++++++++++ how_to_use_mqtt/sub1_text.c | 25 ++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 how_to_use_mqtt/pub1.c create mode 100644 how_to_use_mqtt/pub1_text.c create mode 100755 how_to_use_mqtt/sub1.c create mode 100644 how_to_use_mqtt/sub1_text.c diff --git a/how_to_use_mqtt/pub1.c b/how_to_use_mqtt/pub1.c new file mode 100644 index 0000000..60dbcb0 --- /dev/null +++ b/how_to_use_mqtt/pub1.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +int isConnected = 0; +void connect_callback(struct mosquitto* mosq, void* obj, int rc) { + if (rc == 0) { + printf("---连接服务器成功---\n"); + isConnected = 1; + } +} +int main(int argc, char const* argv[]) { + if (argc < 3) { + printf("format: ./pub1 msgid topic msg"); + return -1; + } + mosquitto_lib_init(); + struct mosquitto* client = mosquitto_new(NULL, true, NULL); + mosquitto_connect_callback_set(client, connect_callback); + mosquitto_connect(client, "10.12.156.19", 1883, 60); + mosquitto_loop_start(client); + while (isConnected == 0) + int msg_id = atoi(argv[1]); + char* topic = (char*)argv[2]; + char* msg = (char*)argv[3]; + int flag = + mosquitto_publish(client, &msg_id, topic, strlen(msg), msg, 1, true); + if (flag == MOSQ_ERR_SUCCESS) { + printf("---消息发布成功: %s(%d):%s---\n", topic, msg_id, msg); + mosquitto_destroy(client); + mosquitto_lib_cleanup(); + } + return 0; +} \ No newline at end of file diff --git a/how_to_use_mqtt/pub1_text.c b/how_to_use_mqtt/pub1_text.c new file mode 100644 index 0000000..e48b8d1 --- /dev/null +++ b/how_to_use_mqtt/pub1_text.c @@ -0,0 +1,7 @@ +1.初始化mosquitto的库环境 mosquitto_lib_init(); +2.创建mosquitto的服务端 创建服务端句柄 struct mosquitto* client = + mosquitto_new(NULL, true, NULL); +3.回调函数 mosquitto_connect_callback_set(client, connect_callback); +mosquitto_connect(client, "10.12.156.19", 1883, 60); +mosquitto_loop_start(client); +4.发布消息 mosquitto_publish(client, &msg_id, topic, strlen(msg), msg, 1, true); \ No newline at end of file diff --git a/how_to_use_mqtt/sub1.c b/how_to_use_mqtt/sub1.c new file mode 100755 index 0000000..aa7ac6d --- /dev/null +++ b/how_to_use_mqtt/sub1.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include + +struct mosquitto* client; +int isRunning = 1; +pthread_t main_th_id; +void connect_callback(struct mosquitto* mosq, void* args, int rc) { + if (rc == 0) { + printf("连接MQTT服务成功!\n"); + } else { + printf("连接MQTT服务失败!\n"); + } +} +void on_subscribe(struct mosquitto* mosq, + void* obj, + int mid, + int qos_count, + const int* granted_qos) { + printf("-订阅的消息ID为--%d 成功, Qos的granted: %d--\n", mid, + *granted_qos); +} +void on_message(struct mosquitto* mosq, + void* obj, + const struct mosquitto_message* message) { + printf("收到%s(%d)的消息: %s\n", message->topic, message->mid, + (char*)message->payload); + if (strncmp((char*)message->payload, "exit", 4) == 0) { + isRunning = 0; + mosquitto_loop_stop(client, true); + } +} +int main(int argc, char const* argv[]) { + main_th_id = pthread_self(); + // 1. 初始化mosquitto的库环境 + mosquitto_lib_init(); // 初始化(固定格式) + // 使用mosquitto库函数前,要先初始化,使用之后要清除。 + // 2. 创建mosquitto的客户端 + unsigned char userdata[128] = "1"; + client = mosquitto_new("166", true, userdata); // 创建客户端句柄 + // struct mosquitto* mosquitto_new( + // const char *id,//用户自定义标识ID + // bool clean_session, //断开后是否保留订阅信息true/false + // void *userdata //回调参数 + // ); + // d可以为NULL,clean_session的标识必须是true, userdata也可以是NULL + if (client == NULL) { + printf("创建mqtt客户端失败!\n"); + perror("mosquitto_new\n"); + return -1; + } + mosquitto_connect_callback_set(client, connect_callback); + mosquitto_subscribe_callback_set(client, on_subscribe); + mosquitto_message_callback_set(client, on_message); + // 3. 连接mqtt broker + int flag = mosquitto_connect(client, "10.12.156.19", 1883, 60); + if (flag == MOSQ_ERR_SUCCESS) { + printf("-----连接MQTT 服务器成功!-----\n"); + } + int msgId = 1; + // 开始订阅信息 + flag = mosquitto_subscribe(client, &msgId, "dht11", 0); + if (flag == MOSQ_ERR_SUCCESS) { + printf("订阅消息ID: %d 成功, 等待消息!\n", msgId); + } + while (isRunning) { + // 处理网络事件 + mosquitto_loop_start(client); // 接收网络数据 + usleep(500); + } + + // 关闭mosquitto的客户端 + mosquitto_destroy(client); + // 最后清理库环境 + mosquitto_lib_cleanup(); + return 0; +} diff --git a/how_to_use_mqtt/sub1_text.c b/how_to_use_mqtt/sub1_text.c new file mode 100644 index 0000000..86f7cdf --- /dev/null +++ b/how_to_use_mqtt/sub1_text.c @@ -0,0 +1,25 @@ +1. 初始化mosquitto的库环境 + // int mosquitto_lib_init(void); + 2. 创建mosquitto的客户端 创建客户端句柄 + // client = mosquitto_new("166", true, userdata); + 3. 在发布或订阅之前客户端要先链接服务器,不管链接成功与否都会触发链接回调 + // mosquitto_connect_callback_set( + // struct mosquitto* mosq, // 客户端 + // void (*on_connect)( // 回调函数 + // struct mosquitto* mosq, // 客户端数据 + // void* obj, // 创建客户端时的回调参数 + // int rc // 0表示连接成功,其它表示失败 + // )) + // mosquitto_connect_callback_set(client, connect_callback); 连接确认回调 + // mosquitto_subscribe_callback_set(client, on_subscribe);设置订阅回调函数 + // mosquitto_message_callback_set(client,on_message);置订阅消息到来的回调函数【重要】 + 4. 客户端连接服务器 + // int flag = mosquitto_connect(client, "10.12.156.19", 1883, 60); + 5. 订阅消息 + // flag = mosquitto_subscribe(client, &msgId, "dht11", 0); + 6.网络事件循环处理函数 + // mosquitto_loop_start(client); // 接收网络数据 + 7.关闭mosquitto的客户端 + // mosquitto_destroy(client); + 8. 最后清理库环境 + // mosquitto_lib_cleanup(); \ No newline at end of file