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

