Mosquitto是一個(gè)開(kāi)源的MQTT消息代理(broker)軟件,它遵循MQTT協(xié)議,允許設(shè)備之間高效交換消息。作為一個(gè)輕量級(jí)的消息中間件,Mosquitto支持QoS(服務(wù)質(zhì)量)等級(jí),支持發(fā)布/訂閱模式,適用于嵌入式系統(tǒng)和低功耗設(shè)備。其小巧的體積和簡(jiǎn)潔的架構(gòu)使其成為許多嵌入式應(yīng)用的理想選擇,特別是在資源受限的嵌入式開(kāi)發(fā)板上運(yùn)行。
將Mosquitto移植到嵌入式開(kāi)發(fā)板上,能夠使這些嵌入式設(shè)備具備完整的消息中間件功能。這對(duì)于需要進(jìn)行遠(yuǎn)程數(shù)據(jù)交換、設(shè)備控制和實(shí)時(shí)監(jiān)控的物聯(lián)網(wǎng)項(xiàng)目尤其重要。比如,在智能家居或環(huán)境監(jiān)控系統(tǒng)中,開(kāi)發(fā)板通過(guò)Mosquitto與其他設(shè)備或系統(tǒng)進(jìn)行通信,可以大大提升系統(tǒng)的靈活性和擴(kuò)展性。
下面將和各位小伙伴詳細(xì)介紹如何將Mosquitto及其所依賴的OpenSSL、libuuid庫(kù)移植到ElfBoard的開(kāi)發(fā)板,并在開(kāi)發(fā)板上運(yùn)行MQTT服務(wù)。
交叉編譯libuuid庫(kù)
elf@ubuntu:~/work$ tar zxvf libuuid-1.0.3.tar.gz
elf@ubuntu:~/work$ cd libuuid-1.0.3/
elf@ubuntu:~/work/libuuid-1.0.3$ mkdir install
elf@ubuntu:~/work/openssl-1.0.2g$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
5、生成Makefile
elf@ubuntu:~/work/libuuid-1.0.3$ ./configure --host=arm-poky-linux-gnueabi --prefix=$(pwd)/install
6、編譯
elf@ubuntu:~/work/libuuid-1.0.3$ make
elf@ubuntu:~/work/libuuid-1.0.3$ make install
elf@ubuntu:~/work/libuuid-1.0.3$ cd install/
elf@ubuntu:~/work/libuuid-1.0.3/install$ ls
include lib
elf@ubuntu:~/work/libuuid-1.0.3/install$ tar cvjf libuuid.tar.bz2 *
交叉編譯OpenSSL庫(kù)
1、下載地址:https://openssl-library.org/source/old/1.0.2/index.html
2、拷貝源碼到開(kāi)發(fā)環(huán)境/home/elf/work目錄下解壓
elf@ubuntu:~/work$ tar zxvf openssl-1.0.2g.tar.gz
elf@ubuntu:~/work$ cd openssl-1.0.2g/
elf@ubuntu:~/work/openssl-1.0.2g$ mkdir install
elf@ubuntu:~/work/openssl-1.0.2g$ ./config no-asm shared --prefix=$(pwd)/install
6、執(zhí)行環(huán)境變量
elf@ubuntu:~/work/openssl-1.0.2g$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
7、編譯
elf@ubuntu:~/work/openssl-1.0.2g$ make
elf@ubuntu:~/work/openssl-1.0.2g$ make install
9、查看安裝目錄下的文件并打包
elf@ubuntu:~/work/openssl-1.0.2g$ cd install/
elf@ubuntu:~/work/openssl-1.0.2g/install$ ls
bin include lib ssl
elf@ubuntu:~/work/openssl-1.0.2g/install$ tar cvjf openssl.tar.bz2 *
交叉編譯Mosquitto庫(kù)
1、下載地址:https://mosquitto.org/files/source/
2、拷貝源碼到開(kāi)發(fā)環(huán)境/home/elf/work目錄下解壓
elf@ubuntu:~/work/mosquitto-1.5.1$ tar zxvf mosquitto-1.5.1.tar.gz
elf@ubuntu:~/work/mosquitto-1.5.1$ cd mosquitto-1.5.1/
elf@ubuntu:~/work/mosquitto-1.5.1$ mkdir install
elf@ubuntu:~/work/mosquitto-1.5.1$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
elf@ubuntu:~/work/mosquitto-1.5.1$ make WITH_SRV=no CFLAGS="-I /home/elf/work/openssl-1.0.2g/install/include -I /home/elf/work/openssl-1.0.2g/install/lib -I /home/elf/work/openssl-1.0.2g/install/include/openssl -I /home/elf/work/libuuid-1.0.3/install/include -I /home/elf/work/libuuid-1.0.3/install/lib" LDFLAGS="-L /home/elf/work/openssl-1.0.2g/install/lib -L /home/elf/work/libuuid-1.0.3/install/lib -lssl -lcrypto -luuid" CROSS_COMPILE=" "
elf@ubuntu:~/work/mosquitto-1.5.1$ make DESTDIR=$(pwd)/install install
elf@ubuntu:~/work/mosquitto-1.5.1$ cd install
elf@ubuntu:~/work/mosquitto-1.5.1/install$ ls
etc usr
elf@ubuntu:~/work/mosquitto-1.5.1/install$ tar cvjf mosquitto.tar.bz2 *
root@ELF1:~# cp /run/media/sda1/libuuid.tar.bz2 /
root@ELF1:~# cp /run/media/sda1/openssl.tar.bz2 /
root@ELF1:~# cp /run/media/sda1/mosquitto.tar.bz2 /
root@ELF1:~# cd /
root@ELF1:/# tar mxvf libuuid.tar.bz2
root@ELF1:/# tar mxvf openssl.tar.bz2
root@ELF1:/# tar mxvf mosquitto.tar.bz2
root@ELF1:/# cd usr/local/
root@ELF1:/usr/local# cp -rf ./lib/* /lib
root@ELF1:/usr/local# cp -rf ./bin/* /bin
root@ELF1:/usr/local# cp -rf ./sbin/* /bin
root@ELF1:/usr/local# cd /etc/mosquitto/
root@ELF1:/etc/mosquitto# cp mosquitto.conf.example /etc/mosquitto.conf
修改/etc/mosquitto.conf文件,在第40行下添加user root。
測(cè)試
root@ELF1:~# mosquitto -d -c /etc/mosquitto.conf
?
3、一端訂閱test主題
root@ELF1:~# mosquitto_sub -t test
4、一端發(fā)布test主題
root@ELF1:~# mosquitto_pub -t test -m "hello world"
可以看到訂閱端接收到發(fā)布端的“hello world”消息,測(cè)試成功。