Skip to content

MQTT transmission time #7473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
savagerex opened this issue Nov 15, 2022 · 12 comments
Closed
1 task done

MQTT transmission time #7473

savagerex opened this issue Nov 15, 2022 · 12 comments
Assignees
Labels
Resolution: Awaiting response Waiting for response of author Status: Needs investigation We need to do some research before taking next steps on this issue

Comments

@savagerex
Copy link

Board

ESP32 S3

Device Description

ESP32-S3-DevKitc-1

Hardware Configuration

NONE

Version

v2.0.5

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80Mhz

PSRAM enabled

yes

Upload speed

921600

Description

I use MQTT lib to transfer data ( '0').

it spend about 5 sec. this is too long.

i test on PC , it need 142ms.

why ESP32 need spend 5 sec???

Sketch

#include <WiFi.h>
#include <MQTT.h>
#include "mqtt_client.h"


const char* ssid = "vivo Y21";  //SW1-WIFI6
const char* pass = "1234567890";   //wireless

const char* mqtt_server = "broker.emqx.io";   //broker.emqx.io  192.168.99.124   192.168.76.176
const char *topic = "DataTopic";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";

WiFiClient net;
MQTTClient client(75000);

uint32_t i, j, k, m;
uint8_t ui8txLogBuf[75000];

#define PACKAGE 72000

unsigned long StartTime;
unsigned long EndTime;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  delay(10);
    WiFi.begin(ssid,pass);

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

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

    client.begin(mqtt_server, net);

    Serial.print("\nconnecting...");
  while (!client.connect("arduino", "public", "public")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");
    
}

void loop() {
  // put your main code here, to run repeatedly:


  for (i = 0; i < 9; i++) {
      ui8txLogBuf[j] = '0';
      ui8txLogBuf[j + 1] = '0';
      j += 2;
    }

    if(j == PACKAGE) {

               if(m == 0) {
               StartTime = millis();
               }
      
               ui8txLogBuf[j] = '\n';
               ui8txLogBuf[j+1] = '\r';
               //client.write(ui8txLogBuf, PACKAGE+2);
               char *DataBuf = (char*)ui8txLogBuf;
               //client.publish(topic, ui8txLogBuf);
               
               //client.publish(topic, ui8txLogBuf, PACKAGE+2);
               client.publish(topic, DataBuf, PACKAGE+2, false, 0);

               //client.subscribe(topic);
               m += 1;
               j=0;
            }

        if (m==1){     //400
           EndTime = millis();
           Serial.print("**************wifi Transfer Time*************");
           Serial.println(EndTime-StartTime);
           m=0;
    }

}

Debug Message

NONE

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@savagerex savagerex added the Status: Awaiting triage Issue is waiting for triage label Nov 15, 2022
@SuGlider SuGlider self-assigned this Nov 16, 2022
@VojtechBartoska
Copy link
Contributor

Before @SuGlider will start investigating this, can you share your 2 cents on this @david-cermak? Thank you in advance

@VojtechBartoska VojtechBartoska added the Status: Needs investigation We need to do some research before taking next steps on this issue label Nov 16, 2022
@SuGlider
Copy link
Collaborator

@savagerex - it may take some time to be investigated.
At a first glance, it would be necessary to check the response time from the MQTT server as well.

@SuGlider
Copy link
Collaborator

@savagerex - Could you please provide a link to the MQTT libraries used in the sketch of this issue? Thanks.

@savagerex
Copy link
Author

@vkjambit
Copy link

i just stumbled here, maybe not a solution but i have some projects using mqtt without problems and i always used this library:
https://github.com/marvinroger/async-mqtt-client.git

maybe worth a try.

@SuGlider
Copy link
Collaborator

@savagerex - I've researched about a similar issue with MQTT that I had seen in the past:
#7183

Could you please try this new code for the setup() in your testing sketch?

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  delay(10);

  // this must be called before WiFi.begin()
  WiFi.useStaticBuffers(true);   // <<=== This line may change WiFi performance...
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

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

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

  client.begin(mqtt_server, net);

  Serial.print("\nconnecting...");
  while (!client.connect("arduino", "public", "public")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");

}

@SuGlider SuGlider added Resolution: Awaiting response Waiting for response of author and removed Status: Awaiting triage Issue is waiting for triage labels Nov 21, 2022
@yourchanges
Copy link

yourchanges commented Nov 23, 2022

we face the same problem on esp32-s3-wroom-1 n8r8, we use the https://github.com/marvinroger/async-mqtt-client.git and we have aleady add the "WiFi.useStaticBuffers(true); WiFi.mode(WIFI_STA);" to receive the 156KB data from mqtt server, it uses about 2 seconds.

@bertmelis
Copy link
Contributor

bertmelis commented Nov 23, 2022

I worked on the async MQTT library and created my own MQTT library but never experienced such a lag. Not saying the bug isn't there but I cannot believe at this point the fault lies within the library.

In cases like this, Wireshark can be useful (dropped packets, retransmission...).

In the given example, you try to send 75k data. with a TCP packet size of 1500 this is 50 packets. This takes some time on an ESP32. There's no point in comparing this mcu with a PC.

@yourchanges
Copy link

yourchanges commented Nov 24, 2022

when we use the esp-idf directly, we config the wifi parameters follow by https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-guides/wifi.html#using-psram
···
WIFI_STATIC_RX_BUFFER_NUM 24
....
···
we can receive the 156KB data from mqtt server in 700ms in the same ENV.

@yourchanges
Copy link

yourchanges commented Nov 25, 2022

Now we use the esp32-arduino-lib-builder with following config modified(in file configs/defconfig.esp32s3)

CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=24
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=85
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM=32
CONFIG_ESP32_WIFI_RX_BA_WIN=32
CONFIG_ESP32_WIFI_IRAM_OPT=y
CONFIG_ESP32_WIFI_RX_IRAM_OPT=y
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=32768
CONFIG_LWIP_TCP_WND_DEFAULT=32768
CONFIG_TCP_SND_BUF_DEFAULT=32768
CONFIG_TCP_WND_DEFAULT=32768

and then re-generate the esp32-arduino static libs and replace the precompiled libs in "~/.platformio/packages/framework-arduinoespressif32/tools"

At last, we can receive the 156KB data from mqtt server in 400ms in the same ENV in the arduino-esp32 project

Only one question for us, we should run the board as long as possible to check it stability or not?

@SuGlider
Copy link
Collaborator

Yes, this is a way to solve this same issue.

When using WiFi.useStaticBuffers(true);, that makes Arduino to use this CONFIG:

CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=8
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER=y
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE=0
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM=8
CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM=16

Which is smaller that the one you suggested with Lib Builder.

Unfortunately, Arduino can't define its own WiFi low level configuration.

Another way to move forward with its own WiFi setup, would be going with Arduino as IDF Component and changing the sdkconfig using idf.py menuconfig (or even directly editing it)

@SuGlider
Copy link
Collaborator

@savagerex - Let me know if using WiFi.useStaticBuffers(true); had helped you.

If not, I think that the way to solve it, would be by moving to Arduino as Component Project and then setting high performance WiFi in the sdkconfig as suggested above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Awaiting response Waiting for response of author Status: Needs investigation We need to do some research before taking next steps on this issue
Projects
Development

No branches or pull requests

6 participants