Two sketches in one (Wifi + MQTT) + (Servo) not compatible - missing something?

Hi all,
new to Arduino WiFi Rev2 and having some problems, maybe someone can point me into the right direction.

Total noob so please bare with me…

Goal: make one sketch that includes Wifi functionality + MQTT receiving + controlling a servo (depending on what comes out of the MQTT)

I have got a working broker (I clearly see that I can publish and subscribe, no problem there). If I use the example “WifiSimpleReceive” it works fine, I see through the console that it receives the commands in Arduino. It says it is connected and waiting for commands, wifi also connected fine.

I am using a local console and Arduino Create on the web btw.

If I use some parts of a servo example it works fine also, I can control the servo as I want.

BUT - if I combine those two sketches (Wifi + MQTT) + (Servo) the mqtt stops receiving commands. I have got it to receive commands two or three times but it stopped immediately after that (no change in code).

After many trial and errors I found out that if I take away the following two rows of code from the Setup section (used for the servo functionality) the mqtt starts working again and I receive commands. But if I put the code back it stops receiving commands immediately. And without the code, the servo functionality does not work. So either or at this point. And no errors reported in the console. No errors in the debugger either, at least not to my understanding.

myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT); //Sets the pin as input

This leads me to think that I am missing something totally obvious here but I don’t understand what. Can someone please point me in the right direction?

Below is the combined code and the two rows of code I am talking about are in the beginning of the Setup section (“My stuff”). I am fully aware of that the code part that is mine is far from optimized and well written, I am just trying to get it to work at this point.

#include <Servo.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate 
//    flashed in the WiFi module.

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "192.168.1.122";
int        port     = 1883;
const char topic[]  = "#";


//My stuff
Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int gateState = 0; // 0 = closed, 1 = open
int openGate = 0; //Coming from Google Assistant
int buttonPin = 2; // the pin that the button is connected to. If + comes in on that, then switch state
int reading;
int state = LOW;
int previous = HIGH;
int startUp = 1;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

char incomingMessage[8];



void setup() {
  
  //My stuff   ------ here are the two rows that makes mqtt stop working -------
  //myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  //pinMode(buttonPin, INPUT); //Sets the pin as input

  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a unique client ID, if not set the library uses Arduino-millis()
  // Each client must have a unique client ID
  // mqttClient.setId("clientId");

  // You can provide a username and password for authentication
  // mqttClient.setUsernamePassword("username", "password");

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  Serial.print("Subscribing to topic: ");
  Serial.println(topic);
  Serial.println();

  // subscribe to a topic
  mqttClient.subscribe(topic);

  // topics can be unsubscribed using:
  // mqttClient.unsubscribe(topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(topic);
  Serial.println();
  
  
}

void loop() {
  int messageSize = mqttClient.parseMessage();
  if (messageSize) {
    // we received a message, print out the topic and contents
    Serial.print("Received a message with topic '");
    Serial.print(mqttClient.messageTopic());
    Serial.print("', length ");
    Serial.print(messageSize);
    Serial.println(" bytes:");

    // use the Stream interface to print the contents
    while (mqttClient.available()) {
      Serial.print((char)mqttClient.read());
      openGate = 1;
    }
    Serial.println();

    Serial.println();
  }
  
  
  
  //My stuff
  reading = digitalRead(buttonPin);

  if ((reading == HIGH && previous == LOW && millis() - time > debounce) || openGate == 1) {
    gateState = 1;
    time = millis();
    myservo.write(170);
    Serial.println("Opening the gate");
    openGate = 0;
  }


  previous = reading;




  if ((gateState == 1 && millis() - time > 2000) || startUp == 1)
  {
    gateState = 0;
    openGate = 0;
    startUp = 0;
    myservo.write(-170);
    Serial.println("Closing the gate");
  }

  
  
  
}

Problem solved. Found another post with a totally unrelated problem and by changing the servo from pin 9 to pin 8 the mqtt started working fine. Maybe this will help somebody else that comes across this post.