how to use mqtt

This commit is contained in:
chang 2023-10-24 17:19:13 +08:00
parent a925114074
commit f534aea381
4 changed files with 148 additions and 0 deletions

35
how_to_use_mqtt/pub1.c Normal file
View File

@ -0,0 +1,35 @@
#include <mosquitto.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -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);

81
how_to_use_mqtt/sub1.c Executable file
View File

@ -0,0 +1,81 @@
#include <mosquitto.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
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可以为NULLclean_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;
}

View File

@ -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();