第三方監控平臺聯動MQTT實現動環監控教程!
本文背景:主要是安裝mosquitto服務端,利用第三方平臺(如樂維監控、zabbix等)使用matt完成對srne碩日太陽能動環對接監控。
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服務
服務器建立mqtt文件夾
https://mosquitto.org/files/source/mosquitto-2.0.20.tar.gz
3安裝服務
tar -xvf mosquitto-2.0.20.tar.gz
cd mosquitto-2.0.20/
make && make install
報錯:重新執行make && make install,繼續報錯config.h:86:12: fatal error: cjson/cJSON.h: No such file or directory

解決方案:那就網上找cJSON包安裝下, 在mqtt目錄下執行安裝
先確認下是否已經安裝git,沒有的話。yum install git
git clone https://github.com/DaveGamble/cJSON.git
創建一個構建目錄并進入:
mkdir build && cd build/
使用 CMake 生成構建文件
cmake …
最后,使用 sudo make install 命令安裝 cJSON 庫到系統目錄中。這一步需要 root 權限:
sudo make install
但是可能會報錯,CMake 版本不足,版本太大,需要3.0以上的

需要先升級cmake
yum remove -y cmake
1.創建新目錄并進入:
mkdir /opt/cmake
cd /opt/cmake/
2.下載最新版本的 CMake: 您可以從 CMake 官網下載最新版本的 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
4.安裝編譯工具:
yum install -y gcc gcc-c++
5.編譯并安裝 CMake:
cd cmake-3.16.6
./configure --prefix=/usr/local/cmake
make && make install
6.創建鏈接:
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
7.驗證新版本的 CMake:
cmake --version
重新編譯 cJSON 庫
更新 CMake 版本后,您可以重新進入 cJSON 的目錄并嘗試編譯:
bash復制
cd /path/to/cJSON/build
cmake …
make
sudo make install
安裝完畢后,切換回
cd …/mosquitto-2.0.18/
重新執行make && make install

4啟動服務
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/ 目錄下創建一個名為 mosquitto 的符號鏈接,它指向 /usr/local/sbin/mosquitto。下面用到的mosquitto_passwd、mosquitto_sub、mosquitto_pub這些命令也是要軟鏈接一下,不然也會報錯說沒有的命令
可以使用 ls -l 命令來檢查符號鏈接是否正確創建:
ls -l /usr/bin/mosquitto
配置文件中默認使用user mosquitto。如果不想創建此用戶,可以修改成其他的用戶名,如果是root,就像下面這樣,但是會可能不允許使用root,那就用默認的mosquitto,但是會報錯沒有這個用戶就稍等創建。

問題: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 重新執行啟動命令
大于2.0.0版本的mosquitto,默認配置只會綁定到localhost,如果希望其他機器訪問,需要修改配置文件添加, 也可以自己修改端口號。
vim /etc/mosquitto/mosquitto.conf
listener 1883 0.0.0.0
修改密碼方法如下:
1、找到并打開Mosquitto服務器的配置文件
vi /etc/mosquitto/mosquitto.conf
2、將其中的配置選項allow_anonymous改為 false,禁止匿名登錄(然后再測試的命令的時候一直說沒有找到這個目錄文件,那就將這個改為true)
allow_anonymous false 保存
3、將密碼配置選項配置如下:
password_file /etc/mosquitto/pwfile
4、默認該目錄下沒有該文件,則進入該目錄,并拷貝一份,命令如下:
cp /etc/mosquitto/pwfile.example /etc/mosquitto/pwfile
5、添加用戶信息。在終端執行以下代碼,應用mosquitto_passwd命令創建用戶名, username修改成自己的
mosquitto_passwd -c /etc/mosquitto/pwfile username
執行以后會提示輸入密碼,重復2次輸入之后,用戶名密碼配置完成。
6、重新啟動mosquitto服務之后,用戶名密碼生效
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 看看文件在哪里,再執行上面的軟鏈接
繼續執行,又報錯
問題: 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 看看文件在哪里,再執行上面的軟鏈接
客戶端測試:???????
5訂閱命令mosquitto_sub參數說明

6發布命令mosquitto_pub參數說明

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

8樂維監控使用mqtt對接srne碩日太陽能動環截圖效果圖




浙公網安備 33010602011771號