PubSubClient (or AduinoMqttClient) + Wifinina + Servo problem

Hi,
newbie here trying to get Arduino Wifi Rev 2 to work with servo, wifi and mqtt. No success.

Bottom line
When using wifi + mqtt + servo the wifi and/or mqtt connection becomes very unstable. As soon as the servo is out of the picture, it works very well. Servo on its own works well.
In the below example the servo is not even attached to the Arduino. So no externals are connected at all but still the problem is there.

Setup
Arduino Wifi Rev 2, firmware 1.2.1
Wifinina version 1.4.0
Servo version 1.1.3
PubSubClient 2.7.0

Description
As soon as the row with the attaching of the servo is active (see the code), the whole setup becomes very unstable. See log. I cannot use all three libraries together no matter what pin I attach the servo to. The servo functionality in itself works fine, it is the dropping of the wifi and/or mqtt that is the problem. The wifi connection and mqtt connection works fine when servo is not attached. See log.

It does not matter if I use ArduinoMqttClient or PubSubClient. Same instability. The example is with PubSubClient.

First I thought the Arduino was too far away from the AP but it is exactly in the same spot as when it works. I have tried switch the servo on/off in the code several time, same result. Works when off, does not work when on (the wifi/mqtt connection).

Code
I have attached the code in a file. It is basically exactly the WifiSimpleReceive sketch but with two lines added connected to the servo setup.

Logs WITHOUT servo active

Attempting to connect to WPA SSID: XXXXXX

You're connected to the network

Connecting to MQTT... connected!


Message arrived in topic: Home/Outside/Gate/
Message: OpenGate
The total message is:OpenGate
---------------------------------------------------


Message arrived in topic: Home/Outside/Gate/
Message: LockGate
The total message is:LockGate
---------------------------------------------------

Quite stable and working well.

Logs WITH servo active

Attempting to connect to WPA SSID: XXXXXXXX
.
You're connected to the network

Connecting to MQTT... connected!
MQTT connection lost - trying again.
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... connected!
MQTT connection lost - trying again.
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... failed with state -2
Connecting to MQTT... connected!
MQTT connection lost - trying again.
Connecting to MQTT... failed with state -2
Connecting to MQTT... connected!
MQTT connection lost - trying again.
Connecting to MQTT... failed with state -2
Connecting to MQTT... connected!

And so on. Very unstable.

I have been isolating this for a long time (newbie as stated before), now I understand a little bit more but still in the dark when coming up with a solution. It would be very appreciated if someone more experienced could push me in the right direction or tell me if I am doing something fundamentally wrong. Thanks!

WfiSimpleReceive.txt (3.51 KB)

Hi, I was experiencing similar issues (with an ESP32), but in my case not the Wifi connection to the broker went unstable but the servo operation. MQTT messages were received but the servo did not move properly. It worked without MQTT in a test loop.
The solution was the power suppy, when the servo was connected to an external 5V power suppy, the problems were gone. Maybe this helps.

bye, Raipat

Hi,
thanks for your reply but it is the same without the servo even connected as stated in my original post. But the developers have found a big problem in the Uno Wifi Rev2 library and I installed an update that made it more stable (bot not fully) if I used a delay of 500 ms in the loop. More or less than that it goes unstable again. I haven't had more time to spend on this since it really should have been working from the beginning so I don't know at the moment if there is a way to make it even better.

Thanks for your feedback!

If you want to read more about this: