源码搭建、配置文件
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) |