Cross-Compiling Mosquitto to Transform Duo into an Intelligent Home MQTT Server
Let’s get straight to the point. When it comes to deploying an MQTT broker on embedded devices, Mosquitto is the go-to choice. While there are plenty of online cross-compilation tutorials available for ARM architecture, the same cannot be said for RISC-V. We embarked on an experimental journey and documented the following steps.
I. Source Code Acquisition
You can download the source code from Eclipse Mosquitto’s download page. The latest version at the moment is mosquitto-2.0.15.tar.gz.
After downloading to your local machine, extract it using the command: tar zxvf mosquitto-2.0.15.tar.gz
.
The compilation of Mosquitto depends on cJSON and OpenSSL, so cross-compilation of these two third-party libraries is also necessary.
cJSON (1.7.x)
-
License: MIT
-
Project: GitHub - DaveGamble/cJSON: Ultralightweight JSON parser in ANSI C
OpenSSL (1.1.1)
-
License: OpenSSL License and SSLeay License
-
Project: https://openssl.org
-
Source: GitHub - openssl/openssl: TLS/SSL and crypto library
The current latest version is openssl-1.1.1u. Sometimes, the download speed from GitHub can be slow, so you can download the archive directly from here.
II. Cross-Compilation
-
- Cross-Compiling cJSON
Modify the CMakeLists.txt file and add compilation parameters as follows:
set(CJSON_VERSION_SO 1)
set(CJSON_UTILS_VERSION_SO 1)
set(CMAKE_C_FLAGS "-mcpu=c906fdv -march=rv64imafdcv0p7xthead -mabi=lp64d -mtune=c906")
set(CMAKE_CXX_FLAGS "-mcpu=c906fdv -march=rv64imafdcv0p7xthead -mabi=lp64d -mtune=c906")
It’s recommended to set both dynamic and static library options to ‘ON’ in the ‘option’ section.
Ensure that the cross-compilation tool environment is properly set. You can verify this by typing ‘riscv’ and pressing TAB to confirm that the toolchain is correctly configured.
Next, create a ‘build’ directory within the current directory:
mkdir build
Navigate into the ‘build’ directory:
cd build
cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=Off -DBUILD_SHARED_AND_STATIC_LIBS=On -DCMAKE_INSTALL_PREFIX=$(pwd)/install_cv1800b
make
make install
This completes the cross-compilation for cJSON.
-
- Cross-compiling OpenSSL
Navigate to the OpenSSL directory:
cd openssl-1.1.1u
Ensure that the cross-compilation tool environment is properly set. You can verify this by typing ‘riscv’ and pressing TAB to confirm that the toolchain is correctly configured.
mkdir build
cd build
Generate the Makefile:
../Configure linux64-riscv64 shared no-asm --prefix=$(pwd)/install_cv1800b CROSS_COMPILE=riscv64-unknown-linux-musl-
Modify the Makefile. On lines 94 and 95, make the following changes and then save:
CFLAGS=-Wall -O3 -mcpu=c906fdv -march=rv64imafdcv0p7xthead -mcmodel=medany -mabi=lp64d
CXXFLAGS=-Wall -O3 -mcpu=c906fdv -march=rv64imafdcv0p7xthead -mcmodel=medany -mabi=lp64d
Compile and install OpenSSL:
make
make install
This completes the cross-compilation for OpenSSL.
-
- Cross-compiling Mosquitto
Enter the source code directory:
cd mosquitto-2.0.15
Ensure that the cross-compilation tool environment is properly set. You can verify this by typing ‘riscv’ and pressing TAB to confirm that the toolchain is correctly configured.
mkdir build
cd build
Modify the CMakeLists.txt file. Add compilation parameters before the following statement:
project(mosquitto)
Add the following:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR riscv64)
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_C_COMPILER "riscv64-unknown-linux-musl-gcc")
set(CMAKE_CXX_COMPILER "riscv64-unknown-linux-musl-g++")
Add compilation parameters after the following statement:
include(GNUInstallDirs)
set(CMAKE_FIND_ROOT_PATH "/home/yxm/milk-v/CV180x/opensrc/openssl-1.1.1u/build/install_cv1800b" "/home/yxm/milk-v/CV180x/opensrc/cJSON/build/install_cv1800b")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcpu=c906fdv -march=rv64imafdcv0p7xthead -mcmodel=medany -mabi=lp64d")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=c906fdv -march=rv64imafdcv0p7xthead -mcmodel=medany -mabi=lp64d")
Special Note:
Please ensure that the paths in the following line are changed to reflect the actual paths of your locally cross-compiled cJSON and OpenSSL:
set(CMAKE_FIND_ROOT_PATH "/home/yxm/milk-v/CV180x/opensrc/openssl-1.1.1u/build/install_cv1800b" "/home/yxm/milk-v/CV180x/opensrc/cJSON/build/install_cv1800b")
Next, set the documentation compilation to OFF as follows. This will disable the compilation of documentation:
option(DOCUMENTATION "Build documentation?" OFF)
Please select ON or OFF for other relevant features within the option
section to customize your build according to your requirements.
After making the necessary modifications, save the file.
Generate the build files with:
cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd)/cv1800b
Execute the compilation and installation as follows:
make
make install
This completes the compilation of Mosquitto.
III. Running on the Duo Board
You’ll need to copy all the lib
directories from the installation directories of cJSON, OpenSSL, and Mosquitto (cross-compiled) to the /usr/lib
directory on your development board.
Copy all the files from the bin
directories of the aforementioned packages to the /usr/bin
directory on your development board.
Copy the configuration files from etc/mosquitto/
to /etc/mosquitto/
on your development board.
After completing the above steps, you can start Mosquitto with the following command:
mosquitto -c /etc/mosquitto/mosquitto.conf -v
To run it in the background, use the -d
flag. This will allow you to run it as a background process while still monitoring its output.
Open another shell using SSH, and run the following command to subscribe to the “demo/1” topic. This shell will be blocked, waiting for incoming messages. When a message arrives, it will display the message content.
mosquitto_sub -h localhost -p 1883 -t "demo/1"
Open another SSH shell and use the following commands to publish messages to the “demo/1” topic:
mosquitto_pub -h localhost -p 1883 -t "demo/1" -m "test"
mosquitto_pub -h localhost -p 1883 -t "demo/1" -m "hhhhhhh"
This will publish messages to the specified topic.
Return to the previous subscription shell.
You will see the received messages.
The server also provides information about connections, subscriptions, and publications.
In addition to the mentioned tools, there are additional tools for user management and control.
/etc/mosquitto/mosquitto.conf
is the MQTT server configuration file. You can refer to the official website and search engines to configure your MQTT server according to your needs.
You can also develop your own client applications based on the compiled Mosquitto library.
For more information on using Mosquitto and configuring server files, please refer to the official documentation or search on the internet.
IV. Obtaining Software Packages
To make it easier, software packages and dynamic libraries have been uploaded to GitHub. You can directly copy these software packages to your Duo board and use the tar zxvf xxx.tar.gz
command to extract them. Then, execute the install.sh
script to complete the installation.