ESP32 is three times slower than ESP8266 with MQTT and TCP related stuff

sblantipodi
Posts: 20
Joined: Thu Apr 16, 2020 9:41 am

ESP32 is three times slower than ESP8266 with MQTT and TCP related stuff

Postby sblantipodi » Wed Jun 16, 2021 8:19 pm

### Hardware:
Board: ESP32 from D32 board from Lolin
Core Installation version: 1.0.6
IDE name: Platformio (platform-espressif32 v.3.2.1)
LIB Used: pubsubclient for MQTT, world most used MQTT lib I think (v2.8)
Flash Frequency: 40Mhz
PSRAM enabled: no
Upload Speed: 500.000 or 1.000.000 or 2.000.000
Computer OS: Windows 10 and Ubuntu

### Description:
ESP32 is at least 3 times slower than ESP8266 when using MQTT or TCP more in general.
I have created a sketch that subscribe to an MQTT topic and increment a counter, every five seconds print the number of received message per second.

With ESP32 that sketch goes up to 45 messages per second,
ESP8266 scores 145 messages per second.

Isn't ESP32 supposed to be faster than ESP32?

### Sketch:
This is a really small sketch that shows the problem, it works on both ESP8266 and ESP32

Code: Select all

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#if defined(ESP8266)
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include <PubSubClient.h>
#include <FastLED.h>

const char *mqtt_server = "192.168.1.3"; //mqtt server
const char *ssid = "XXX";
const char *password = "XXX";
float framerate = 0;
float framerateCounter = 0;

WiFiClient espClient;
PubSubClient client(espClient); //lib required for mqtt

void callback(char *topic, byte *payload,
              unsigned int length) {   //callback includes topic and payload ( from which (topic) the payload is comming)
  framerateCounter++;

}

void reconnect() {
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    if (client.connect("ESP32_clientID")) {
      Serial.println("connected");
      client.subscribe("lights/glowwormluciferin/set/stream"); //topic=Demo
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void connectmqtt() {
  client.connect("ESP32_clientID");  // ESP will connect to mqtt broker with clientID
  {
    Serial.println("connected to MQTT");
    client.subscribe("lights/glowwormluciferin/set/stream"); //topic=Demo
    if (!client.connected()) {
      reconnect();
    }
  }
}


void setup() {
  Serial.begin(1000000);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  WiFi.begin(ssid, password);
  Serial.println("connected");
  client.setServer(mqtt_server, 1883);//connecting to mqtt server
  client.setCallback(callback);
  //delay(5000);
  connectmqtt();
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  EVERY_N_SECONDS(10) {
    framerate = framerateCounter > 0 ? framerateCounter / 10 : 0;
    framerateCounter = 0;
    Serial.println("framerate");
    Serial.println(framerate);
  }

}
Can you fix this performance problem on ESP32 please?
Is it a problem in its WiFiClient ?

Who is online

Users browsing this forum: No registered users and 74 guests