CYD: ESP32+Wifi crashes screen

Description

I tried a simple code to show some values on the CYD (Cheap-Yellow-Display with ILI9341), received through MQTT, but the screen always is getting white on updates.

What MCU/Processor/Board and compiler are you using?

ESP32-WROOM on CYD with ILI9341

What do you want to achieve?

Show some values (e.g. temperature) received from MQTT devices

What have you tried so far?

The LVGL init works fine and I can update values, if there is no WIFI initialized. I got a successfull WIFI initialization if I put 1s delay after the display init. When updating the screen after receiving the MQTT meassge the screen turns white. No relevant log messages (INFO-Level).

LVGL version is 8.3 to be compatible through Squarline project
Updating the screen (with static values) without Wifi works stable

Code to reproduce

#include <TFT_eSPI.h>
#include <Arduino.h>
#include <SPI.h>

#define USE_UI          //if you want to use the ui export from Squareline ,pleease define USE_UI.

#ifdef USE_UI
#include <lvgl.h>
#include "ui.h"
#endif


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

const char* ssid = "+++";
const char* password = "+++";
const char* mqtt_server = "192.168.179.40";

WiFiClient espClient;
PubSubClient client(espClient);



static const uint16_t screenWidth  = 320;
static const uint16_t screenHeight = 240;
uint16_t calData[5] = { 189, 3416, 359, 3439, 1 };



TFT_eSPI lcd = TFT_eSPI(); /* TFT entity */



bool reconnect() {
  // Loop until we're reconnected
  unsigned int i = 0;
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("CYD1")) {
      Serial.println("connected");
      // Subscribe
      client.publish("CYD1/arbeitszimmer/status",  "connected to MQTT");

      client.subscribe("esp32/draussen2/bme_temperature");
      client.subscribe("esp32/draussen2/bme_humidity");

      client.loop();
      client.loop();
      return true;
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
      i++;
      if (i >5)
        return false;
    }
  }
  return true;
}

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);
  int i=0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if(i++ > 5)
      ESP.restart();
  }

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

void callbackmqtt(char* topic, byte* payload, unsigned int length) {
 // char str[20];
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

 // client.publish("esp32/epaper/state",  "received msg");

  if ((String(topic) == ("esp32/draussen2/bme_humidity"))) {     
   // strcpy(str,(char *)payload);
    hum_draussen = atof((char *)payload);
    update_draussen = true;
  }
  if ((String(topic) == ("esp32/draussen2/bme_temperature"))) {     
    temp_draussen = atof((char *)payload);
    update_draussen = true;
  }

 
}


#if defined USE_UI
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf1[ screenWidth * screenHeight / 13 ];

//_______________________
/* display flash */
void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
{
  uint32_t w = ( area->x2 - area->x1 + 1 );
  uint32_t h = ( area->y2 - area->y1 + 1 );

  lcd.startWrite();
  lcd.setAddrWindow( area->x1, area->y1, w, h );
  lcd.pushColors( ( uint16_t * )&color_p->full, w * h, true );
  lcd.endWrite();

  lv_disp_flush_ready( disp );
  //Serial.println("Flush");
}


void my_print(const char * buf)
{

    Serial.printf(buf);
    Serial.flush();
}

void setup()
{
  Serial.begin( 115200 ); /*serial init */

  //Port_D
  pinMode(25, OUTPUT);
  digitalWrite(25, LOW);

  //LCD init
  lcd.begin();          
  lcd.setRotation(1); 
  lcd.fillScreen(TFT_BLACK);
 // lcd.setTouch(calData);
  delay(100);
  //background light pin
  pinMode(27, OUTPUT);
  digitalWrite(27, HIGH);

 
  //lvgl init
  lv_init();
  
  lv_disp_draw_buf_init( &draw_buf, buf1, NULL, screenWidth * screenHeight / 13 );

  /*Display init*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init( &disp_drv );
  /*Display driver port of LVGL*/
  disp_drv.hor_res = screenWidth;
  disp_drv.ver_res = screenHeight;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  lv_disp_drv_register( &disp_drv );
  
  lv_log_register_print_cb(my_print); 
  
  ui_init();        //LVGL UI init
  lv_timer_handler();
  delay(1000);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
 // client.setCallback(callback);


  client.setCallback(callbackmqtt);

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

  client.publish("CYD1/arbeitszimmer/version","1.0");

  Serial.println( "Setup done" );
  
}


void loop()
{
  lv_timer_handler();

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

  vTaskDelay(100 / portTICK_PERIOD_MS);
  
  if(update_draussen)
  {
    /*char tbuffer[100];
    sprintf(tbuffer,"%0.1f °C %0.1f %% \0",temp_draussen,hum_draussen);
    Serial.println(tbuffer);*/
    delay(1000);
    lv_label_set_text(ui_temperatur, "ttt");    
    lv_refr_now(NULL);
    delay(1000);
    update_draussen = false;
  }

}

The thing is that I thought to put this on the other core from loop() after writing this thread. And it worked out of the box. So init and update of the screen runs on core 0 and all other stuff including Wifi on Core 1.

setup()
{
.
.

 xTaskCreatePinnedToCore(
      coreTask, /* Function to implement the task */
      "Task1", /* Name of the task */
      10000,  /* Stack size in words */
      NULL,  /* Task input parameter */
      0,  /* Priority of the task */
      NULL,  /* Task handle. */
      0); /* Core where the task should run */
  
}


void coreTask(void * parameter)
{
  Serial.print("coreTask() running on core ");
  Serial.println(xPortGetCoreID());

   //LCD init
  lcd.begin();          
  lcd.setRotation(1); 
  lcd.fillScreen(TFT_BLACK);
 // lcd.setTouch(calData);
  delay(100);
  //background light pin
 // pinMode(27, OUTPUT);
  //digitalWrite(27, HIGH);



 
  //lvgl init
  lv_init();
  
  lv_disp_draw_buf_init( &draw_buf, buf1, NULL, screenWidth * screenHeight / 13 );

  /*Display init*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init( &disp_drv );
  /*Display driver port of LVGL*/
  disp_drv.hor_res = screenWidth;
  disp_drv.ver_res = screenHeight;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  lv_disp_drv_register( &disp_drv );
  
  lv_log_register_print_cb(my_print); 
  ui_init();        //LVGL UI init

  while(true)
  {
    lv_timer_handler();
    delay(5);
    if(update_draussen)
    {
      char tbuffer[100];
      sprintf(tbuffer,"%0.1f °C\0",temp_draussen);
      Serial.println(tbuffer);
      
      lv_label_set_text(ui_temperatur,tbuffer);    
      sprintf(tbuffer,"%0.1f %% \0",hum_draussen);
      Serial.println(tbuffer);
      
      lv_label_set_text(ui_luftfeuchte,tbuffer);    
      lv_refr_now(NULL);
      
      update_draussen = false;
    }
    if(update_WZ)
    {
      //printTempHum(temp_WZ,hum_WZ, 100, 115);
    // lv_refr_now(NULL);
      update_WZ = false;
    }
  }


}