Using an ESP32, perhaps a rethink of how you are doing the thing could be in order?
First I am going to post some example code that does a thing with an ESP32 and MQTT.
/*
10/17/202 Ambrose Campbell
Project to display MQTT data on a OLED screen
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include "certs.h"
#include "sdkconfig.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include "time.h"
//
//
hw_timer_t * timer = NULL;
EventGroupHandle_t eg;
#define evtDoDisplay ( 1 << 1 )
#define evtCollectHistory ( 1 << 2 )
#define evtParseMQTT ( 1 << 3 )
#define evtDoTheAirParticleThing ( 1 << 4 )
// the events in this event groups tasks have been given a delay to avoid a possible race condition.
#define evtGroupBits ( evtCollectHistory | evtDoDisplay | evtDoTheAirParticleThing )
// CS GPIO_NUM_27, DC GPIO_NUM_12, DIN GPIO_NUM_13, CLK GPIO_NUM_14, RST GPIO_NUM_26
Adafruit_SSD1351 tft = Adafruit_SSD1351( 128, 128, GPIO_NUM_27, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_26 );
////
WiFiClient wifiClient;
PubSubClient MQTTclient( mqtt_server, mqtt_port, wifiClient );
//////
// black, blue, red, green, cyan, magenta, yellow, white, lightyellow, brown
const int DataCells = 60;
int ColorPalette[10] = { 0x0000, 0x001F, 0xF800, 0x07E0, 0x07FF, 0xF81F, 0xFFE0, 0xFFFF, 0xFFFFD8, 0xFF8040 };
int arrayCellPtr = 0;
float oPressureHistory[DataCells] = {0.0f};
String str_eTopic;
char strPayload [300] = {NULL};
float oTemperature = 0.0f;
float oHumidity = 0.0f;
float oIAQ = 0.0f; // Indexed Air Quality
float oPressure = 0.0f;
byte mac[6];
////
////
SemaphoreHandle_t sema_HistoryCompleted;
SemaphoreHandle_t sema_MQTT_Parser;;
SemaphoreHandle_t sema_MQTT_KeepAlive;
////
volatile int iDoTheThing = 0;
////
void IRAM_ATTR mqttCallback(char* topic, byte * payload, unsigned int length)
{
xSemaphoreTake( sema_MQTT_Parser, portMAX_DELAY);
str_eTopic = topic;
int i = 0;
for ( i; i < length; i++)
{
strPayload[i] = ((char)payload[i]);
}
strPayload[i] = NULL;
//log_i( "topic %s payload %s" ,str_eTopicPtr, strPayloadPtr );
xSemaphoreGive ( sema_MQTT_Parser );
xEventGroupSetBits( eg, evtParseMQTT ); // trigger tasks
} // void mqttCallback(char* topic, byte* payload, unsigned int length)
////
void IRAM_ATTR WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_STA_CONNECTED:
log_i("Connected to access point");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
log_i("Disconnected from WiFi access point");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
log_i("WiFi client disconnected");
break;
default: break;
}
}
////
void IRAM_ATTR onTimer()
{
BaseType_t xHigherPriorityTaskWoken;
iDoTheThing++;
if ( iDoTheThing == 60000 )
{
xEventGroupSetBitsFromISR( eg, evtGroupBits, &xHigherPriorityTaskWoken );
iDoTheThing = 0;
}
}
////
void setup()
{
/* Use 4th timer of 4.
1 tick 1/(80MHZ/80) = 1us set divider 80 and count up.
Attach onTimer function to timer
Set alarm to call timer ISR, every 1000uS and repeat / reset ISR (true) after each alarm
Start an timer alarm
*/
timer = timerBegin( 3, 80, true );
timerAttachInterrupt( timer, &onTimer, true );
timerAlarmWrite(timer, 1000, true);
timerAlarmEnable(timer);
//
str_eTopic.reserve(300);
//
eg = xEventGroupCreate();
//
tft.begin();
tft.fillScreen(0x0000);
//
sema_MQTT_Parser = xSemaphoreCreateBinary();
sema_HistoryCompleted = xSemaphoreCreateBinary();
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_HistoryCompleted );
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
////
xTaskCreatePinnedToCore( fparseMQTT, "fparseMQTT", 7000, NULL, 5, NULL, 1 ); // assign all to core 1, WiFi in use.
xTaskCreatePinnedToCore( fCollectHistory, "fCollectHistory", 10000, NULL, 4, NULL, 1 );
xTaskCreatePinnedToCore( MQTTkeepalive, "MQTTkeepalive", 7000, NULL, 2, NULL, 1 ); //this task makes a WiFi and MQTT connection.
xTaskCreatePinnedToCore( fUpdateDisplay, "fUpdateDisplay", 50000, NULL, 5, NULL, 1 );
////
} //setup() END
////