Opta fails to reconnect to MQTT broker after broker restart

I have an Opta connect via ethernet to a Mosquitto MQTT broker on a Raspberry Pi. Sketch uploads are done by the IDE 2.3.0 and version 4.1.1 Arduino Mbed OS Opta Boards is installed.

This is the sketch i used for the Opta. pubsubclient/examples/mqtt_basic at master · knolleary/pubsubclient · GitHub

I have tried different sketches and a different mqtt library with the same results. ArduinoMqttClient/examples/WiFiSimpleReceive at master · arduino-libraries/ArduinoMqttClient · GitHub

When I reboot the Raspberry Pi the Opta fails to reconnect to the Mosquitto broker. I have a client in Node-RED that is able to reconnect, so I know the broker is responding correctly.

Any assistance would be appreciated.

I have the same problem using WiFi with the MQTT-library from Joel Gaehwiler.

Does it work ??

I also tried mqtt library with Portenta machine control but its does not get connect to broker using PLC IDE. using arduino IDE it works.

Rakesh

Hi, if everyone found the solution, I'll be interested by also. Facing same issue on OPTA while any other device it's OK. Any workaround would be fine also :slight_smile:

Hey @mathieu4585 which other device have you tried it on? I contacted arduino support but that said everything is fine.

Does anyone have an Portenta board to test this senario on?

I solved the issue with my opta by adding a client.disconnect() when waiting for a new reconnection

1 Like

Thanks @alb-6 , can you please share your sketch.

Sure:

void reconnect()
{
  // Ensure clean disconnect
  client.disconnect();
  delay(100);

  logMessage("Attempting MQTT connection...");

  // Attempt to connect to the MQTT broker
  client.connect("ArduinoOpta");
  if (client.connect("ArduinoOpta"))
  {
    logMessage("connected");
  }
  else
  {
    logMessage("failed, rc=");
    logMessage(String(client.state()));
    logMessage(" try again in 5 seconds");

  }
}

Please note the logMessage function is just a wrapper on Serial.println() basically with some logic inside

4 Likes

Great @alb-6 ! Your simple algorithm avoids turning Opta into a opening-socket machine gun. Even though very powerful, Opta -and every single machine- has limited resources. Moreover, pausing between reconections help you to deal with some firewalls than can detect connection rate abuse and block you.

Generally speaking, handle carefully your socket instead open and close multiple, it improves application responsiveness and bandwidth.

1 Like