1

I have a Raspberry Pi Zero 2W on 32bit PiOS running the following Docker containers: Node-RED, Mosquitto, and Influxdb. I have a Seeed Studios Xiao ESP32C3 running a C program compiled by the Arduino IDE, that monitors a BMP280. In Node-Red, I have set the mqtt broker "Keep Alive" element set to 600. I also re-booted the Pi after setting this parameter.

My problem arose when I set a time delay between sequential readings of the sensor and publishing the data to the mqtt broker. The first report after resetting the Xiao comes through fine. I have programmed the Xiao to publish a log message every x seconds in a while loop that uses the delay(x) function. If I set x to anything more than 24 seconds (e.g. delay(24000)), the connection to the mqtt broker is lost. I have a check for client.connected() which re-establishes the connection if it is lost. The Arduino IDE Serial Monitor shows that the loop is running, that the mqtt broker connection is re-established, then the first report comes through, and the connection is lost again.

I seem to be missing something somewhere.

1
  • 2
    There is not enough information here to really help. I assume all the delay(x) is in the arduino code? Edit the question to show the code.
    – hardillb
    Commented Dec 4, 2022 at 19:00

2 Answers 2

1

I suggest you tail the MQTT broker logs, and see if the client is getting kicked off because it's exceeding the KeepAlive period.

Best guess is that the delays end up being longer than the default KeepAlive so you get booted. You can't just stop the loop for extended periods of time because the MQTT client needs to run more regularly than you are sending data.

2 options:

  1. Increase the default keep alive so that 1.5 * the value is longer than your delay period.
  2. Set the delay to a much shorter internval and let the loop run more often, but keep a count of how many times you've been round the loop, and when delay * loop counter = sensor reading delay, then send the message.
5
  • I understood the KeepAlive parameter is in seconds. As I mentioned in my original post, I set the KeepAlive to 600 (that would be ten minutes). I set the x in delay(x) to 24000. I understood that delay() is in milliseconds, so that would be 24 seconds. Commented Dec 5, 2022 at 2:29
  • Keep alive is a per client setting, setting it to 600 in Node-RED will do nothing to the client on the ESP32
    – hardillb
    Commented Dec 5, 2022 at 8:04
  • So the answer to my original question, "what am I missing," is "the KeepAlive parameter is set by the client, unless it is intentionally superseded by the server." Commented Dec 5, 2022 at 15:14
  • No, that isn't quite it either. The server does not 'supersede' the client KeepAlive interval. On the other hand, the Node-RED mqtt nodes are clients, that require an mqtt server connection, for which Node-RED sets its own KeepAlive interval. This controls the connection between Node-RED and the mqtt server, and has nothing to do with the connection between the server and any other clients to which it may be connected. Commented Dec 5, 2022 at 16:04
  • As originally said, we are having to guess because there isn't enough information in the original question to do more than that.
    – hardillb
    Commented Dec 5, 2022 at 16:10
0

What I was missing is that Node-RED mqtt nodes (mqtt in, mqtt out) are ALSO mqtt clients. As such, they require connection to a broker, for which they must specify their own KeepAlive interval...which is what the Keep Alive element of the Node-RED mqtt broker set-up is for. This has no effect on any other clients (and their respective KeepAlive intervals) that may be connected to the broker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.