RishiC
RishiC

Reputation: 826

Recieves only partial messages

I am using MQTT to get some data from my server to my app. I'm using the mqtt_client package of Dart for this.

The data I'm sending is json, but it is being sent as a string, so I doubt the problem lies there (please correct me if I'm wrong)

I'm able to recieve some messages completely, but some are not being recieved completely. I ran a mosquitto subscriber on a different machine to listen to the same messages as the app, but there the complete message is being printed.

Flutter Code (relevant part):

void mqttConnect() async {
  client = MqttServerClient(broker, '');
  client.port = port;
  client.logging(on: true);
  client.keepAlivePeriod = 60;
  client.onDisconnected = _onDisconnected;
  client.onConnected = _onConnected;

  final mqtt.MqttConnectMessage connMess = mqtt.MqttConnectMessage()
      .withClientIdentifier(clientIdentifier)
      .startClean() // Non persistent session for testing
      .keepAliveFor(60)
      .withWillQos(mqtt.MqttQos.atMostOnce);
  client.connectionMessage = connMess;

  try {
    await client.connect(username, passwd);
  } catch (e) {
    print(e);
    _disconnect();
  }

  // ignore: deprecated_member_use
  if (client.connectionState == mqtt.MqttConnectionState.connected) {
    connectionState = client.connectionState;
  } else {
    _disconnect();
  }

  client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
    final MqttPublishMessage message = c[0].payload;
    final payload =
        MqttPublishPayload.bytesToStringAsString(message.payload.message);
    print('Received message:$payload from topic: ${c[0].topic}>');

  });
}

Expected message:

{
  "Sensors": [
    {
      "Sensor_name": "esp3",
      "Sensor_ID": "sensor_f6c068b81a054824b824bc6a883d677d",
      "MAC_address": "00:CC:00:CC:00:CC",
      "Value1": {
        "Value_Name": "",
        "Value_ID": "value_923bd7c893284a74affbf02170dbe5f2",
        "Value": "",
        "Unit": ""
      },
      "Value2": {
        "Value_Name": "",
        "Value_ID": "value_69c5deffeb664953a09fc60c93c8f0a7",
        "Value": "",
        "Unit": ""
      },
      "Value3": {
        "Value_Name": "",
        "Value_ID": "value_1b40bd7c83654a3686bfce7fddf6015f",
        "Value": "",
        "Unit": ""
      },
      "Value4": {
        "Value_Name": "",
        "Value_ID": "value_e6cf1f5393914ce7ba5bbc94164d24ed",
        "Value": "",
        "Unit": ""
      },
      "Value5": {
        "Value_Name": "",
        "Value_ID": "value_07d549690750422fbd9fcc385b18d040",
        "Value": "",
        "Unit": ""
      },
      "Value6": {
        "Value_Name": "",
        "Value_ID": "value_cd24d78e90284745b852289973838e05",
        "Value": "",
        "Unit": ""
      }
    },
    {
      "Sensor_name": "esp4",
      "Sensor_ID": "sensor_a737e70afdbe491ca5b8a69da4f25e4c",
      "MAC_address": "00:DD:00:DD:00:DD",
      "Value1": {
        "Value_Name": "",
        "Value_ID": "value_1a420fc35c8142a7b73b6b473e75ff8e",
        "Value": "",
        "Unit": ""
      },
      "Value2": {
        "Value_Name": "",
        "Value_ID": "value_b11246b0fe1746dc849afa2f5dd2e35d",
        "Value": "",
        "Unit": ""
      },
      "Value3": {
        "Value_Name": "",
        "Value_ID": "value_d6c05f81205c439eb68f09ab3b1c080d",
        "Value": "",
        "Unit": ""
      },
      "Value4": {
        "Value_Name": "",
        "Value_ID": "value_c35860167f2546008e31a1ea3561b3fb",
        "Value": "",
        "Unit": ""
      },
      "Value5": {
        "Value_Name": "",
        "Value_ID": "value_f04f562130734192a295e3a8a085b4a5",
        "Value": "",
        "Unit": ""
      },
      "Value6": {
        "Value_Name": "",
        "Value_ID": "value_b4a7da43d54e487ba5122526e1948400",
        "Value": "",
        "Unit": ""
      }
    }
  ]
}

Recieved Message:

{
  "Sensors": [
    {
      "Sensor_name":"esp3",
      "Sensor_ID":"sensor_f6c068b81a054824b824bc6a883d677d",
      "MAC_address":"00:CC:00:CC:00:CC",
      "Value1":{
        "Value_Name":"",
        "Value_ID":"value_923bd7c893284a74affbf02170dbe5f2",
        "Value":"",
        "Unit":""
      },
      "Value2":{
        "Value_Name":"",
        "Value_ID":"value_69c5deffeb664953a09fc60c93c8f0a7",
        "Value":"",
        "Unit":""
      },
      "Value3":{
        "Value_Name":"",
        "Value_ID":"value_1b40bd7c83654a3686bfce7fddf6015f",
        "Value":"",
        "Unit":""
      },
      "Value4":{
        "Value_Name":"",
        "Value_ID":"value_e6cf1f5393914ce7ba5bbc94164d24ed",
        "Value":"",
        "Unit":""
      },
      "Value5":{
        "Value_Name":"",
        "Value_ID":"value_07d549690750422fbd9fcc385b18d040",
        "Value":"",
        "Unit":""
      },
      "Value6":{
        "Value_Name":"",
        "Value_ID":"value_cd24d78e90284745b852289973838e05",
        "Value":"",
        "Unit":""
      }
    },
    {
      "Sensor_name":"esp4",
      "Sensor_ID":"sensor_a737e70afdbe491ca5b8a69da4f25e4c",
      "MAC_address":"00:DD:00:DD:00:DD",
      "Value1":{
        "Value_Name":"",
        "Value_ID":"value_1a420fc35c8142a7b73b6b473e75ff8e",
        "Value":"",
        "Unit":""
      },
      "Value2":{
        "Value_Name":"",
        "Value_ID":"value_b11246b0fe1746dc849afa2f5dd2e

As I said before, if I run a mosquitto subscriber on my terminal, and listen on the same topic, the message recieved at the terminal is complete. But on the app, it is recieving only partial.

And this doesn't happen for all messages, it is happening for only a few.

Upvotes: 0

Views: 146

Answers (1)

AVEbrahimi
AVEbrahimi

Reputation: 19184

Flutter's print text length is limited, you can't print large strings. You should split into chunks.

Try using this helper:

void printWrapped(String text) {
  final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

source :

Upvotes: 2

Related Questions