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.

James Risner
  • 105
  • 1
  • 2
  • 4

2 Answers2

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.
hardillb
  • 12,813
  • 1
  • 21
  • 34
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.