本文背景:主要是安裝mosquitto服務(wù)端,利用第三方平臺(如樂維監(jiān)控、zabbix等)使用mqtt完成對srne碩日太陽能動環(huán)對接監(jiān)控。
1?安裝依賴
yum install gcc-c++ cmake openssl-devel libuuid-devel c-ares-devel uuid-devel libwebsockets-devel.x86_64 libwebsockets.x86_64 -y
2?下載mosquitto服務(wù)
服務(wù)器建立mqtt文件夾
https://mosquitto.org/files/source/mosquitto-2.0.20.tar.gz
3?安裝服務(wù)
tar -xvf mosquitto-2.0.20.tar.gz
cd mosquitto-2.0.20/
make && make install
報錯:重新執(zhí)行make && make install,繼續(xù)報錯config.h:86:12: fatal error: cjson/cJSON.h: No such file or directory
解決方案:那就網(wǎng)上找cJSON包安裝下, 在mqtt目錄下執(zhí)行安裝
先確認(rèn)下是否已經(jīng)安裝git,沒有的話。yum install git
git clone https://github.com/DaveGamble/cJSON.git
創(chuàng)建一個構(gòu)建目錄并進(jìn)入:
mkdir build && cd build/
使用CMake 生成構(gòu)建文件
cmake ..
最后,使用sudo make install?命令安裝 cJSON 庫到系統(tǒng)目錄中。這一步需要 root 權(quán)限:
sudo make install
但是可能會報錯,CMake 版本不足,版本太大,需要3.0以上的
需要先升級cmake
yum remove -y cmake
1.?創(chuàng)建新目錄并進(jìn)入:
mkdir /opt/cmake
cd /opt/cmake/
2.?下載最新版本的CMake:您可以從CMake 官網(wǎng)下載最新版本的 CMake。例如,下載 3.16.6 版本:
wget https://cmake.org/files/v3.16/cmake-3.16.6.tar.gz
3.?解壓下載的文件:
tar -zxvf cmake-3.16.6.tar.gz
[if !supportLists]4.?[endif]安裝編譯工具:
yum install -y gcc gcc-c++
5.?編譯并安裝CMake:
cd cmake-3.16.6
./configure --prefix=/usr/local/cmake
make && make install
6.?創(chuàng)建鏈接:
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
7.?驗(yàn)證新版本的CMake:
cmake --version
重新編譯cJSON 庫
更新CMake 版本后,您可以重新進(jìn)入 cJSON 的目錄并嘗試編譯:
bash復(fù)制
cd /path/to/cJSON/build
cmake ..
make
sudo make install
安裝完畢后,切換回
cd ../mosquitto-2.0.18/
重新執(zhí)行make && make install
4?啟動服務(wù)
cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
mosquitto -c /etc/mosquitto/mosquitto.conf -d -v ??#可能報錯沒有mosquitto這個命令,需要軟連接一下就好,或者直接使用絕對路徑
ln -s /usr/local/sbin/mosquitto /usr/bin/mosquitto
這個命令會在/usr/bin/ 目錄下創(chuàng)建一個名為 mosquitto 的符號鏈接,它指向 /usr/local/sbin/mosquitto。下面用到的mosquitto_passwd、mosquitto_sub、mosquitto_pub這些命令也是要軟鏈接一下,不然也會報錯說沒有的命令
可以使用ls -l 命令來檢查符號鏈接是否正確創(chuàng)建:
ls -l /usr/bin/mosquitto
配置文件中默認(rèn)使用user mosquitto。如果不想創(chuàng)建此用戶,可以修改成其他的用戶名,如果是root,就像下面這樣,但是會可能不允許使用root,那就用默認(rèn)的mosquitto,但是會報錯沒有這個用戶就稍等創(chuàng)建。
問題:Unable to drop privileges to ‘mosquitto’ because this user does not exist. Trying ‘nobody’ instead
解決方案:
groupadd mosquitto
useradd -g mosquitto mosquitto
chown -R mosquitto:mosquitto /etc/mosquitto/
mosquitto -c /etc/mosquitto/mosquitto.conf -d -v 重新執(zhí)行啟動命令
大于2.0.0版本的mosquitto,默認(rèn)配置只會綁定到localhost,如果希望其他機(jī)器訪問,需要修改配置文件添加, 也可以自己修改端口號。
vim ?/etc/mosquitto/mosquitto.conf
listener 1883 0.0.0.0
修改密碼方法如下:
1、找到并打開Mosquitto服務(wù)器的配置文件
vi /etc/mosquitto/mosquitto.conf
2、將其中的配置選項(xiàng)allow_anonymous改為 false,禁止匿名登錄(然后再測試的命令的時候一直說沒有找到這個目錄文件,那就將這個改為true)
allow_anonymous false ??保存
3、將密碼配置選項(xiàng)配置如下:
password_file /etc/mosquitto/pwfile
4、默認(rèn)該目錄下沒有該文件,則進(jìn)入該目錄,并拷貝一份,命令如下:??
cp /etc/mosquitto/pwfile.example ?/etc/mosquitto/pwfile
5、添加用戶信息。在終端執(zhí)行以下代碼,應(yīng)用mosquitto_passwd命令創(chuàng)建用戶名, username修改成自己的
mosquitto_passwd -c /etc/mosquitto/pwfile username
執(zhí)行以后會提示輸入密碼,重復(fù)2次輸入之后,用戶名密碼配置完成。
6、重新啟動mosquitto服務(wù)之后,用戶名密碼生效
ps ?-ef|grep mosque#然后kill掉
mosquitto -c /etc/mosquitto/mosquitto.conf -d -v
問題:mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解決方案:
ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig
如果還是報錯,那就先find / -name ?libmosquitto.so.1 看看文件在哪里,再執(zhí)行上面的軟鏈接
繼續(xù)執(zhí)行,又報錯
問題: mosquitto_sub: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory
解決方案
ln -s /usr/local/lib/libcjson.so.1 /usr/lib/libcjson.so.1
ldconfig
如果還是報錯,那就先find / -name ?libcjson.so.1 看看文件在哪里,再執(zhí)行上面的軟鏈接
客戶端測試:
5?訂閱命令mosquitto_sub參數(shù)說明
6?發(fā)布命令mosquitto_pub參數(shù)說明
7?命令測試?
mosquitto_sub ?-h ip -u username -P password ?-t test_topic ?-v
mosquitto_pub ?-h ip -u username -P ?password -t test_topic ?-m 'helloworld'
8樂維監(jiān)控使用mqtt對接srne碩日太陽能動環(huán)截圖效果圖