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:
- every time HA restarts, I have to unplug and plug (hard reset) in the esp board to re-establish the connection.
- every time my wifi reboots, I have to unplug and plug in (hard reset) the esp board to re-establish the connection.
- 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 ). 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:
- will the use of the this
client.loop
function including a smalldelay
indeed solve my problem?; - if so, where do I need to call this function? At the beginning of the loop?
- 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.