Atomvm_mqtt_client no init: Failed to load port "atomvm_mqtt_client"

Hi Everyone, I have a problem initializing the atomvm mqtt module, does anyone else have this problem?

1970-01-01T00:00:03.525Z [notice] <0.6.0> higrow_wifi:got_ip/1 (/Users/joserodriguez/atomvm/higrow/src/higrow_wifi.erl:63) STA IP: {192,168,1,40}
Failed to load port "atomvm_mqtt_client".  Ensure the port is configured properly in the build.
CRASH 
======
pid: <0.1.0>

Stacktrace:
[{higrow_mqtt,start,1,[{file,"/Users/joserodriguez/atomvm/higrow/src/higrow_mqtt.erl"},{line,46}]},{higrow,start,0,[{file,"/Users/joserodriguez/atomvm/higrow/src/higrow.erl"},{line,38}]}]

cp: #CP<module: 16, label: 2, offset: 122>

x[0]: error
x[1]: {case_clause,{error,{init_stopped,badarg}}}
x[2]: {2,2,103,2,[{4,108},{16,296}],error}

Stack 
------

[]
[]
{error,{init_stopped,badarg}}
#CP<module: 4, label: 2, offset: 98>
[]
{{192,168,1,40},{255,255,255,0},{192,168,1,1}}
#CP<module: 0, label: 35, offset: 0>


Mailbox
--------


Monitors
--------


**End Of Crash Report**
AtomVM finished with return value: error
e[0;32mI (4342) AtomVM: AtomVM application terminated.  Going to sleep forever ...e[0m
e[0;32mI (31812) network_driver: Posting SNTP synchronization evente[0m
e[0;32mI (31812) network_driver: SNTP_EVENT_BASE_SYNC received.e[0m

source code

Hello, and thank you for your interest in the project! It looks like you might be trying to use the esp32 port atomvm_mqtt_client from:
GitHub - atomvm/atomvm_mqtt_client: An MQTT client library for AtomVM with the greneric_unix port of AtomVM. That repository is specifically for the esp32 platform.

To use it with esp32 it needs to be cloned into the main AtomVM project in the AtomVM/src/platforms/esp32/components directory. Then idf.py menuconfig needs to be used and the configuration saved. (You don’t need to make any changes, this just ensures the configuration is updated to include the atomvm_mqtt_client port driver).

There are details in the build intructions

If you are looking for a way to use mqtt with the generic_unix port you can take a look at a more generic example that should work on any platform with network support:

I hope this helps.

I did spot a few thing in your code that may be giving you trouble as well…
I don’t think client_id is supported in the config map, and with some mqtt servers, user and pass need to be supplied as part of the url for example: user:password@your.mqtt.server.

…but it looks like the mqtt_client driver was not built into the VM correctly. Can you give a few more details about your build environment too? Which version of ESP-IDF did you use to build the AtomVM image, and which OS you build on?

I am using using MacOS to compile.

I installed these dependencies and I am using erlang and elixir with asdf.

brew instal cmake gperf zlib

Steps:

git clone -b v4.4.7 --recursive https://github.com/espressif/esp-idf.git esp-idf-v4.4.7
cd esp-idf-v4.4.7/
. ./install.sh
. ./export.sh
cd ~
git clone https://github.com/atomvm/AtomVM.git
cd /AtomVM/src/platforms/esp32/components
git clone https://github.com/atomvm/atomvm_mqtt_client.git
cd ..
idf.py set-target esp32
idf.py reconfigure
idf.py build
./build/mkimage.sh

With menuconfig I make sure that the component is there.

Then I record the image to the esp32 and then the app. But now I get the following error.

[0;32mI (931) AtomVM: Starting AtomVM revision 0.7.0-dev+git.aec99057e[0m
e[0;32mI (941) sys: Loaded BEAM partition boot.avm at address 0x1d0000 (size=262144 bytes)e[0m
e[0;32mI (971) network_driver: Initialized network interfacee[0m
e[0;32mI (971) network_driver: Created default event loope[0m
e[0;31mE (981) AtomVM: Invalid startup avmpack. size=262144e[0m

abort() was called at PC 0x400d9f2a on core 0


Backtrace: 0x40082042:0x3ffba050 0x4008c2a5:0x3ffba070 0x40093ed6:0x3ffba090 0x400d9f2a:0x3ffba100 0x401f8a2c:0x3ffba140 0x4008f965:0x3ffba160




ELF file SHA256: 951b52a593974764

Rebooting...

One more problem I spotted in the atomvm_mqtt_client documentation, the links to the main AtomVM documentation are broken :disappointed:. The link to the build instructions is broken, it should be:
https://www.atomvm.net/doc/v0.6.0/build-instructions.htm

Yes, but what I did was if there was something different in the git workflows and I didn’t find something different from how I had been compiling.

Your build steps took correct. Are you using minicom for monitoring the serial? If so can you try using idf.py monitor from the src/platforms/esp32 directory? (It should decode the backtrace and give us some more clues) This looks like the default application (one that runs if no user application is flashed) is crashing, we had a problem with this happening on esp32-s3 builds just before the v0.6.0 release, but that bug was fixed, and it didn’t affect other esp32 chipsets.

You also might try completely erasing the flash with esptool, and then reflash the VM image and your application.

I (931) AtomVM: Starting AtomVM revision 0.7.0-dev+git.aec99057
I (941) sys: Loaded BEAM partition boot.avm at address 0x1d0000 (size=262144 bytes)
I (971) network_driver: Initialized network interface
I (971) network_driver: Created default event loop
E (981) AtomVM: Invalid startup avmpack. size=262144

abort() was called at PC 0x400d9f2a on core 0
0x400d9f2a: app_main at /Users/joserodriguez/atomvm/AtomVM/src/platforms/esp32/main/main.c:85 (discriminator 3)



Backtrace: 0x40082042:0x3ffba050 0x4008c2a5:0x3ffba070 0x40093ed6:0x3ffba090 0x400d9f2a:0x3ffba100 0x401f8a2c:0x3ffba140 0x4008f965:0x3ffba160
0x40082042: panic_abort at /Users/joserodriguez/atomvm/esp-idf-v4.4.7/components/esp_system/panic.c:408

0x4008c2a5: esp_system_abort at /Users/joserodriguez/atomvm/esp-idf-v4.4.7/components/esp_system/esp_system.c:137

0x40093ed6: abort at /Users/joserodriguez/atomvm/esp-idf-v4.4.7/components/newlib/abort.c:46

0x400d9f2a: app_main at /Users/joserodriguez/atomvm/AtomVM/src/platforms/esp32/main/main.c:85 (discriminator 3)

0x401f8a2c: main_task at /Users/joserodriguez/atomvm/esp-idf-v4.4.7/components/freertos/port/port_common.c:141 (discriminator 2)

0x4008f965: vPortTaskWrapper at /Users/joserodriguez/atomvm/esp-idf-v4.4.7/components/freertos/port/xtensa/port.c:142

Yes, I always do an erase clean when I install a new image.

1 Like

Hello @joserp93, I have your same setup and I have MQTT working on my esp32 devices. I’d suggest you try with latest ESP-IDF v5.1.2 as that’s the only difference we have. Other than that, feel free to post error messages/code and we will take a look.

1 Like

Hi @arpunk, I try now.

This was going to be my next suggestion, but it should still work with a 4.4 version of ESP-IDF. I honestly haven’t tested with 4.4 in a while, mostly using 5.1 myself. I will try a build with 4.4 and see if I can recreate the error… it may be when 5.x support was added something was accidentally broken for 4.4 builds.

@Winford @arpunk

I still have the same error.

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7080
load:0x40078000,len:15584
load:0x40080400,len:4
0x40080400: _init at ??:?

ho 8 tail 4 room 4
load:0x40080404,len:3876
entry 0x4008064c
I (30) boot: ESP-IDF v5.1.2 2nd stage bootloader
I (31) boot: compile time Apr 11 2024 16:20:54
I (31) boot: Multicore bootloader
I (35) boot: chip revision: v1.0
I (39) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 001c0000
I (91) boot:  3 boot.avm         RF data          01 01 001d0000 00040000
I (99) boot:  4 main.avm         RF data          01 01 00210000 00100000
I (106) boot: End of partition table
I (111) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=33894h (211092) map
I (195) esp_image: segment 1: paddr=000438bc vaddr=3ff80063 size=00008h (     8) load
I (196) esp_image: segment 2: paddr=000438cc vaddr=3ffb0000 size=03ee4h ( 16100) load
I (208) esp_image: segment 3: paddr=000477b8 vaddr=40080000 size=08860h ( 34912) load
I (224) esp_image: segment 4: paddr=00050020 vaddr=400d0020 size=125efch (1203964) map
I (659) esp_image: segment 5: paddr=00175f24 vaddr=40088860 size=11b18h ( 72472) load
I (689) esp_image: segment 6: paddr=00187a44 vaddr=400c0000 size=00064h (   100) load
I (703) boot: Loaded app from partition at offset 0x10000
I (703) boot: Disabling RNG early entropy source...
I (714) cpu_start: Multicore app
I (715) cpu_start: Pro cpu up.
I (715) cpu_start: Starting app cpu, entry point is 0x40081774
0x40081774: call_start_cpu1 at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/esp_system/port/cpu_start.c:157

I (702) cpu_start: App cpu up.
I (733) cpu_start: Pro cpu start user code
I (733) cpu_start: cpu freq: 160000000 Hz
I (733) cpu_start: Application information:
I (738) cpu_start: Project name:     atomvm-esp32
I (743) cpu_start: App version:      v0.6.0-29-gaec99057
I (749) cpu_start: Compile time:     Apr 11 2024 16:21:22
I (755) cpu_start: ELF file SHA256:  d692a5851c8ebb19...
I (761) cpu_start: ESP-IDF:          v5.1.2
I (766) cpu_start: Min chip rev:     v0.0
I (771) cpu_start: Max chip rev:     v3.99
I (775) cpu_start: Chip rev:         v1.0
I (780) heap_init: Initializing. RAM available for dynamic allocation:
I (788) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (793) heap_init: At 3FFB86B8 len 00027948 (158 KiB): DRAM
I (800) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (806) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (812) heap_init: At 4009A378 len 00005C88 (23 KiB): IRAM
I (820) spi_flash: detected chip: generic
I (823) spi_flash: flash io: dio
I (828) app_start: Starting scheduler on CPU0
I (832) app_start: Starting scheduler on CPU1
I (832) main_task: Started on CPU0
I (842) main_task: Calling app_main()

    ###########################################################

       ###    ########  #######  ##     ## ##     ## ##     ##
      ## ##      ##    ##     ## ###   ### ##     ## ###   ###
     ##   ##     ##    ##     ## #### #### ##     ## #### ####
    ##     ##    ##    ##     ## ## ### ## ##     ## ## ### ##
    #########    ##    ##     ## ##     ##  ##   ##  ##     ##
    ##     ##    ##    ##     ## ##     ##   ## ##   ##     ##
    ##     ##    ##     #######  ##     ##    ###    ##     ##

    ###########################################################

I (892) AtomVM: Starting AtomVM revision 0.7.0-dev+git.aec99057
I (902) sys: Loaded BEAM partition boot.avm at address 0x1d0000 (size=262144 bytes)
I (932) network_driver: Initialized network interface
I (932) network_driver: Created default event loop
E (942) AtomVM: Invalid startup avmpack. size=262144

abort() was called at PC 0x400d9fce on core 0
0x400d9fce: app_main at /Users/joserodriguez/atomvm/AtomVM/src/platforms/esp32/main/main.c:85 (discriminator 3)



Backtrace: 0x40081dda:0x3ffba8b0 0x4008c829:0x3ffba8d0 0x40094832:0x3ffba8f0 0x400d9fce:0x3ffba960 0x401f3da7:0x3ffba9a0 0x4008f319:0x3ffba9d0
0x40081dda: panic_abort at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/esp_system/panic.c:452
0x4008c829: esp_system_abort at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/esp_system/port/esp_system_chip.c:84
0x40094832: abort at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/newlib/abort.c:38
0x400d9fce: app_main at /Users/joserodriguez/atomvm/AtomVM/src/platforms/esp32/main/main.c:85 (discriminator 3)
0x401f3da7: main_task at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/freertos/app_startup.c:208 (discriminator 13)
0x4008f319: vPortTaskWrapper at /Users/joserodriguez/atomvm/esp-idf-v5.1.2/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162





ELF file SHA256: d692a5851c8ebb19

I should not be necessary to add atomvmlib and atomvm_mqtt_client to the applications in your .app.src file, I don’t know if this is causing rebar3 or packbeam to corrupt the final avm file, but you might try removing those entries. You may need to run: rebar3 atomvm packbeam -f just to be completely sure packbeam repacks the application before reflashing it to the esp32.

This is happening to me with an application as simple as blinky. It seems to me that there is a problem in compiling AtomVM, I am going to try to do it in Linux.

Indeed. If blinky isn’t working there is something fundamentally wrong with the builds.

1 Like

I don’t use MacOS, but maybe @arpunk has some insight into problems getting set up on MacOS. There may be some missing information in the MacOS section of the Getting Started and Build Instructions. If you find something there that is inaccurate, hard to follow, or missing, please let us know so we can fix it.

I compiled in a docker with Ubuntu and the same problem continues. With and without the mqtt component.

This makes me wonder if something is going wrong in the process of flashing your application. How are you flashing your application? Can you post the commands you use and the output?