mosquitto的安裝與使用


  一款實現了消息推送協議 MQTT v3.1 的開源消息代理軟件,提供輕量級的,支持可發布/可訂閱的的消息推送模式,使設備對設備之間的短消息通信變得簡單,比如現在應用廣泛的低功耗傳感器,手機、嵌入式計算機、微型控制器等移動設備。一個典型的應用案例就是 Andy Stanford-ClarkMosquitto(MQTT協議創始人之一)在家中實現的遠程監控和自動化。並在 OggCamp 的演講上,對MQTT協議進行詳細闡述。

一、准備材料

安裝包:openssl、JSON-C、mosquitto
安裝環境:ubuntu20.04

二、MQTT協議

MQTT每個版本之間存在一定的差異,需要了解相關信息的請參考相關協議說明
MQTT中文網:http://mqtt.p2hp.com/

三、下載源碼

  1. openssl下載
    openssl官網:https://www.openssl.org/
    openssl源碼下載

  2. mosquitto下載
    mosquitto官網:http://mosquitto.org/
    mosquitto源碼下載


    mosquitto的所以源碼版本都在source路徑下
  3. json-c下載
    通過git獲取json-c源碼
git clone https://github.com/DaveGamble/cJSON

四、mosquitto安裝

  安裝方式有兩種,分為指令安裝和源碼安裝,可以根據自己的需要選擇相應的安裝方式

  1. 源碼安裝
    了解源碼的安裝主要是方便后面學習移植,先將openssl和mosquitto拷貝到linux環境中進行解壓,先安裝openssl再安裝mosquitto。
    安裝openssl
tar vxf openssl-1.1.1l.tar.gz
cd openssl-1.1.1l
ls

./config
make
make test
sudo make install

安裝完成后會在/usr/local路徑下生成ssl文件,並在include和share文件中生產相關文件。
安裝json-c

git clone https://github.com/DaveGamble/cJSON
cd cJSON
make all
sudo make install

安裝mosquitto

tar vxf mosquitto-2.0.11.tar.gz
cd mosquitto-2.0.11
ls


直接make即可

make
sudo make install
  1. 指令安裝
sudo apt-get install mosquitto

sudo apt-get install mosquitto-clients

五、mosquitto使用

  測試mosquitto至少需要打開3個命令窗口。

  1. 啟動服務端(代理服務器)
mosquitto -c /etc/mosquitto/mosquitto.conf.example


2. 啟動訂閱端(訂閱者)

mosquitto_sub -t temp


訂閱成功后代理服務器將會輸出相應的信息

3. 發布消息(發布者)

mosquitto_pub -t temp -m hello_world


發布完成后訂閱者將會收到相信的信息

問題處理

  1. mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

    解決辦法:
sudo ldconfig
  1. Error: Address already in use

    解決辦法:
sudo lsof -i:1端口號
kill -9 進程號

參考文獻

mosquitto百度百科:https://baike.baidu.com/item/mosquitto/3172080?fr=aladdin
openssl百度百科:https://baike.baidu.com/item/openssl/5454803?fr=aladdin
Linux下cJSON安裝及使用:https://zhuanlan.zhihu.com/p/55095477
MQTT服務器搭建--Mosquitto:https://hao123.blog.csdn.net/article/details/112789257
MQTT--mosquitto使用詳解:https://blog.csdn.net/qq_28877125/article/details/78330809


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM