Linux 搭建MQTT服务器(mosquitto)

文章目录

  • 1、安装编译mosquitto
  • 2、错误处理
  • 2、paho.mqtt.c客户端库使用

1、安装编译mosquitto

1、下载包:
wget http://mosquitto.org/files/source/mosquitto-1.4.15.tar.gz
2、解压:
tar -xvf mosquitto-1.4.15.tar.gz
3、进入:
cd mosquitto-1.4.15
sudo make
sudo make install

2、错误处理

错误处理:
Openssl/ssl.h
sudo apt-get install libssl-dev

ares.h
sudo apt-get install libc-ares-dev

uuid/uuid.h
sudo apt-get install uuid-dev

无法创建普通文件’/usr/local/lib/libmosquitto.so.1’: 权限不够
sudo cp libmosquitto.so.1 /usr/local/lib
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
sudo ldconfig


复制配置文件
cd /etc/mosquitto
sudo cp mosquitto.conf.example mosquitto.conf

修改mosquitto.conf

添加当前用户
user root

启动
mosquitto -c /etc/mosquitto/mosquitto.conf 

再新建一个terminal,使用netstat -tunlp|grep 1883 来检查端口是否被监听

使用 mosquitto_sub -t 'firstmqtt' 请求一个订阅

再在本机新建一个terminal,用mosquitto_pub -t 'firstmqtt' -m 'Hello World'发布一个订阅

2、paho.mqtt.c客户端库使用

https://github.com/eclipse/paho.mqtt.c
sudo apt install cmake

安装完毕后一直cd到paho.mqtt.c的文件目录下
cmake .
make
make install

gcc MQTTClient_publish.c -o MQTTClient_publish -lpaho-mqtt3c -lpaho-mqtt3a
./MQTTClient_publish
#include 
#include 
#include 
#include "MQTTClient.h"
#define ADDRESS     "tcp://localhost:1883" //更改此处地址
#define CLIENTID    "ubuntu" //更改此处客户端ID
//#define TOPIC       "test"  //更改发送的话题 firstmqtt
#define TOPIC       "firstmqtt"  //更改发送的话题 firstmqtt
#define PAYLOAD     "Hello World!" //更改信息内容
#define QOS         1
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
	//声明一个MQTTClient
    MQTTClient client;
    char *username= "root"; //添加的用户名
    char *password = "123456"; //添加的密码
    //初始化MQTT Client选项
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
	//#define MQTTClient_message_initializer { {'M', 'Q', 'T', 'M'}, 0, 0, NULL, 0, 0, 0, 0 }
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
	//声明消息token
    MQTTClient_deliveryToken token;
    int rc;
    //使用参数创建一个client,并将其赋值给之前声明的client
    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.username = username; //将用户名写入连接选项中
	conn_opts.password = password;//将密码写入连接选项中
	 //使用MQTTClient_connect将client连接到服务器,使用指定的连接选项。成功则返回MQTTCLIENT_SUCCESS
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
	rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
	printf("Message with delivery token %d delivered\n", token);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

编译:

gcc mqttClient.c -o mqttClient -lpaho-mqtt3c -lpaho-mqtt3a

你可能感兴趣的:(Linux,系统编程,linux,服务器,ubuntu)