Linux C 语言 mosquitto 方式 MQTT 发布消息

1 说明

采用 mosquitto 库,实现对主题发布消息。

其中服务器有做限制,需要对应的 cilent id ,cafile 、certfile 、keyfile 等配置

2 开发环境

采用ubuntu 直接编译调试

安装mosquitto 库

c 复制代码
sudo apt install libmosquitto-dev
sudo apt-get install mosquitto

3 参考代码

每隔2秒发布一次消息, 代码存为文件mqtt_publisher.c

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mosquitto.h>

#define MQTT_HOST "akhrsh85xmt1q-ats.iot.us-east-1.amazonaws.com"
#define MQTT_PORT 8883
#define MQTT_TOPIC "aws/rules/funcResponse_v1_1/WoSweeperOrigin/1A2023040002/funcResponse"
#define MQTT_CERT_PATH "/home/sky/develop/test/mosqito/cert.pem"
#define MQTT_KEY_PATH "/home/sky/develop/test/mosqito/key.pem"
#define MQTT_CA_PATH "/home/sky/develop/test/mosqito/ca.pem"
#define MQTT_CLIENT_ID "ID_666"

int main(int argc, char *argv[])
{
    struct mosquitto *mosq = NULL;
    int rc = 0;
    int i = 0;

    // 初始化 mosquitto 库
    mosquitto_lib_init();

    // 创建 mosquitto 客户端实例
    mosq = mosquitto_new(MQTT_CLIENT_ID, true, NULL);
    if (!mosq) {
        printf("Failed to create mosquitto instance.\n");
        return 1;
    }

    // 设置 TLS 证书
    rc = mosquitto_tls_set(mosq, MQTT_CA_PATH, NULL, MQTT_CERT_PATH, MQTT_KEY_PATH, NULL);
    if (rc != MOSQ_ERR_SUCCESS) {
        printf("Failed to set TLS certificate.\n");
        return 1;
    }

    // 连接 MQTT 服务器
    rc = mosquitto_connect(mosq, MQTT_HOST, MQTT_PORT, 60);
    if (rc != MOSQ_ERR_SUCCESS) {
        printf("Failed to connect to MQTT server.\n");
        return 1;
    }

    for(i = 0; i < 10; i++){
        char message[50];
        sprintf(message, "Message %d", i+1);
		printf("%s %s %d [%d/10], send message:%s\n", __FILE__, __func__, __LINE__, i, message);
        rc = mosquitto_publish(mosq, NULL, MQTT_TOPIC, strlen(message), message, 0, false);
        if(rc != MOSQ_ERR_SUCCESS){
            fprintf(stderr, "Error: Could not publish message. %s\n", mosquitto_strerror(rc));
        }
		
		rc = mosquitto_loop(mosq, -1, 1);
        if (rc != MOSQ_ERR_SUCCESS) {
            printf("Failed to process messages.\n");
            break;
        }
		
        sleep(2);
    }

    mosquitto_disconnect(mosq);
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();

    return 0;
}

对应的makefile

c 复制代码
all: mqtt_publisher

mqtt_publisher: mqtt_publisher.o
	gcc -o mqtt_publisher mqtt_publisher.o -lmosquitto

mqtt_publisher.o: mqtt_publisher.c
	gcc -c mqtt_publisher.c

clean:
	rm -f mqtt_publisher mqtt_publisher.o

4 调试

c 复制代码
sky@ubuntu:~/develop/test/mosqito$ make
gcc -c mqtt_publisher.c
gcc -o mqtt_publisher mqtt_publisher.o -lmosquitto

运行

c 复制代码
sky@ubuntu:~/develop/test/mosqito$ ./mqtt_publisher 
mqtt_publisher.c main 49 [0/10], send message:Message 1
mqtt_publisher.c main 49 [1/10], send message:Message 2
mqtt_publisher.c main 49 [2/10], send message:Message 3
mqtt_publisher.c main 49 [3/10], send message:Message 4
mqtt_publisher.c main 49 [4/10], send message:Message 5
mqtt_publisher.c main 49 [5/10], send message:Message 6
mqtt_publisher.c main 49 [6/10], send message:Message 7

网页端查看

相关推荐
o(╥﹏╥)19 分钟前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
娶不到胡一菲的汪大东23 分钟前
Ubuntu概述
linux·运维·ubuntu
Yuan_o_35 分钟前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
FeboReigns1 小时前
C++简明教程(文章要求学过一点C语言)(1)
c语言·开发语言·c++
那就举个栗子!1 小时前
Ubuntu 20.04下Kinect2驱动环境配置与测试【稳定无坑版】
linux·ubuntu
FeboReigns1 小时前
C++简明教程(文章要求学过一点C语言)(2)
c语言·开发语言·c++
灯火不休➴1 小时前
[Xshell] Xshell的下载安装使用、连接linux、 上传文件到linux系统-详解(附下载链接)
linux·运维·服务器
Lukea111 小时前
【新教程】Ubuntu server 24.04配置无线网WiFi
linux·ubuntu·教程
小峰编程1 小时前
独一无二,万字详谈——Linux之文件管理
linux·运维·服务器·云原生·云计算·ai原生
卜及中1 小时前
【Linux】资源隔离机制 — 命名空间(Namespace)详解
linux·服务器·php