前言:
一.linux下EMQX服务器搭建
1.下载deb包
2.安装
3.运行
即可验证自己服务器安装的是否成功,用户名是 admin,初始密码 public
二.客户端工具MQTTX的安装
我用的是windows下的,接着安装运行直接下一步走下去就行了。
三.linux下paho.mqtt.c的开发
1. paho.mqtt.c下载
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
mkdir build
cd build
cmake ..
make
make install
安装完成会在/usr/local/include和/usr/local/lib下看见mqtt的头文件和动态库。
2.mqtt代码开发
#include <MQTTAsync.h> //换成你自己的安装路径,不然回报错
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define SERVERIP "xxx" //换成你自己的服务器
#define Clientid "sza123" //这个随便写
int isConnected=0;
//消息回调函数
int onMessrecv(void* context,char*topicName,int topicLen,MQTTAsync_message*message){
printf("recv name:\ntopic:%s,payload:%s\n",topicName,(char*)message->payload);
MQTTAsync_free(topicName);
MQTTAsync_free(message);
return 1;
}
void onSubscribe(void* context,MQTTAsync_successData* response){
printf("sub success!");
}
//连接mqtt服务器
void onConnect(void *context,MQTTAsync_successData* response)
{
MQTTAsync client =(MQTTAsync)context;
int ret;
MQTTAsync_responseOptions response_opt=MQTTAsync_responseOptions_initializer;
printf("Succeed in connecting to mqtt-server!\n");
response_opt.onSuccess=onSubscribe;
ret=MQTTAsync_subscribe(client,"/pub",1,&response_opt);//订阅/pub主题消息
if(ret!=MQTTASYNC_SUCCESS){
printf("fail to sub!\n");
}
isConnected=1;
}
void disConnect(void *context,MQTTAsync_failureData* response)
{
printf("Failed to connect mqtt-server!\n");
}
void onSend(void* context,MQTTAsync_successData* response){
printf("send message to mqtt server success!\n");
}
int main()
{
MQTTAsync client;
int ret;
MQTTAsync_connectOptions conn_opt = MQTTAsync_connectOptions_initializer;//初始化连接选项
ret=MQTTAsync_create(&client,SERVERIP,Clientid,MQTTCLIENT_PERSISTENCE_NONE,NULL);
if(ret!=MQTTASYNC_SUCCESS)
{
printf("Cannot create mqtt client!\n");
return -1;
}
//初始化接收消息回调
ret=MQTTAsync_setCallbacks(client,NULL,NULL,onMessrecv,NULL);
if(ret!=MQTTASYNC_SUCCESS){
printf("cannnot set call back function!\n");
return -1;
}
conn_opt.onSuccess=onConnect;
conn_opt.onFailure=disConnect;
conn_opt.automaticReconnect=1;
conn_opt.context=client;
conn_opt.cleansession=0;
//conn_opt.username="xxx";//设置用户名密码换成自己的
//conn_opt.password="xxx";
ret=MQTTAsync_connect(client,&conn_opt);
//因为是异步的,当MQTTAsync_connect返回的时候只是代表底层代码对参数进行了检查
//当正确返回时,表示底层代码接收了该connect连接命令
if(ret!=MQTTASYNC_SUCCESS)
{
printf("Cannot start a mqttt server connect!\n");
return -1;
}
while (1){
//1表示成功连接到我们的服务器
if(isConnected==1){
MQTTAsync_message message=MQTTAsync_message_initializer;
MQTTAsync_responseOptions res_option=MQTTAsync_responseOptions_initializer;
char buffer[200];
static int temp=16;
static int humid=58;
sprintf(buffer," temp:%d,humid:%d ",temp++,humid++);
message.payload=buffer;
message.payloadlen=strlen(buffer);
message.qos=1;
res_option.onSuccess=onSend;
printf("read the message is :");
//发布消息
ret=MQTTAsync_sendMessage(client,"/sensor/dht11/0",&message,&res_option);//发送消息
if(ret!=MQTTASYNC_SUCCESS){
printf("参数错误!");
}
sleep(10);
if(temp>30|humid>90){
temp=16;
humid=58;
}
}
}
return 0;
}
然后编译
gcc mqtt.c -o mqtt -I /usr/local/include/ -lpaho-mqtt3a
运行mqtt
文章来源:https://www.toymoban.com/news/detail-740074.html
此时windows上的客户端工具就会接收相应的内容,也可以向linux客户端发送消息。文章来源地址https://www.toymoban.com/news/detail-740074.html
到了这里,关于linux下MQTT服务器(EMQX)搭建及paho.mqtt.c客户端开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!