256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

calling publish break connection #257

Closed horchi closed 3 years ago

horchi commented 3 years ago

Hello,

I try to publish messages like: [{"name":"A0","value":8},{"name":"A1","value":1345},{"name":"A2","value":1329}, {"name":"A3","value":8},{"name":"A4","value":1345},{"name":"A5","value":1329},{"name":"A&","value":1329}] if I do so the WiFi connection was interrupted each time and nothing is published. Here is my publish code:

   StaticJsonDocument<1024> doc;
   lastUpdateAt= millis();  

   for (int n = 0; n < 7; n++)
   {
      unsigned int pin = intToA(n);
      doc[n]["name"] = String("A") + n;
      doc[n]["value"] = analogRead(pin);
   }

   String outString = "";
   serializeJson(doc, outString);
   mqtt.publish(topicOut, outString, true, 0);

If I try it with a plain const char "....." directly in the code without the json stuff the problem is still the same. If I change the loop to " < 4 " al woks as expected.

Am I missing something essential, am I still missing a setting for the MQTT lib? I using the MKR WIFI 1010 board and the newest library version which is delivered by the library manager.

Thx and best regards Jörg

horchi commented 3 years ago

one more info about this, with:

   if (!mqtt.publish(topicOut, outString, true, 0))
   {
    lwmqtt_err_t err = mqtt.lastError();
    Serial.println("Failed to publish message " + String(err) + " : " + String(mqtt.returnCode()));
   }

I get Failed to publish message -1 : 0

Regarding the header:

typedef enum {
  LWMQTT_SUCCESS = 0,
  LWMQTT_BUFFER_TOO_SHORT = -1,
  LWMQTT_VARNUM_OVERFLOW = -2,
  LWMQTT_NETWORK_FAILED_CONNECT = -3,
  LWMQTT_NETWORK_TIMEOUT = -4,
  LWMQTT_NETWORK_FAILED_READ = -5,
  LWMQTT_NETWORK_FAILED_WRITE = -6,
  LWMQTT_REMAINING_LENGTH_OVERFLOW = -7,
  LWMQTT_REMAINING_LENGTH_MISMATCH = -8,
  LWMQTT_MISSING_OR_WRONG_PACKET = -9,
  LWMQTT_CONNECTION_DENIED = -10,
  LWMQTT_FAILED_SUBSCRIPTION = -11,
  LWMQTT_SUBACK_ARRAY_OVERFLOW = -12,
  LWMQTT_PONG_TIMEOUT = -13,
} lwmqtt_err_t;

-1 is LWMQTT_BUFFER_TOO_SHORT How I can increase the buffer?

horchi commented 3 years ago

I fount this in the Header: explicit MQTTClient(int bufSize = 128);

now I declare the client like this: MQTTClient mqtt(1024); and it is working :) may you can add this parameter to the example at the overview page?