Wait after send(msg)

My gateway connects to an MQTT broker which connects to Home Assistant. My problem is that if I don’t have a wait(mS) after the send(msg), packets get lost. I have found that I need to wait 2000 mS so that packets don’t get lost.

On the flip side, if I request:

  wait(2000,2,V_TEXT); // wait 2secs or until a V_TEXT message is received
  if (SamplePeriodReceived) {
    MY_SERIALDEVICE.print(F("----------------Sample Period sync'd:"));MY_SERIALDEVICE.println(SamplePeriod);
  else {
    MY_SERIALDEVICE.println(F("----------------Sample Period NOT sync'd: "));

I get a response sufficiently quick.

  1. the documentation says that the second parameter in wait() is a command, I put 2, for “req” and it works, but I don’t have a clue why it is there, but needs to be there so that the wait will wait for either the timeout or a V_TEXT packet coming in.)
  2. SamplePeriodReceived is set true in the interrupt service routine receive().

How can I speed up the sends?