I am using the async-mqtt-client library but I keep getting different payloads each time I test the mqtt. Instead of getting the ON payload, the message is followed by characters and so the esp32 does not recognise the payload. How can I fix this issue? Thanks for helping
**
Message received **
topic: cmnd/home/power feedbackTopic: cmnd/home/power qos: 1 dup: 0 retain: 0 len: 2 index: 0 payload: ON⸮⸮? ⸮⸮?⸮⸮��!ᆳ⸮xV⸮⸮
Publishing Feedback
** Published packetId: 51
** Message received **
topic: cmnd/home/power feedbackTopic: cmnd/home/power qos: 1 dup: 0 retain: 0 len: 2 index: 0 payload: ON⸮⸮? ⸮⸮?�
Publishing Feedback
** Published packetId: 52
** Message received **
topic: cmnd/home/power feedbackTopic: cmnd/home/power qos: 1 dup: 0 retain: 0 len: 2 index: 0 payload: ON⸮$
⸮�⸮ظ'⸮⸮ᆳ⸮xV⸮⸮
Publishing Feedback
** Published packetId: 53
arduino code :
Code: Select all
#include <WiFi.h>
#include <AsyncMqttClient.h>
#define mySSID "xxxxx"
#define myPASSWORD "xxxxx"
AsyncMqttClient mqttClient;
#define led 27
#define Blinds_Topic "cmnd/home/power"
#define Blinds_Feedback_Topic "stat/home/power"
void setRelay(String command) {
if (command == "ON") digitalWrite(led, HIGH);
else digitalWrite(led, LOW);
}
void onMqttConnect(bool sessionPresent) {
Serial.println("** Connected to the broker **");
mqttClient.subscribe(Blinds_Topic, 1);
Serial.print("Subscribing : ");
Serial.println(Blinds_Topic);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("** Disconnected from the broker **");
Serial.println("Reconnecting to MQTT...");
mqttClient.connect();
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.print("** Subscribe acknowledged **");
Serial.print(" packetId: ");
Serial.print(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("** Unsubscribe acknowledged **");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
Serial.println("** Message received **");
Serial.print(" topic: ");
Serial.print(topic);
Serial.print(" feedbackTopic: ");
Serial.print(Blinds_Topic);
Serial.print(" qos: ");
Serial.print(properties.qos);
Serial.print(" dup: ");
Serial.print(properties.dup);
Serial.print(" retain: ");
Serial.print(properties.retain);
Serial.print(" len: ");
Serial.print(len);
Serial.print(" index: ");
Serial.print(index);
Serial.print(" payload: ");
Serial.println(payload);
if (strcmp(topic, Blinds_Topic) == 0) {
setRelay(payload);
mqttClient.publish(Blinds_Feedback_Topic, 1, false, payload);
Serial.println("Publishing Feedback");
}
}
void onMqttPublish(uint16_t packetId) {
Serial.print("** Published");
Serial.print(" packetId: ");
Serial.print(packetId);
Serial.print("\n\n");
}
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Start");
// Relay PIN
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to Wi-Fi");
WiFi.begin(mySSID, myPASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" OK");
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
// mqttClient.onSubscribe(onMqttSubscribe);
// mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(IPAddress(192, 168, 0, 101), 1883);
mqttClient.setKeepAlive(5).setCleanSession(false).setCredentials("xxxxxx", "xxxxxx").setClientId("m");
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void loop() {
}