參考的幾個文章
- linux安裝MQTT:https://blog.csdn.net/weixin_37139197/article/details/78934961
- MQTT源碼講解:https://blog.csdn.net/weixin_37139197/article/details/78966249
- 交叉編譯OpenSSL和MQTT:https://blog.csdn.net/qq_39436605/article/details/82017454
需要準備的軟件
- Ubuntu電腦:交叉編譯使用,Ubuntu使用openwrt的編譯鏈在Ubuntu上編譯openwrt可運行的文件,複製到opwnwrt上運行
- MQTT包:paho.mqtt.c庫,編譯成動態鏈接庫給openwrt調用
- openssl:MQTT依賴的加密協議
步驟
安裝openssl
下載壓縮包即可,本人因爲使用其他程序已經使用了openssl1.0.0,所以我下載了1.0.0的編譯.
tar zxvf openssl-1.0.0t.tar.gz
cd openssl-1.0.0t/
./config no-asm shared --prefix=$PWD/install --cross-compile-prefix=/home/zsj/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-uclibc-
./config參數解析
no-asm : Do not use assembler code
shared : 編譯連接成動態庫
--prefix=$PWD/install : 指定編譯後安裝路徑
--cross-compile-prefix=/home/zsj/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-uclibc-: 指定交叉編譯工具鏈,我用了絕對路徑
修改Makefile,找到Makefile中的所有-m64,或者-m32的地方,然後刪除(應該只有兩處地方),很重要。
make
make install
查看編譯的結果
我的編譯文件在,......../openssl-1.0.0t/文件夾內,沒有到install中
此處已經OK,之後編譯MQTT只要加入openssl路徑即可
安裝paho.mqtt.c
下載,可使用git,也可以去下載壓縮包解壓,我的git速度不行都用下載壓縮包的方法.
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
修改Makefile。在129行添加openssl的鏈接目錄,注意改成你自己的路徑。
...
CFLAGS += -I./../openssl-1.0.0t/install/include
LDFLAGS += -L./../openssl-1.0.0t/install/lib
...
修改完成之後,執行如下命令:我都用絕對路徑
make CC=/home/zsj/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-uclibc-gcc
這樣會在當前目錄創建build目錄,進入build目錄,你能看到output目錄和一個版本頭文件,進入output,你能看到庫文件,examples和測試程序。
(圖片抄襲)
將Ubuntu動態庫複製到MT7620產品中,放在/usr/lib下,如圖,有mqtt的和ssl的.so
運行MQTT測試代碼
完成上面的步驟只是將MT7620這個產品支持了MQTT庫,並沒有寫應用程序測試
將Ubuntu下mqtt編譯好的例子複製到MT7620產品中,放在/test文件夾下,如圖
./MQTTClient_publish
這些只是主要步驟,細節有問題再交流.