How can I publish data from esp32 to emqx in Qos Level 1 or 2?

How can I publish data from esp32 to emqx in Qos Level 1 or 2 ?

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>

// WiFi
const char *ssid = "XXXXXXXXXXXX";
const char *password = "YYYYYY";

IPAddress local_IP(192, 168, X, X);
IPAddress gateway(192, X, X, X);
IPAddress subnet(X, X, X, 0);
IPAddress primaryDNS(0, 0, 0, 0);

// MQTT Broker
const char *mqtt_broker = "XXXXXXXXX";
const char *topic = "esp32/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

const int buttonUpPin = 15;
const int buttonDownPin = 2;
const int buttonClearPin = 0;

int count = 0;

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  Serial.println("-----------------------");
}

void publishCount(const char *topic, int c) {
  Serial.print("Count: ");
  Serial.println(c);
  client.publish(topic, String(c).c_str(),MQTTpubQos);
}

void setup() {
  Serial.begin(115200);
  pinMode(buttonUpPin, INPUT_PULLUP);
  pinMode(buttonDownPin, INPUT_PULLUP);
  pinMode(buttonClearPin, INPUT_PULLUP);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);
  while (!client.connected()) {
    String client_id = "esp32-client-";
    client_id += String(WiFi.macAddress());
    Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
    if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("Public emqx mqtt broker connected");
    } else {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }
  client.publish(topic,"Hi EMQX & Node-red ! I'm ESP32 ^^",MQTTpubQos);
  client.subscribe(topic);
}

void loop() {

  if (digitalRead(buttonUpPin) == LOW) {
    count++;
    publishCount(topic, count);
    delay(200);
  }
  if (digitalRead(buttonDownPin) == LOW) {
    count--;
    publishCount(topic, count);
    delay(200);
  }

  if (digitalRead(buttonClearPin) == LOW) {
    count = 0;
    publishCount(topic, count);
    while (digitalRead(buttonClearPin) == LOW) {}
    delay(200);
  }
  client.loop();
}

PS. i use PlatformIO to upload on ESP32

Your question is about the PubSubClient library not EMQX. The beauty of a protocol like MQTT is that it doesnt matter what is the broker. If you use EMQX, Mosquitto or any other broker, they all understand your client.

If you are using this PubSubClient, it says right there in the README that it cannot publish QoS 1 and 2 messages, so you’re out of luck. You may have more luck with this?