How to avoid MQTT packet fragmentation?

Hi all,

The following code supposed to send an ADXL345 Buffer (32 levels) data over MQTT from my ESP32 board to the PC.

I am using wireshark to see the sent data .If I commented Line 127 my packet will be fragmented and I see packets of 1490 Byte and 235 Bytes although my actual packet Size (msg) is 860 Bytes.

If I use line 127 with the disconnect order then every thing is okay and in wireshark I see the 860 Bytes as it supposed to be.

Can anyone please tell me why I am seeing 1490 Bytes although I am sending only 860 Bytes?

Here is my code:

#include <WiFi.h>
#include <PubSubClient.h>
#include<ADXL345_WE.h>
#include<SPI.h>

#define CS_PIN 5   // Chip Select Pin
bool spi = true;    // flag that SPI shall be used
const int int2Pin = 4;
volatile bool event = false;

String FifoData;
String myStrings[32];
String data;
xyzFloat g;

const char* ssid = "NETGEAR61";
const char* password = "classyflute500";
const char* mqtt_server = "192.168.1.4";


WiFiClient espClient;
PubSubClient client(espClient);
char msg[1000];

ADXL345_WE myAcc = ADXL345_WE(CS_PIN, spi);

void eventISR() {
  event = true;
}

void setup() {
  delay (500);
  Serial.begin(115200);
  pinMode(int2Pin, INPUT_PULLDOWN);

  if (!myAcc.init()) {
    Serial.println("ADXL345 not connected!");
  }

  
  myAcc.setDataRate(ADXL345_DATA_RATE_3200);
  delay(100);
  Serial.print("Data rate: ");
  Serial.print(myAcc.getDataRateAsString());
  myAcc.setRange(ADXL345_RANGE_8G);
  Serial.print("  /  g-Range: ");
  Serial.println(myAcc.getRangeAsString());
  Serial.println();
  
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  myAcc.setMeasureMode(false);
  attachInterrupt(digitalPinToInterrupt(int2Pin), eventISR, RISING);
  myAcc.setInterruptPolarity(ADXL345_ACT_HIGH);
  myAcc.setInterrupt(ADXL345_WATERMARK, INT_PIN_2); // Interrupt when FIFO is full
  myAcc.setFifoParameters(ADXL345_TRIGGER_INT_1, 32);
  myAcc.setFifoMode(ADXL345_FIFO);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(mqtt_server)) {
      Serial.println("connected");
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long rssi = WiFi.RSSI();
  event = false; 
  
  myAcc.readAndClearInterrupts();
  myAcc.setMeasureMode(true); // this is the actual start
  while(!event){}  // wait until the fifo get full //event = true means WATERMARK interrupt triggered -> means FIFO is full
  myAcc.setMeasureMode(false); // stop measuring
  //Serial.println("FiFo full");
  
  for(int i=0; i<32; i++){ // when i > 32 samples, the values do not change because the FIFO is full
    g = myAcc.getGValues();
    myStrings[i]= "x: " + String(g.x) + " y: " + String(g.y) + " z: " + String(g.z) + "\n";
    data = data + myStrings[i];
    
    }
    data=  " RSSI: " + String(rssi) + "dB" + data;

    Serial.println(data.length());
  
  data.toCharArray(msg , data.length() + 1);

  client.publish("test1", msg);
  //client.disconnect();
  
  //emptying the strings for the next FIFO set.
  data="";
    for( int i = 0; i < MQTT_Size;  ++i ){
    msg[i] = (char)0;
    }

}

@msawaftah Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.

thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.