How to keep the mqtt connection from my Arduino device alive?

Dear all,

Background
I use the Grocy add-on in HA to keep track of our household stock. XDROLLOXD wrote a nice piece of software for a ESPboard to scan products and update the stock amount in Grocy (which I connected to a tts speaker too, to provide information about the scanned product).

Issues
I run into three problems with the original code:

  1. every time HA restarts, I have to unplug and plug (hard reset) in the esp board to re-establish the connection.
  2. every time my wifi reboots, I have to unplug and plug in (hard reset) the esp board to re-establish the connection.
  3. say, after 30 seconds (even when HA has not restarted and/or the wifi connection is just fine), when I scan a product I first get an mqtt(?) error code -2, have to wait 1 or 2 seconds, and with the next scan, the software works as expect. From a ‘user’ perspective it looks like if the barcode scanner needs to wake up first with a first scan before being able to do the actual work.

Solutions so far
I was able to the first two issues after some trail and error (see my pull request Patch 2 by pmkruyen · Pull Request #6 · XDROLLOXD/ESP32_Grocy_Barcode_Scanner · GitHub).

#1 by adding the retain flag in mqttClient.beginMessage(stateTopic, true, 1); (see for example line 404 in my pull request).

#2 by adding two functions which are fired after a wifi and/or mqtt connection lost is detected (see line 739 and further in my pull request).

Remaining issue
Unfortunately, I am unable to solve my third issue at this moment (I am at my top of my first mqtt project learning curve at the moment :slight_smile: ). I have some feeling that is has to do with a keep alive setting; but I don’t know how to change this to some infinitive value).

I just found out another post on this forum (MQTT disconnects - Tried keepalive but no go. ESP8266 Board - #8 by HypnoToad) in which the use of a client.loop function is recommended.

Questions
My questions are:

  1. will the use of the this client.loop function including a small delay indeed solve my problem?;
  2. if so, where do I need to call this function? At the beginning of the loop?
  3. And if so, will it also solve issue #1 and #2 in another way? Making my first adjustments redundant?

Thank you for your time.

Please let me know if I need to provide more information.

From a quick glance at the code, and checking the documentation for the arduinomqttclient library, it looks like the mqttClient.poll() call is missing from the loop.

This must be called often (you don’t need a delay, and should really never use delay() anyway because it is a blocking function) so the MQTT client can maintain the connection, send keepalives, etc.

Thank you @AaronCake for your reply!

The mqttClient.poll() is now at the bottom of the loop (approx line 913); do I need to put it at the start of the loop? Or at both places? Will try.

Yes, I agree on the delay() but thought I read it somewhere.