INTEGRATION¶
Introduction¶
Note
In this section, we introduce how to integrate the mqtt driver into the project.
Create a New Component¶
Warning
Before you create a component in the driver directory, make sure you have incorporated the driver directory into the project by adding the path to the driver directory to the EXTRA_COMPONENT_DIRS variable in the project-level CMakeLists.txt file.
Open the project in VSCode, open the integrated terminal, and type in
to actiate the ESP-IDF environment. Then type in the following command to create a new component namednode_mqtt: Danger
Note that there is a built-in MQTT module in esp-idf, so to avoid conflicts, we name this module node_mqtt. In the past, ESPRESSIF had a built-in MQTT module, but later ESPRESSIF stripped the MQTT module from the built-in modules, so now there is no built-in MQTT module in ESP-IDF. Our node_mqtt module still depends on this library, but this library is no longer part of the built-in modules of ESP-IDF. Now, if you want to use it, you can either use the online download method or the offline package method. Here we use the offline package method, I have put this library in the driver directory, please make sure this package is also in your project driver directory.
The command above means to create a new component named node_mqtt in the driver directory. After executing the command, you will see a new directory named node_mqtt in the driver directory. And the command will automatically generate the CMakeLists.txt file for the new component, as well as the node_mqtt.h and node_mqtt.c files.
Alternatively, you can mannually create the node_mqtt directory in the driver directory, and then create the CMakeLists.txt, node_mqtt.h, and node_mqtt.c files in the node_mqtt directory.
Replace the Code¶
Component Level
Replace the code in this section into the CMakelists.txt, node_mqtt.h, and node_mqtt.c files under the component.
Project Level
Replace the code in main.c with the corresponding code. No need to change the CMakeLists.txt file at the project level.
Compile and Flash the Project¶
Open the project in VSCode, open the integrated terminal, and type in
Now, you will be able to see the corresponding output in the terminal.