MQTT payload garbage

Hello,
I’ve started my journey with OpenHab few weeks ago, reading a lot topics, and finnaly i have decided to implement this solution in my home.
First, easy project is to implement ESP32 with relay connected to controll light via MQTT.
I have wrote, simple program witch works in fact, but there is strange behaviour:
Payload messages received from OpenHab have aditional characters after real message and looks like:

payload: : OFF⸮ᆳ⸮xV⸮⸮0⸮⸮?8
  • Platform information:
    • Hardware: Raspberry PI 4 (4GB RAM)
    • openHAB version: 3.1.0

My sketch code:

#include <AsyncMqttClient.h>
#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"
  #include "freertos/timers.h"
}

const int Button_1 = 14;
const int Relay_1 = 32;

int button_1_State = 0;
int relay_1_State = 0;
int pushed = 0;
int relay_count =0;
int i =0;

#define WIFI_SSID "******"
#define WIFI_PASSWORD "******"

TimerHandle_t wifiReconnectTimer;

#define MQTT_HOST IPAddress(192, 168, ***,***)
#define MQTT_PORT 1883
#define MQTT_USERNAME "******"
#define MQTT_PASSWORD "******"
AsyncMqttClient mqttClient;

TimerHandle_t mqttReconnectTimer;

void connectToWifi(){
  Serial.println("Connecting to Wi-FI...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  uint16_t packetIdSub = mqttClient.subscribe("esp32/Button/Button1", 2);
  Serial.print("Subscribing at QoS 2, packetId: ");
  Serial.println(packetIdSub);
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
  mqttClient.publish("esp32/online/LWT",1,true,"ON");
  mqttClient.publish("esp32/Button/Button1",1,true,"OFF");
}

void onMqttPublish(uint16_t packetId) {
  Serial.println("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}

void WiFiEvent(WiFiEvent_t event){
  Serial.printf("[WiFi-event] event: %d\n", event);
  switch(event){
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
    break;
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  Serial.print("  qos: ");
  Serial.println(qos);
}


void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  Serial.println("Publish received.");
  Serial.print("  topic: ");
  Serial.println(topic);
  Serial.print("  qos: ");
  Serial.println(properties.qos);
  Serial.print("  dup: ");
  Serial.println(properties.dup);
  Serial.print("  retain: ");
  Serial.println(properties.retain);
  Serial.print("  len: ");
  Serial.println(len);
  Serial.print("  index: ");
  Serial.println(index);
  Serial.print("  total: ");
  Serial.println(total);
  Serial.print(" payload: ");
  Serial.println(payload);


  if ( strncmp((char *)payload, "ON",2)==0){
    digitalWrite(Relay_1,LOW);
  }
  if ( strncmp((char *)payload, "OFF",3) == 0) {
    digitalWrite(Relay_1,HIGH);
  }
}

void setup() {
Serial.begin(115200);
pinMode(Relay_1, OUTPUT);
pinMode(Button_1, INPUT_PULLUP);
digitalWrite(Relay_1,HIGH);
relay_1_State = 1;
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.onMessage(onMqttMessage);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.setCredentials(MQTT_USERNAME, MQTT_PASSWORD);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setWill("esp32/online/LWT",1,true,"OFF");
connectToWifi();
}

void loop() {
button_1_State = digitalRead(Button_1);

if (button_1_State == HIGH && relay_1_State == LOW){
  pushed = 1-pushed;
  delay(100);
  if (pushed == HIGH){
  digitalWrite(Relay_1,HIGH);
  
  mqttClient.publish("esp32/Button/Button1",1,true,"OFF");
}else{
  digitalWrite(Relay_1,LOW);
  mqttClient.publish("esp32/Button/Button1",1,true,"ON");
}
}
relay_1_State = button_1_State;
}

When i’m switching that swtich from OpenHab in mqtt logs we can find information:

1635073689: Sending PUBLISH to mqtt-explorer-01a093bd (d0, q0, r0, m0, 'esp32/Button/Button1', ... (3 bytes))

Witch means that OpenHab is sending properly “OFF” message.
In MQTT Explorer i can find also proper information:
Przechwytywanie
But in fact ESP32 on serial port monitor i have garbage in payload:

As you can see in my sketch code, i have solved that by reading only 3 (or 2) first characters from message, but for me seems to be only temporary solution.

Do you have any idea what could be wrong?
Edited: There is one more strange behaviour. This payload message i can read only from serial port monitor from Arduino IDE. I’m writing my code in Visual Studio Code. VSC serial port monitor stop showing any messages when i’m trying to println with payload message. I need to close monitor and open it again to get new messages, but on first message received it stops again.
As you can see on picture (in comparrision to serial port monitor from Arduino IDE) it should show right now payload message:

Only a minority of OH users are using ESPs. Only a minority of those are using custom written code on the ESP and instead are using something like Tasmota or ESPEasy. Given this really doesn’t have anything to do with OH you will have better luck asking on an ESP or Arduino forum.