ESP32 5inch display

I am using an ESP32 5inch display for an application. The application requires communication using MQTT.

When I add PUBSUB library to a working display the MQTT client intermittently disconnects and takes a long time to connect. When I comment out the lcd->begin(); function from the code MQTT works properly.

Can someone help with this problem


/*********

  Rui Santos

  Complete project details at https://randomnerdtutorials.com  

*********/


#include <WiFi.h>

#include <PubSubClient.h>

#include <Wire.h>

#include <Adafruit_BME280.h>

#include <Adafruit_Sensor.h>

#include <Arduino_GFX_Library.h>


/*******************************************************************************

 * Screen Driver Configuration

*******************************************************************************/

Arduino_ESP32RGBPanel *bus = new Arduino_ESP32RGBPanel(

    GFX_NOT_DEFINED /* CS */, GFX_NOT_DEFINED /* SCK */, GFX_NOT_DEFINED /* SDA */,

    40 /* DE */, 41 /* VSYNC */, 39 /* HSYNC */, 0 /* PCLK */,

    45 /* R0 */, 48 /* R1 */, 47 /* R2 */, 21 /* R3 */, 14 /* R4 */,

    5 /* G0 */, 6 /* G1 */, 7 /* G2 */, 15 /* G3 */, 16 /* G4 */, 4 /* G5 */,

    8 /* B0 */, 3 /* B1 */, 46 /* B2 */, 9 /* B3 */, 1 /* B4 */

    );

Arduino_RPi_DPI_RGBPanel *lcd = new Arduino_RPi_DPI_RGBPanel(

    bus,

    800 /* width */, 0 /* hsync_polarity */, 210 /* hsync_front_porch */, 4 /* hsync_pulse_width */, 43 /* hsync_back_porch */,

    480 /* height */, 0 /* vsync_polarity */, 22 /* vsync_front_porch */, 4 /* vsync_pulse_width */, 12 /* vsync_back_porch */,

    1 /* pclk_active_neg */, 16000000 /* prefer_speed */, true /* auto_flush */);

/*******************************************************************************

 * Screen Driver Configuration  end

*******************************************************************************/




// Replace the next variables with your SSID/Password combination

const char* ssid = "**";

const char* password = "***";


// Add your MQTT Broker IP address, example:

//const char* mqtt_server = "192.168.1.144";

const char* mqtt_server = "192.168.2.22";


WiFiClient espClient;

PubSubClient client(espClient);

long lastMsg = 0;

char msg[50];

int value = 0;

unsigned long startMillis;  //some global variables available anywhere in the program

unsigned long currentMillis;


void setup() {

  Serial.begin(115200);

    //lcd->begin();           // Init Display

    delay(200);

  setup_wifi();

    client.setBufferSize(512);

  client.setServer(mqtt_server, 1883);

  client.setCallback(callback);

}


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);


  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }


  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());

}


void callback(char* topic, byte* message, unsigned int length) {

    Serial.print("Message length: ");

    Serial.print(length);

  Serial.print("  Message arrived on topic: ");

  Serial.print(topic);

  Serial.print(". Message: ");

  String messageTemp;


  for (int i = 0; i < length; i++) {

    Serial.print((char)message[i]);

    messageTemp += (char)message[i];

  }

  Serial.println();


}


void reconnect() {

  // Loop until we're reconnected

  while (!client.connected()) {

    Serial.print("Attempting MQTT connection...");

    // Attempt to connect

        startMillis = millis();

    if (client.connect("ESP8266Client")) {

      Serial.println("connected");

      // Subscribe

        currentMillis = millis();

        Serial.println(currentMillis-startMillis);


      client.subscribe("#");

    } else {

      Serial.print("failed, rc=");

      Serial.print(client.state());

      Serial.println(" try again in 5 seconds");

      // Wait 5 seconds before retrying

      delay(5000);

    }

  }

}

void loop()

{

  if (!client.connected()) {

    reconnect();

  }

  client.loop();

  delay(50);


}

Comments

Sign In or Register to comment.