[solved]Code errors compiling for nextion display

I got a problem with my arduino code for nextion display, as I'm trying to compile the code says: "Error compiling for board Arduino/Genuino Mega or Mega 2560". That happenned after I added parts of the code for changing pages on display. I already wrote the parts of the code for changing pages, but I'm not sure if I did it correctly. If someone find this code confusing, this is the receiving code for my weather station (using HC-12 modules for transmission). I would be very thankful if someone can provide me solution for this problem.
Here is my code:

#include <SoftwareSerial.h>
#include "Nextion.h";
#define nexSerial Serial2

SoftwareSerial mySerial(10, 11); // TX, RX

int CurrentPage = 0;  // Create a variable to store which page is currently loaded

NexText t4 = NexText(0, 9, "t4");
NexText t2 = NexText(1, 6, "t2");
NexText t5 = NexText(2, 7, "t5");
NexPage page0 = NexPage(0, 0, "Home1"); 
NexPage page1 = NexPage(1, 0, "Home2");
NexPage page2 = NexPage(2, 0, "Home3");
char buffer[100] = {0};
NexTouch *nex_listen_list[] = 
{
    &t4,
    &t2,
    &t5,
    &page0,
    &page1,
    &page2,
    NULL
};

const byte numChars = 150;

char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
const byte varSize = 24;

char pressure[varSize] = {
  0};
char altitude[varSize] = {
  0};
char temp[varSize] = {
  0};
char rh[varSize] = {
  0};
char windspeed[varSize] = {
  0};
char winddirection[varSize] = {
  0};
char rain[4] = {
  0};
char light[4] = {
  0};

boolean newData = false;

void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;
}
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;
}
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;
}

void setup(){
  mySerial.begin(2400);
  Serial2.begin(9600);
  delay(500);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial.end();
  Serial.begin(115200);

  page0.attachPush(page0PushCallback);
  page1.attachPush(page1PushCallback);
  page2.attachPush(page2PushCallback);
  
}

void loop(){

  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
  }

}
void recvWithStartEndMarkers(){
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (mySerial.available() > 0 && newData == false) {
    rc = mySerial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void parseData(){      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
  strcpy(temp, strtokIndx); // copy it to temperature

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(pressure, strtokIndx);     // copy to 

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(altitude, strtokIndx);     // copy to 

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(rh, strtokIndx);     // copy to 

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(windspeed, strtokIndx);  // copy to 
  
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(winddirection, strtokIndx);  // copy to 
  
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(rain, strtokIndx);     // copy to 

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  strcpy(light, strtokIndx);     // copy to 

  if(rain == "Yes"){      //This part is for switching between the pages (each page has different picture for different weather)
    Serial2.print("page 2");
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  else if(rain == "No" && light == "Yes"){
    Serial2.print("page 0");
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  else if(rain == "No" && light == "No"){
    Serial2.print("page 1");
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
}

void showParsedData() {   //This part is for sending data to different text boxes on different pages depending on the weather
  if(CurrentPage == 0){
    Serial2.print("t4.txt=");  
    Serial2.print("\"");  
    Serial2.println(temp);
    Serial2.println(pressure);
    Serial2.println(rh);
    Serial2.println(windspeed);
    Serial2.println(winddirection);
    Serial2.print("\"");  
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  else if(CurrentPage == 1){
    Serial2.print("t2.txt=");  
    Serial2.print("\"");  
    Serial2.println(temp);
    Serial2.println(pressure);
    Serial2.println(rh);
    Serial2.println(windspeed);
    Serial2.println(winddirection);
    Serial2.print("\"");  
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  else if(CurrentPage == 2){
    Serial2.print("t5.txt=");  
    Serial2.print("\"");  
    Serial2.println(temp);
    Serial2.println(pressure);
    Serial2.println(rh);
    Serial2.println(windspeed);
    Serial2.println(winddirection);
    Serial2.print("\"");  
    Serial2.write(0xff);  
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  
  nexLoop(nex_listen_list);  // Check for any touch event
  
}

You've posted a load of code, but only hinted at the error messages produced.

I posted the whole code, so you can understand what I'm trying to achieve with it. So please if anyone can tell me how to change pages on nextion display with arduino.

Have a look at the Nextion instruction set for “page”. Just try a simple page change first with a number for the page. The Nextion code is very sensitive about spaces .

For me the code compiles with a bunch of warnings that probably would better be errors.

Somewhere\NextionError\NextionError.ino:2:21: warning: extra tokens at end of #include directive
 #include "Nextion.h";
                     ^
Somewhere\NextionError\NextionError.ino:54:30: warning: unused parameter 'ptr' [-Wunused-parameter]
 void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
                              ^
Somewhere\NextionError\NextionError.ino:58:30: warning: unused parameter 'ptr' [-Wunused-parameter]
 void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
                              ^
Somewhere\NextionError\NextionError.ino:62:30: warning: unused parameter 'ptr' [-Wunused-parameter]
 void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
                              ^
Somewhere\NextionError\NextionError.ino: In function 'void parseData()':
Somewhere\NextionError\NextionError.ino:156:14: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   if(rain == "Yes"){      //This part is for switching between the pages (each page has different picture for different weather)
              ^
Somewhere\NextionError\NextionError.ino:162:19: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if(rain == "No" && light == "Yes"){
                   ^
Somewhere\NextionError\NextionError.ino:162:36: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if(rain == "No" && light == "Yes"){
                                    ^
Somewhere\NextionError\NextionError.ino:168:19: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if(rain == "No" && light == "No"){
                   ^
Somewhere\NextionError\NextionError.ino:168:36: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   else if(rain == "No" && light == "No"){
                                    ^

Using software serial on a Mega seems very strange to me.

But it works only with Arduino Mega.

Kevin15:
"Error compiling for board Arduino/Genuino Mega or Mega 2560".

Kevin15:
But it works only with Arduino Mega.

Which is what you are trying to run on, aren't you?

And you still failed to tell us the error you get, I get only warnings.

Sorry for late response...
That is what arduino compiler returns me:

G:\Documents\Vremenska postaja\receiver_3.0\receiver_3.0.ino:2:21: warning: extra tokens at end of #include directive

#include "Nextion.h";

^

G:\Documents\Vremenska postaja\receiver_3.0\receiver_3.0.ino:3:25: warning: extra tokens at end of #include directive

#include "NexHardware.h";

^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.19.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp: In member function 'availableForWrite':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.19.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp:203:1: internal compiler error: Segmentation fault

}

^

Please submit a full bug report,

with preprocessed source if appropriate.

See http://gcc.gnu.org/bugs.html for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.19.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.19.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for "SD.h"
Used: G:\Documents\Arduino\libraries\SD
Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.19.0_x86__mdqgnx93n4wtt\libraries\SD
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

Topic: Compilation fails after upgrading to IDE 1.8.6 - "Segmentation fault " ? (Read 989 times)

Or a newer one with a fix internal compiler error: Segmentation fault

Thanks, will try that right away.

Thank you so much, the code works now.

Remember to fix the

G:\Documents\Vremenska postaja\receiver_3.0\receiver_3.0.ino:3:25: warning: extra tokens at end of #include directive

 #include "NexHardware.h";

                         ^

:wink: