How can i upload code from arduino IDE to esp8266 with nextion display attached to the PCB

my code can be uploaded from arduino IDE to the esp8266 microcontroller when the nextion display is detached from the PCB but the code cant be uploaded when the nextion display is attached to the PCB. the image below is the error message when i tried uploading the code with the nextion display attached to the PCB

this is my current setup

this is the code that im currently using

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include <ESP8266HTTPClient.h>
#include <SoftwareSerial.h>
#include <Nextion.h>
#include <ArduinoOTA.h>

// WiFi and ThingsBoard Credentials
const char* ssid = "JJ Phone";                // WiFi SSID
const char* password = "ILIKEANIME412";       // WiFi Password
#define TOKEN "AIRIA #0007"                   // ThingsBoard Token
const char* ThingsboardHost = "demo.thingsboard.io"; // ThingsBoard Host

// Heating System and Energy Cost
float energyUsed = 0.0;
const float temperatureRanges[][2] = { {18.0, 18.9}, {19.0, 19.9}, {20.0, 20.9}, {21.0, 21.9}, {22.0, 22.9},
                                       {23.0, 23.9}, {24.0, 24.9}, {25.0, 25.9}, {26.0, 26.9} };
const float energyRates[] = {5314, 4831, 4392, 3993, 3630, 3300, 3000, 2700, 2430};

// Nextion Display


// Sensors
#define DHTPIN 2                // GPIO pin for DHT sensor
#define DHTTYPE DHT22           // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);       // DHT sensor object
int mq131Pin = D0;              // MQ131 sensor pin
int mq9Pin = A0;                // MQ9 sensor pin
SoftwareSerial pmSerial(D6, D7); // PMS5003 pins: RX, TX
// Define the RX/TX pins for communication with Nextion
SoftwareSerial nextionserial(D6, D7); // D6 (RX) and D7 (TX)

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void setup() {
  Serial.begin(9600);
  pmSerial.begin(9600);         // Initialize PMS5003
  dht.begin();                  // Initialize DHT sensor
  nextionserial.begin(9600);   // Initialize Nextion display
  connectWiFi();
  client.setServer(ThingsboardHost, 1883);
  ArduinoOTA.begin();
}

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

  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi(); // Reconnect WiFi if disconnected
  }

  getAndSendData();
  delay(5000);

  ArduinoOTA.handle();
}

void connectWiFi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 10) {
    Serial.print(".");
    delay(500);
    attempts++;
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to WiFi: " + String(ssid));
  } else {
    Serial.println("WiFi connection failed!");
  }
}

void reconnectMQTT() {
  while (!client.connected()) {
    Serial.print("Connecting to ThingsBoard...");
    if (client.connect("ESP8266_Device", TOKEN, NULL)) {
      Serial.println("Connected to ThingsBoard");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void getAndSendData() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float o3_voltage = analogRead(mq131Pin) * (5.0 / 1024.0);
  float o3_ppm = o3_voltage / 100.0;

  int co_raw = analogRead(mq9Pin);
  float co_ppm = convertRawToCOPPM(co_raw);

  int pm25 = readPMS5003();

  StaticJsonDocument<256> doc;
  doc["temperature"] = temperature;
  doc["humidity"] = humidity;
  doc["o3_ppm"] = o3_ppm;
  doc["co_ppm"] = co_ppm;
  doc["pm2.5"] = pm25;

  String jsonString;
  serializeJson(doc, jsonString);

  String url = "http://demo.thingsboard.io/api/v1/AIRIA%20%230007/telemetry" ;

  HTTPClient http;
  http.begin(wifiClient, url);
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.POST(jsonString);
  Serial.print("HTTP response code: ");
  Serial.println(httpCode);
  Serial.println("Response payload: " + http.getString());
  http.end();

  displayOnNextion(temperature, energyUsed);
  calculateEnergyUsed(temperature);
}

float convertRawToCOPPM(int rawValue) {
  float slope = -0.00125;
  float intercept = 0.3;
  float voltage = (rawValue / 1023.0) * 5.0;
  return slope * voltage + intercept;
}

int readPMS5003() {
  if (pmSerial.available() >= 32) {
    uint8_t buffer[32];
    pmSerial.readBytes(buffer, 32);
    if (buffer[0] == 0x42 && buffer[1] == 0x4d) {
      return buffer[12] << 8 | buffer[13];
    }
  }
  return -1; // Error value
}

void calculateEnergyUsed(float temperature) {
  for (int i = 0; i < sizeof(temperatureRanges) / sizeof(temperatureRanges[0]); i++) {
    if (temperature >= temperatureRanges[i][0] && temperature <= temperatureRanges[i][1]) {
      energyUsed = energyRates[i] * 24;
      break;
    }
  }
  Serial.print("Energy Used: W");
  Serial.println(energyUsed);
}

void displayOnNextion(float temperature, float energyUsed) {
  String temperatureStr = String(temperature, 2);
  String energyUsedStr = String(energyUsed, 2);

  // Debugging: Check if correct data is being sent
  Serial.println("Sending data to Nextion:");
  Serial.print("Temperature: ");
  Serial.println(temperatureStr);
  Serial.print("Energy Used: ");
  Serial.println(energyUsedStr);

  // Send data to Nextion display
  nextionserial.print("Temperature.txt=\"");
  nextionserial.print(temperatureStr);
  nextionserial.println("\"");

  nextionserial.print("EnergyUsed.txt=\"");
  nextionserial.print(energyUsedStr);
  nextionserial.println("\"");

  delay(100); // Add a small delay to ensure the Nextion display processes the data
}

any potential method that can help resolve this issue will be greatly appreciated. im also a beginner at coding so providing instructions for the methods will be a life saver

SoftwareSerial pmSerial(D6, D7); // PMS5003 pins: RX, TX
// Define the RX/TX pins for communication with Nextion
SoftwareSerial nextionserial(D6, D7); // D6 (RX) and D7 (TX)

Are you really using the same 2 pins for 2 different purposes ?

the previous iteration of code only had pmserial using the D6 and D7 pins, i asked chatGPT for possible methods to solve this issue and it said something like the hardware serial is being used by the esp8266 so i should use software serial instead for the nextion display, but im not sure which pins are free to be used on my setup.

  • You cannot use the same pins for 2 serial devices
  • You cannot use 2 instances of SoftwareSerial in the same sketch except for very trivial cases

You need to consider using aboard with more hardware Serial interfaces such as an ESP32

1 Like

thank you for taking your time to answer my queries

Please provide a link to the board you are using. Many of the larger esp8266 boards like in your photo have an additional hardware UART independent from usb which you could be able to use for the Nextion.

@jjn

ESP8266 has 2 hardware UARTs, use the second for your Nextion

@cattledog

Beat me to it

Are both of them available for use in both directions and where does the Serial monitor interface figure in this ?

No. The second hardware uart is Tx only, but that is all that @jjn was using with the Nextion.

The ESP32 is indeed a better choice for this application, and will provide the ability to receive from the Nextion and have input to the Arduino from the touch screen.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.