qt調用mqtt以及emq服務器搭建

1、qt使用mqtt客戶端

qmqtt相關庫和頭文件需要自己編譯源碼(源碼下載地址https://github.com/emqx/qmqtt),編譯後lib文件夾下的*.a文件即爲庫文件。

將源碼下的所有頭文件複製到一個文件夾,生成的庫文件放在一個文件夾。

新項目直接包含頭文件和庫文件就可以使用qmqtt了。

2、emq服務器搭建(windows10)

命令行切換到emq目錄,執行命令 emqttd start打開emq服務

D:\ProgramFiles\emqttd-windows10-v2.3.4\emqttd\bin>emqttd start

D:\ProgramFiles\emqttd-windows10-v2.3.4\emqttd>

用瀏覽器打開鏈接http://127.0.0.1:18083/#/

默認用戶名:admin

默認密碼:public

進入可視化界面

停止emq服務

D:\ProgramFiles\emqttd-windows10-v2.3.4\emqttd\bin>emqttd stop

3、啓用用戶名密碼

(1)修改emq.conf文件,我的emq.conf文件路徑爲D:\ProgramFiles\emqttd-windows10-v2.3.4\emqttd\etc\emq.conf。裏面有一個配置選項mqtt.allow_anonymous,其值爲true時,則不作用戶名密碼驗證,爲false時,則進行用戶名密碼驗證。我們要啓用用戶名密碼驗證,將默認的true改成false,如下圖所示:

(2)設置默認用戶名、密碼

修改文件emq_auth_username.conf,我的路徑爲D:\ProgramFiles\emqttd-windows10-v2.3.4\emqttd\etc\plugins\emq_auth_username.conf,編輯用戶名密碼

##--------------------------------------------------------------------
## Username Authentication Plugin
##--------------------------------------------------------------------

## Examples:
##auth.user.1.username = admin
##auth.user.1.password = public
##auth.user.2.username = [email protected]
##auth.user.2.password = public
##auth.user.3.username = name~!@#$%^&*()_+
##auth.user.3.password = pwsswd~!@#$%^&*()_+

(3)啓用插件

3、linux服務器搭建(CentOS7)

(1)安裝unzip

yum -y install unzip

(2)下載emqx

wget https://www.emqx.io/downloads/broker/v4.0.3/emqx-centos7-v4.0.3.zip

(3)解壓並移動到/usr/local目錄下

unzip emqx-centos7-v4.0.3.zip && mv emqx/usr/local

(4)編輯系統環境配置

vi /etc/profile

(5)在末尾添加如下內容

# emqx
export EMQPATH=/usr/local/emqx
export PATH=$EMQPATH/bin:$PATH

(6)更新環境變量

source /etc/profile

(7)執行命令:emqx console檢查安裝和環境變量配置是否正常

[root@VM_16_7_centos ~]# emqx console
Exec: /usr/local/emqx/erts-10.5/bin/erlexec -boot /usr/local/emqx/releases/v4.0.3/emqx -mode embedded -boot_var ERTS_LIB_DIR /usr/local/emqx/erts-10.5/../lib -mnesia dir "/usr/local/emqx/data/mnesia/[email protected]" -config /usr/local/emqx/data/configs/app.2020.04.09.14.10.22.config -args_file /usr/local/emqx/data/configs/vm.2020.04.09.14.10.22.args -vm_args /usr/local/emqx/data/configs/vm.2020.04.09.14.10.22.args -start_epmd false -epmd_module ekka_epmd -proto_dist ekka -- console
Root: /usr/local/emqx
/usr/local/emqx
Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:1:1] [ds:1:1:8] [async-threads:4] [hipe]

Starting emqx on node [email protected]
Start http:management listener on 8081 successfully.
Start http:dashboard listener on 18083 successfully.
Start mqtt:tcp listener on 127.0.0.1:11883 successfully.
Start mqtt:tcp listener on 0.0.0.0:1883 successfully.
Start mqtt:ws listener on 0.0.0.0:8083 successfully.
Start mqtt:ssl listener on 0.0.0.0:8883 successfully.
Start mqtt:wss listener on 0.0.0.0:8084 successfully.
EMQ X Broker 4.0.3 is running now!
Eshell V10.5  (abort with ^G)
([email protected])1> 

(8)依次執行下面命令配置防火牆,開啓端口

firewall-cmd --permanent --add-port=1883/tcp
firewall-cmd --permanent --add-port=18083/tcp
firewall-cmd --reload

(9)啓動服務

emqx start

(10)查看emq當前狀態

emqx_ctl status

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章