mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
255 stars 28 forks source link

No Joy #14

Closed stevensarns closed 1 year ago

stevensarns commented 1 year ago

Installed client and broker on two ESP8266 D1 Minis. No interaction between them. Added debug statements and edited files so that only client was sending data to broker. Still no message being received. Sorry to report this as a bug, but I could find no forum, it's probably something I missed (checked core). Thank you for the excellent documentation and effort. pico.zip stevensarns@gmail.com

mlesniew commented 1 year ago

Your client is configured to connect to broker.hivemq.com, which is a public MQTT server on the internet.

Try initializing your client with the other ESP's IP and it should start working e.g.

PicoMQTT::Client mqtt("192.168.1.50");

It's also a good idea to not have long delays in the main loop. This causes mqtt.loop() to be run only every few seconds and can impact connection stability.

stevensarns commented 1 year ago

Progress! Yeah - that statement "broker.hivemq.com" did have me confused. Thanks. Now it works - sorta. It works about 1 time in 50 resets. Otherwise, the broker does not respond to any messages sent by the client. I have attached my code with your suggestions incorporated and a screen shot of the serial output of a successful session (note that the previous session was not successful before the reset button reset).

I will publish an article on my project currently employing an ESP8266 sending MQTT messages to a PC via hivemq.com. I would really like to use picoMQTT as the project would benefit greatly by replacing the PC with another ESP8266 which would obviate the need for an Internet connection.

Any suggestions on how to troubleshoot this? Thanks.

The only thing that I am uncertain about is the version of my board core package. In the Arduino IDE 1.8.13 the "Preferences" window shows: (which I think downloads the latest package when the IDE initializes) [image: image.png]

[image: broker_working.JPG][image: client_working.JPG]

On Thu, Aug 17, 2023 at 9:04 AM Michał Leśniewski @.***> wrote:

Your client is configured to connect to broker.hivemq.com, which is a public MQTT server on the internet.

Try initializing your client with the other ESP's IP and it should start working e.g.

PicoMQTT::Client mqtt("192.168.1.50");

It's also a good idea to not have long delays in the main loop. This causes mqtt.loop() to be run only every few seconds and can impact connection stability.

— Reply to this email directly, view it on GitHub https://github.com/mlesniew/PicoMQTT/issues/14#issuecomment-1682447728, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUMQY7XCCDNTKSUXDVQDGDXVYXI5ANCNFSM6AAAAAA3UC7A54 . You are receiving this because you authored the thread.Message ID: @.***>

mlesniew commented 1 year ago

I think I know what is happening.

PicoMQTT only tries to reconnect every 30 seconds by default. Depending on what you have in your sketch, PicoMQTT's first connection attempt might happen before wifi is connected. The next MQTT attempt will happen after 30 seconds, even if the wifi connection is established much earlier.

This delay is introduced to not flood the broker with connection requests (when it rejects the connection on purpose, e.g. because of wrong credentials) and also to let the sketch run other tasks instead of spending all CPU time trying to reconnect to MQTT.

See if MQTT publishes eventually start showing up in the broker after waiting ~30 seconds. You can also reduce the reconnect interval using:

PicoMQTT::Client mqtt("...");

setup() {
    mqtt.reconnect_interval_millis = 3 * 1000;   // attempt to reconnect every 3 seconds 
    mqtt.begin();
}

loop() { ... }
stevensarns commented 1 year ago

A hint! Adding the statement per your suggestion causes the main loop to hang forever.

[image: image.png]

On Fri, Aug 18, 2023 at 12:25 PM Michał Leśniewski @.***> wrote:

I think I know what is happening.

PicoMQTT only tries to reconnect every 30 seconds by default. Depending on what you have in your sketch, PicoMQTT's first connection attempt might happen before wifi is connected. The next MQTT attempt will happen after 30 seconds, even if the wifi connection is established much earlier.

This delay is introduced to not flood the broker with connection requests (when it rejects the connection on purpose, e.g. because of wrong credentials) and also to let the sketch run other tasks instead of spending all CPU time trying to reconnect to MQTT.

See if MQTT publishes eventually start showing up in the broker after waiting ~30 seconds. You can also reduce the reconnect interval using:

PicoMQTT::Client mqtt("...");

setup() { mqtt.reconnect_interval_millis = 3 * 1000; // attempt to reconnect every 3 seconds mqtt.begin(); }

loop() { ... }

— Reply to this email directly, view it on GitHub https://github.com/mlesniew/PicoMQTT/issues/14#issuecomment-1684278275, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUMQY5GQV4QND2CQJGTGADXV6XRVANCNFSM6AAAAAA3UC7A54 . You are receiving this because you authored the thread.Message ID: @.***>

mlesniew commented 1 year ago

Your attachments and screenshots don't get added to the GitHub issue when you reply via email. Please add them directly to #14.

stevensarns commented 1 year ago

picoHang

Serial printout of startup without (no hang) and with (hangs) mqtt.reconnect_interval_millis = 3 * 1000;

mlesniew commented 1 year ago

Looks like you closed the issue -- is your problem solved? If it is -- what was wrong? If it isn't -- please reopen and share your code, maybe I will be able to reproduce it.

stevensarns commented 1 year ago

Actually, my problem is pretty simple - the examples you have published "Basic_client" and "Basic_broker" do not communicate with other. I have attempted to pare down the two examples such that the client sends one message to the broker on an ESP8266 pair. I have no vested interest in my code - if you would be kind enough to create a client/broker pair that successfully communicates, I would be grateful. pico_pair.zip

mlesniew commented 1 year ago

I checked the examples and your code. They all work for me as expected.

The only case where I've seen similar symptoms was when I set the wrong broker address in the client.

I also noticed that in such cases mqtt.loop() can block for several seconds each time it is called. This slows down everything else in the loop() function. This is also why your loops counter never reached 200000 -- mqtt.loop() was slowing down the execution too much.

I've added a fix for that and it's included in release 0.3.5. Now mqtt.loop() shouldn't starve other things living in the loop() function.

Anyway, even without that fix the code you shared works fine for me.

I suggest trying the following:

stevensarns commented 1 year ago

I would like to thank you for your troubleshooting tips. My problems were related to my WiFi network (Netgear Nighthawk mesh). I set up an old WiFi router, reset to factory defaults & got it working.

On Mon, Aug 28, 2023 at 1:58 PM Michał Leśniewski @.***> wrote:

I checked the examples and your code. They all work for me as expected.

The only case where I've seen similar symptoms was when I set the wrong broker address in the client.

I also noticed that in such cases mqtt.loop() can block for several seconds each time it is called. This slows down everything else in the loop() function. This is also why your loops counter never reached 200000 -- mqtt.loop() was slowing down the execution too much.

I've added a fix for that and it's included in release 0.3.5. Now mqtt.loop() shouldn't starve other things living in the loop() function.

Anyway, even without that fix the code you shared works fine for me.

I suggest trying the following:

  • double check if you entered the right broker IP address in the client
  • connect to the same WiFi network from your computer and see if you can ping both the client and the server
  • check if firewalls or routing rules in your network can interfere with MQTT traffic
  • make sure your WiFi network has no client isolation
  • try upgrading the library to 0.3.5
  • make sure that you have the latest ESP8266 core installed

— Reply to this email directly, view it on GitHub https://github.com/mlesniew/PicoMQTT/issues/14#issuecomment-1696315631, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUMQY6VUATH2PCQYXWVQFDXXTZ4VANCNFSM6AAAAAA3UC7A54 . You are receiving this because you modified the open/close state.Message ID: @.***>

mlesniew commented 1 year ago

Great that it's working for you now!