Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3786035
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2020-02-11 21:31:25

源码搭建、配置文件

blog.csdn.net/weixin_43025071/article/details/82491560

https://www.cnblogs.com/edan/p/10407202.html

https://blog.csdn.net/qingdujun/article/details/71055759

https://blog.csdn.net/qq_37198420/article/details/97276452

https://blog.csdn.net/qq_37198420/article/details/97759765

https://blog.csdn.net/lanpiao_87/article/details/100025722

https://blog.csdn.net/qq_28643619/article/details/91666536

https://www.cnblogs.com/chen1-kerr/p/7258487.html



https://blog.csdn.net/HoloLens/article/details/80059991

https://www.cnblogs.com/homejim/p/8196763.html

1.安装工具
#yum install gcc gcc-c++
openssl-devel
c-ares-devel
libuuid-devel
make
2.安装eclipse paho C库
#git clone 
#cd paho.mqtt.c
#make
#yum install doxygen
#make html
#make install
3.编译安装mosquitto
#wget 
#tar -xzvf mosquitto-1.4.9.tar.gz
#cd mosquitto-1.4.9
#make
#make install
4.修改mosquitto.conf文件
#vi mosquitto.conf
最后加入
user root
Port 1883

5.运行mosquitto
#mosquitto -c ./mosquitto.conf -v

代码

[root@controller /]# cat a.c
#include
#include
#include
#include

int agent_vetrix_report_vcloud_mqtt(char *mqtt_ip, char *topic, char *message)
{
    int ret = 0;

    //声明一个MQTTClient
    MQTTClient client;
    //初始化MQTT Client选项
    MQTTClient_connectOptions conn = MQTTClient_connectOptions_initializer;
    MQTTClient_deliveryToken dt;
    MQTTClient_message pub_msg = MQTTClient_message_initializer;


    ret = MQTTClient_create(&client, mqtt_ip, "vetrix_pub", MQTTCLIENT_PERSISTENCE_NONE, NULL);
    if(ret != 0)
    {
        return -3;
    }
    //connect config init
    conn.keepAliveInterval = 20;
    conn.cleansession = 1;
    conn.username = "admin";
    conn.password = "public";
    
    ret = MQTTClient_connect(client, &conn);
    if (ret != 0)
    {
        MQTTClient_destroy(&client);
        return -2;
    }

    pub_msg.payload = message;
    pub_msg.payloadlen = (int)strlen(message);
    pub_msg.retained = 0;
    pub_msg.qos = 0;    
    

    ret = MQTTClient_publish(client, topic, pub_msg.payloadlen, pub_msg.payload, pub_msg.qos, pub_msg.retained, NULL);
    if(ret != 0)
    {
        return -5;
    }
    ret = MQTTClient_waitForCompletion(client, dt, 100);
    if(ret != 0){
        return -1;
    }

    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

    return ret;
}

int main(int argc, char* argv[])
{


agent_vetrix_report_vcloud_mqtt(argv[1], argv[2], argv[3]);

return 0;
}
[root@controller /]#


阅读(1617) | 评论(0) | 转发(0) |
0

上一篇:cpu使用率计算

下一篇:dockerfile文件制作

给主人留下些什么吧!~~