Problem importing MQTT example to my code

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Problem importing MQTT example to my code

Postby Jimis1 » Thu Sep 28, 2023 10:34 am

I know the easy way is open an Mqtt example from ESP idf and use that but that's not always possible. What if later you want to use another example then you will have to somehow import the "right stuff" from one example into the other.

So this is my case. I have built a project around the https OTA advanced example and now I want to include the mqtt support (example code).

I have imported all the necessary functions and files (I had to copy paste the whole damn directory esp-idf\components\mqtt\esp-mqtt\include into my project to make "mqtt_client.h" work since it couldn't be found no matter what I did).

Now I get an error of not finding "CONFIG_BROKER_BIN_SIZE_TO_SEND" which is setup by the menuconfig (sdkconfig file) . The problem is that if I open menuconfig there is no mqtt setup anywhere so I can't set it up . My sdkconfig file is missing all this
#
# ESP-MQTT Configurations
#
CONFIG_MQTT_PROTOCOL_311=y
# CONFIG_MQTT_PROTOCOL_5 is not set
CONFIG_MQTT_TRANSPORT_SSL=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET=y
CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y
# CONFIG_MQTT_MSG_ID_INCREMENTAL is not set
# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set
# CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set
# CONFIG_MQTT_USE_CUSTOM_CONFIG is not set
# CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED is not set
# CONFIG_MQTT_CUSTOM_OUTBOX is not set
# end of ESP-MQTT Configurations

# Example Configuration
#
CONFIG_BROKER_URI="mqtts://276f887f0c9540c4b3d8a6be52700425.s2.eu.hivemq.cloud:8883"
CONFIG_BROKER_CERTIFICATE_OVERRIDE=""
CONFIG_BROKER_BIN_SIZE_TO_SEND=20000
# end of Example Configuration

Since I know sdkconfig can't be edited how do I activate those in menuconfig to be able to make my code work ?

Thanks in advance

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Problem importing MQTT example to my code

Postby MicroController » Thu Sep 28, 2023 6:23 pm

since it couldn't be found no matter what I did
Be found by ... what? If the build process cannot find the file, you probably need to REQUIRE the component via cmake. If only the IDE is confused, but the build succeeds, idf.py reconfigure and a reset of the IntelliSense database may do the trick.

CONFIG_BROKER_BIN_SIZE_TO_SEND is a setting of the MQTT example application itself. It's not used/needed for MQTT operation
outside of the example.
I recommend to generally first get a grasp of what parts of an example's code actually do or demonstrate, and to only then copy relevant pieces of example code into your application.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Problem importing MQTT example to my code

Postby Jimis1 » Sat Sep 30, 2023 9:16 am

The error I get is this


[1/10] Performing build step for 'bootloader'
[1/1] cmd.exe /C "cd /D F:\_Jim\Programming\ESP32_codes\SmartHome\SmartHome_OTA_MQTT_basic\build\bootloader\esp-idf\esptool_py && c:\Applications\Programming\ESP\tools\python_env\idf5.1_py3.11_env\Scripts\python.exe C:/Applications/Programming/ESP/esp-idf/components/partition_table/check_sizes.py --offset 0x9000 bootloader 0x1000 F:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/build/bootloader/bootloader.bin"
Bootloader binary size 0x6880 bytes. 0x1780 bytes (18%) free.
[2/8] Building C object esp-idf/mqtt/CMakeFiles/__idf_mqtt.dir/mqtt.c.obj
FAILED: esp-idf/mqtt/CMakeFiles/__idf_mqtt.dir/mqtt.c.obj
C:\Applications\Programming\ESP\tools\tools\xtensa-esp32-elf\esp-12.2.0_20230208\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe -DESP_PLATFORM -DIDF_VER=\"v5.1.1\" -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DSOC_MMU_PAGE_SIZE=CONFIG_MMU_PAGE_SIZE -D_GNU_SOURCE -D_POSIX_READER_WRITER_LOCKS -IF:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/build/config -IF:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/components/mqtt/include -IC:/Applications/Programming/ESP/esp-idf/components/newlib/platform_include -IC:/Applications/Programming/ESP/esp-idf/components/freertos/FreeRTOS-Kernel/include -IC:/Applications/Programming/ESP/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/include -IC:/Applications/Programming/ESP/esp-idf/components/freertos/esp_additions/include/freertos -IC:/Applications/Programming/ESP/esp-idf/components/freertos/esp_additions/include -IC:/Applications/Programming/ESP/esp-idf/components/freertos/esp_additions/arch/xtensa/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_hw_support/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_hw_support/include/soc -IC:/Applications/Programming/ESP/esp-idf/components/esp_hw_support/include/soc/esp32 -IC:/Applications/Programming/ESP/esp-idf/components/esp_hw_support/port/esp32/. -IC:/Applications/Programming/ESP/esp-idf/components/esp_hw_support/port/esp32/private_include -IC:/Applications/Programming/ESP/esp-idf/components/heap/include -IC:/Applications/Programming/ESP/esp-idf/components/log/include -IC:/Applications/Programming/ESP/esp-idf/components/soc/include -IC:/Applications/Programming/ESP/esp-idf/components/soc/esp32 -IC:/Applications/Programming/ESP/esp-idf/components/soc/esp32/include -IC:/Applications/Programming/ESP/esp-idf/components/hal/esp32/include -IC:/Applications/Programming/ESP/esp-idf/components/hal/include -IC:/Applications/Programming/ESP/esp-idf/components/hal/platform_port/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_rom/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_rom/include/esp32 -IC:/Applications/Programming/ESP/esp-idf/components/esp_rom/esp32 -IC:/Applications/Programming/ESP/esp-idf/components/esp_common/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_system/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_system/port/soc -IC:/Applications/Programming/ESP/esp-idf/components/esp_system/port/include/private -IC:/Applications/Programming/ESP/esp-idf/components/xtensa/include -IC:/Applications/Programming/ESP/esp-idf/components/xtensa/esp32/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/include/apps -IC:/Applications/Programming/ESP/esp-idf/components/lwip/include/apps/sntp -IC:/Applications/Programming/ESP/esp-idf/components/lwip/lwip/src/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/port/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/port/freertos/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/port/esp32xx/include -IC:/Applications/Programming/ESP/esp-idf/components/lwip/port/esp32xx/include/arch -IC:/Applications/Programming/ESP/esp-idf/components/esp_event/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_http_client/include -IC:/Applications/Programming/ESP/esp-idf/components/nvs_flash/include -IC:/Applications/Programming/ESP/esp-idf/components/spi_flash/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_partition/include -IC:/Applications/Programming/ESP/esp-idf/components/app_update/include -IC:/Applications/Programming/ESP/esp-idf/components/bootloader_support/include -IC:/Applications/Programming/ESP/esp-idf/components/bootloader_support/bootloader_flash/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_app_format/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_https_ota/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_netif/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_wifi/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_wifi/wifi_apps/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_phy/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_phy/esp32/include -IC:/Applications/Programming/ESP/esp-idf/examples/common_components/protocol_examples_common/include -IF:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/components/ota -IF:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/main -IC:/Applications/Programming/ESP/esp-idf/components/driver/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/deprecated -IC:/Applications/Programming/ESP/esp-idf/components/driver/analog_comparator/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/dac/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/gpio/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/gptimer/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/i2c/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/i2s/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/ledc/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/mcpwm/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/parlio/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/pcnt/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/rmt/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/sdio_slave/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/sdmmc/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/sigma_delta/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/spi/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/temperature_sensor/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/touch_sensor/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/twai/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/uart/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/usb_serial_jtag/include -IC:/Applications/Programming/ESP/esp-idf/components/driver/touch_sensor/esp32/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_pm/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_ringbuf/include -IC:/Applications/Programming/ESP/esp-idf/components/esp_timer/include -IF:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/components/ble_helper/include -IC:/Applications/Programming/ESP/esp-idf/components/tcp_transport/include -IC:/Applications/Programming/ESP/esp-idf/components/esp-tls -IC:/Applications/Programming/ESP/esp-idf/components/esp-tls/esp-tls-crypto -IC:/Applications/Programming/ESP/esp-idf/components/mbedtls/port/include -IC:/Applications/Programming/ESP/esp-idf/components/mbedtls/mbedtls/include -IC:/Applications/Programming/ESP/esp-idf/components/mbedtls/mbedtls/library -IC:/Applications/Programming/ESP/esp-idf/components/mbedtls/esp_crt_bundle/include -mlongcalls -Wno-frame-address -fdiagnostics-color=always -ffunction-sections -fdata-sections -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=unused-but-set-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -Wno-enum-conversion -gdwarf-4 -ggdb -Og -fmacro-prefix-map=F:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic=. -fmacro-prefix-map=C:/Applications/Programming/ESP/esp-idf=/IDF -fstrict-volatile-bitfields -fno-jump-tables -fno-tree-switch-conversion -DconfigENABLE_FREERTOS_DEBUG_OCDAWARE=1 -std=gnu17 -Wno-old-style-declaration -MD -MT esp-idf/mqtt/CMakeFiles/__idf_mqtt.dir/mqtt.c.obj -MF esp-idf\mqtt\CMakeFiles\__idf_mqtt.dir\mqtt.c.obj.d -o esp-idf/mqtt/CMakeFiles/__idf_mqtt.dir/mqtt.c.obj -c F:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/components/mqtt/mqtt.c
F:/_Jim/Programming/ESP32_codes/SmartHome/SmartHome_OTA_MQTT_basic/components/mqtt/mqtt.c:14:10: fatal error: mqtt_client.h: No such file or directory
14 | #include "mqtt_client.h"
| ^~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.

* The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command ninja " terminated with exit code: 1.



The ckamelists file is this
Screenshot_2.png
Screenshot_2.png (33.7 KiB) Viewed 2728 times

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Problem importing MQTT example to my code

Postby MicroController » Sat Sep 30, 2023 3:31 pm

The problem may be that your (project-local) component is named "mqtt", and it declares a dependency on a component named "mqtt". I guess you want the dependency of your mqtt component to refer to "the other" (IDF's) mqtt component, which probably won't work as long as both components are named identically.
Rename your mqtt component.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Problem importing MQTT example to my code

Postby Jimis1 » Sat Sep 30, 2023 6:52 pm

That was the problem. Thank you !

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Problem importing MQTT example to my code

Postby Jimis1 » Sun Oct 01, 2023 9:27 am

The program compiles without errors but I get an error during running of the code. It seems it resets when it gets to ESPLOGI() macro. any ideas why is that ? I have included the log component in the cmakelists files where it is used so it should have been ok

*TAG pointer is set at
global.h
extern char *TAG ;
main.c
char *TAG;

Screenshot_3.png
Screenshot_3.png (54.65 KiB) Viewed 2622 times
Screenshot_4.png
Screenshot_4.png (31.71 KiB) Viewed 2622 times
Screenshot_5.png
Screenshot_5.png (45.99 KiB) Viewed 2622 times

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Problem importing MQTT example to my code

Postby MicroController » Sun Oct 01, 2023 3:50 pm

char *TAG;
Of course, you also need to initialize TAG before using it.

(Declaring the log tag in a header file is rather uncommon. You usually want multiple tags in your application (e.g. one tag per .c file) so that you can better correlate logged messages to code.)

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Problem importing MQTT example to my code

Postby Jimis1 » Sun Oct 01, 2023 6:08 pm

I thought of that but I had put char *TAG="ggg"; above the app_main() function in the globals declarations.
I put it inside the app_main() and it worked.

Thanks

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Problem importing MQTT example to my code

Postby MicroController » Sun Oct 01, 2023 6:56 pm

Btw, in order to not confuse the linker more than necessary, I suggest declaring tags as static globals inside each .c file:

Code: Select all

static const char* const TAG = "mytag";
or

Code: Select all

static const char TAG[] = "mytag";
(Making TAG const helps the compiler with optimizations; the type const char allows the compiler to put the string into flash instead of RAM, which is a good thing unless you want to use the string/tag in IRAM code.)

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 112 guests