Payload issue

sahbin
Posts: 2
Joined: Fri Mar 01, 2019 12:52 pm

Payload issue

Postby sahbin » Fri Mar 01, 2019 2:40 pm

Hello everyone,
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() {
}

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Payload issue

Postby chegewara » Fri Mar 01, 2019 6:10 pm

Maybe try to use len value to get data. Mqtt payload is not 0 terminated string.

sahbin
Posts: 2
Joined: Fri Mar 01, 2019 12:52 pm

Re: Payload issue

Postby sahbin » Fri Mar 01, 2019 8:17 pm

Thank you very much

this piece of code worked
char new_payload[len+1];
new_payload[len] = '\0';
strncpy(new_payload, payload, len);

Edje11
Posts: 18
Joined: Thu May 17, 2018 10:33 am
Contact:

Re: Payload issue

Postby Edje11 » Fri Mar 01, 2019 10:28 pm

Payload is a pointer to the payload, by using the variable again will be overwritten try something like this.

Code: Select all

char *mypayload = payload;

if (strcmp(topic, Blinds_Topic) == 0)  {
    setRelay(payload);
    mqttClient.publish(Blinds_Feedback_Topic, 1, false, mypayload);
    Serial.println("Publishing Feedback");

Who is online

Users browsing this forum: No registered users and 127 guests