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
}