espressif / esp-mqtt

ESP32 mqtt component
Apache License 2.0
603 stars 255 forks source link

MQTT is frequently connecting and disconnecting say around 2 to 3 times in each second. #93

Closed vinodstanur closed 5 years ago

vinodstanur commented 5 years ago

I am running the basic example with tcp. It is connecting publishing disconnecting all happening very fast and toggling. So unable to receive any message from subscribed topic. It shows subscribed successfully but instantly it is showing disconnection event and again connection event. These are happening very fast say more than 3 times in a second. So unable to receive any message. I tried to increase the keepalive to higher values but still not working. Basically unable to receive any subscribed message because it is disconnecting as if it is programmed to disconnect immediately after connection.

My IDF version is 3.3 beta.

david-cermak commented 5 years ago

Hi @vinodstanur

Are you sure, you're using correct version of mqtt library? for idf 3.3beta1 (https://github.com/espressif/esp-idf/commit/646d36c7287d8e447b5bd76ec11071b739299887) it should be https://github.com/espressif/esp-mqtt/commit/a7b1cea5b3e246298607a8c64447765297626f36. I'm asking because the current version does not reconnect that fast (2-3 times per second) yet, reconnection takes place about 10s after disconnect event. At this moment we're merging a PR which improves reconnection, but that's not yet published.

It might be also helpful if you can share your log (would suggest to increase verbosity in menuconfig to debug and remove this line esp_log_level_set("*", ESP_LOG_INFO); from the app_main, so we can see debug logs from all modules).

vinodstanur commented 5 years ago

I installed the new esp-idf by cloning the master branch. Then updated the submodules using git command.

Then the mqtt folder appeared and now I am facing below error while compiling it.

fatal error: mqtt_client.h: No such file or directory

Actually the files are there in the mqtt/include folder under components directory but not sure why it is not included in the include path. I am not sure how to edit the huge makefile under make folder to add it. Some thing seems broken because it is supposed to work because I haven't done anything to it and just cloned the https://github.com/espressif/esp-idf and added git submodule update --init --recursive that is all I did!

git describe = v3.3-beta1-328-gabea9e4c0

vinodstanur commented 5 years ago

UPdate:

Hello, I removed the default mqtt submodule and pulled this repo. (https://github.com/espressif/esp-mqtt)

Now it is not disconnecting!

16:13:47:937] <0x1b>[0;32mI (5522) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000<0x1b>[0m␍␊
[16:13:48:302] <0x1b>[0;32mI (5852) MQTT_SAMPLE: MQTT_EVENT_CONNECTED<0x1b>[0m␍␊
[16:13:48:302] <0x1b>[0;32mI (5852) MQTT_SAMPLE: sent subscribe successful, msg_id=2898<0x1b>[0m␍␊
[16:13:48:302] <0x1b>[0;32mI (5862) MQTT_SAMPLE: sent subscribe successful, msg_id=42630<0x1b>[0m␍␊
[16:13:48:302] <0x1b>[0;32mI (5862) MQTT_SAMPLE: sent unsubscribe successful, msg_id=53758<0x1b>[0m␍␊
[16:13:48:766] <0x1b>[0;32mI (6352) MQTT_SAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=2898<0x1b>[0m␍␊
[16:13:48:766] <0x1b>[0;32mI (6352) MQTT_SAMPLE: sent publish successful, msg_id=0<0x1b>[0m␍␊
[16:13:49:098] <0x1b>[0;32mI (6672) MQTT_CLIENT: deliver_publish, message_length_read=21, message_length=16<0x1b>[0m␍␊
[16:13:49:098] <0x1b>[0;32mI (6672) MQTT_SAMPLE: MQTT_EVENT_DATA<0x1b>[0m␍␊
[16:13:49:098] TOPIC=/topic/qos0␍␍␊
[16:13:49:098] DATA=0␍␍␊
[16:13:49:370] <0x1b>[0;32mI (6952) MQTT_SAMPLE: MQTT_EVENT_UNSUBSCRIBED, msg_id=53758<0x1b>[0m␍␊
david-cermak commented 5 years ago

Does it mean that the default submodule version does not even build, but when esp-mqtt was replaced with master it builds and also works? This is strange as all IDF commits are tested not only to build all examples, but also to run basic functionality (for mqtt it check simple connection to broker via tcp, ssl, ws).

Did you setup correctly your IDF_PATH to the new idf-repo?

One way to see if correct include paths were propagated to build command is to run e.g. make -n | grep mqtt_client, where you can see all the compilation flags with include dirs...

vinodstanur commented 5 years ago

HI, Yes the default submodule mqtt is not even building. I just installed fresh esp-idf from master branch. Then initialized the submodule init. Ones it is done I can see mqtt folder in protocols. IDF path is correct.

Now if I go into mqtt_tcp example and try build, it failed.

I then cloned this mqtt repo separately and tried to build but it again failed due to the mqtt submodule issue. Then I removed the mqtt submodule using git and then tried to build this and it worked as the libs are already there in the folder. Then I merged it with my application by copying the libraries into my application folder and it is working fine now.

But I still don't understand why the standard installation example doesn't even build. May be you can also try it from your end ones so that we can confirm.

david-cermak commented 5 years ago

Oh, ok, I think I see where the issue is. You've copied the examples to IDF root path from examples/protocols including the parent mqtt folder. In this case, IDF (make) build system wrongly assumes it to be a component (there's one in component/mqtt of the same version) so there's a collision. It also explains why master esp-mqtt builds fine, because this version is not yet supposed to be an IDF component, so there is no clash. Unfortunately this is a limitation of the (legacy) make build system (works fine in CMake).

I would suggest to either

david-cermak commented 5 years ago

Hi @vinodstanur

As there's no update from your side, assuming the issue was indeed caused by this strange naming collision between mqtt component and mqtt example.

Closing the issue, but please feel free to reopen if any issue persists on your end.