Commands to Nextion Stop Working

Hi everyone, I’m reaching you because I’m facing some trouble with the code execution at my Arduino. I’m just learned to programming the Arduino from about one month ago so I’m not an expert, but I found it as a very amazing platform to do whatever you want.
So, my project uses an Arduino one, relay output modules, a BMPE280 sensor, a MPU6050 and a Nextion HMI. I read some topics in the forum and found a lot of tips that makes me be able to complete my project.
My Nextion project have multiple pages, at the beginning the control to change the pages was do it at the Nextion HMI, but after that I notice that if you send commands to the Nextion display when the page is not active prodeces errors that delay the code execution at the Arduino so the solution used at some post was to control the Nextion page changes with the Arduino, but when I change to this approach some commands to the Nextion stop to work. I’m not sure if the problem is the memory of the Arduino because when I compile my code it display a message that the global variables uses a 88% percent of the memory.
I tried to increase the communication speed between the Arduino and the Nextion to 115200 bauds but still have same problem. So I start to reduce the instructions at my code commenting it and at some point the communication to the Nextion start to work….Anyone have a similar experience? Do I need to use a Mega instead an One board? :confused:

CambioPaginaMasFinal.ino (7.92 KB)

Hello mauzuk,
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance and
How to use this forum
Especially item #7 on posting code.

If you post your code in code tags according to the instructions then I will have a look.

Please note that support for Nextion on these fora is pretty much as follows:
You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so while I am happy to offer help on using my methods I cannot offer anything very helpful for any Nextion library.
The original Nextion libraries are full of bugs. There is a link from my tutorial to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.
There's also a separate Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you.
Beyond that the odd person occasionally offers a bit of help but not much.

Thanks a lot for your reply Perry. Yes I know that you don't use the Nextion Libraries, I read your posts before start programming, but unfortunately I used the libraries I don't know why, but seems to me easy to understand at this first approuch. I notice that this libraries uses a lot of memory of the Arduino just to be added to the code. I tried at the frst post to add my code directly but it has more than 9000 characteres so I attached it. Will try again. Do you think that is to much code to be handled by the One board? Or there are bugs in my code? I added comments in Spanish into my code but if you need I can translate that. Maybe is just a problem of the libraries. (I will take a lok to the Ray Livingston libraries and learn to use your methods too)

//***Libraries Declaration***

//Nextion
#include "Nextion.h"
SoftwareSerial NextionSerial(11,12);

//BMEP280Sensor
#include "Adafruit_BME280.h"
Adafruit_BME280 bme;
#define SEALEVELPRESSURE_HPA (1013.25)

//MPU6050
//#include <Wire.h>
//#define MPU 0x68

//Ratios de conversion
//#define A_R 16384.0
//#define G_R 131.0
// 
////Conversion de radianes a grados 180/PI
//#define RAD_A_DEG = 57.295779

//***Declaracion de Variables de Programa***

int Page=1;
const int Relay1 = 2;
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;
const int Relay5 = 6;
const int Relay6 = 7;
const int Relay7 = 8;
const int Relay8 = 9;

int Altitud_Escalado;

//MPU-6050 da los valores en enteros de 16 bits
//Valores sin refinar
//int16_t AcX, AcY, AcZ, GyX, GyY, GyZ;
// 
////Angulos
//float Acc[2];
//float Gy[2];
//float Angle[2];
//long tiempo_prev;
//float dt;
//float Angle0_Previous, ang_x;
//float Angle1_Previous, ang_y;
//int PitchG_Val;
//int RollG_Val;

//***Declaracion de Componentes NEXTION***

NexDSButton Boton1 = NexDSButton(1, 2, "Boton1");
NexDSButton Boton2 = NexDSButton(1, 3, "Boton2");
NexDSButton Boton3 = NexDSButton(1, 4, "Boton3");
NexDSButton Boton4 = NexDSButton(1, 5, "Boton4");
NexDSButton Boton5 = NexDSButton(1, 6, "Boton5");

NexText Pitch = NexText(2, 4, "Pitch");
NexText Roll = NexText(2, 5, "Roll");
NexGauge PitchG = NexGauge(2, 1, "PitchG");
NexGauge RollG = NexGauge(2, 2, "RollG");

NexNumber Altitud = NexNumber(3, 8, "Altitud");
NexNumber Temp = NexNumber(3, 13, "Temp");
NexNumber Humedad = NexNumber(3, 15, "Humedad");
NexWaveform WaveForm = NexWaveform(3, 4, "WaveForm");

NexButton Retorno2 = NexButton(2, 3, "Retorno2");
NexButton Retorno3 = NexButton(3, 3, "Retorno3");
NexButton GoPage2 = NexButton(1, 8, "GoPage2");
NexButton GoPage3 = NexButton(1, 9, "GoPage3");

NexPage Botonera = NexPage(1, 0, "Botonera");  // Page added as a touch event
NexPage Inclinacion = NexPage(2, 0, "Inclinacion");  // Page added as a touch event
NexPage Barometrico = NexPage(3, 0, "Barometrico");  // Page added as a touch event

//***Lista de Componentes NEXTION a Escuchar***

NexTouch *nex_listen_list[] = 
{
  &Boton1,
  &Boton2,
  &Boton3,
  &Boton4,
  &Boton5,
  &GoPage2,
  &GoPage3,
  &Retorno2,
  &Retorno3, 
  &Botonera,  // Pagina 1
  &Inclinacion,  // Pagina 2
  &Barometrico,  // Pagina 3
  NULL
};

//***FUNCIONES Para Cambio de Paginas***

void MoveToPage1(void *ptr)
{
Botonera.show();
Serial.println("Colocar pagina 1");
Page=1;
}
void MoveToPage2(void *ptr)
{
Inclinacion.show();
Serial.println("Colocar pagina 2");
Page=2;
}
void MoveToPage3(void *ptr)
{
Barometrico.show();
Serial.println("Colocar pagina 3");
Page=3;
}

//***SETUP de Programa***

void setup() {

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Relay5, OUTPUT);
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
digitalWrite(Relay5, HIGH);

//Wire.begin();
//Wire.beginTransmission(MPU);
//Wire.write(0x6B);
//Wire.write(0);
//Wire.endTransmission(true);

nexInit();

Serial.begin(9600);
Serial.println("Iniciando...");
Retorno2.attachPop(MoveToPage1, &Retorno2);
Retorno3.attachPop(MoveToPage1, &Retorno3);
GoPage2.attachPop(MoveToPage2, &GoPage2);
GoPage3.attachPop(MoveToPage3, &GoPage3);

Boton1.attachPop(Funcion_Boton1, &Boton1);
Boton2.attachPop(Funcion_Boton2, &Boton2);
Boton3.attachPop(Funcion_Boton3, &Boton3);
Boton4.attachPop(Funcion_Boton4, &Boton4);
Boton5.attachPop(Funcion_Boton5, &Boton5);

//***Inicializacion de Sensor BMPE280***
Serial.println(!bme.begin(0x76) ? F("Error en Sensor BME280!") : F("Sensor BME280! Iniciado Correctamente"));

}

//***LOOP de Programa***

void loop() {
nexLoop(nex_listen_list);

if (Page==1){Serial.println("pagina 1");};

if (Page==2){
  Serial.println("pagina 2");

////Leer los valores del Acelerometro de la IMU
//   Wire.beginTransmission(MPU);
//   Wire.write(0x3B); //Pedir el registro 0x3B - corresponde al AcX
//   Wire.endTransmission(false);
//   Wire.requestFrom(MPU,6,true); //A partir del 0x3B, se piden 6 registros
//   AcX=Wire.read()<<8|Wire.read(); //Cada valor ocupa 2 registros
//   AcY=Wire.read()<<8|Wire.read();
//   AcZ=Wire.read()<<8|Wire.read();
//
////A partir de los valores del acelerometro, se calculan los angulos Y, X
////respectivamente, con la formula de la tangente.
//   Acc[1] = atan(-1*(AcX/A_R)/sqrt(pow((AcY/A_R),2) + pow((AcZ/A_R),2)))*RAD_TO_DEG;
//   Acc[0] = atan((AcY/A_R)/sqrt(pow((AcX/A_R),2) + pow((AcZ/A_R),2)))*RAD_TO_DEG;
// 
////Leer los valores del Giroscopio
//   Wire.beginTransmission(MPU);
//   Wire.write(0x43);
//   Wire.endTransmission(false);
//   Wire.requestFrom(MPU,4,true); //A diferencia del Acelerometro, solo se piden 4 registros
//   GyX=Wire.read()<<8|Wire.read();
//   GyY=Wire.read()<<8|Wire.read();
// 
////Calculo del angulo del Giroscopio
//   Gy[0] = GyX/G_R;
//   Gy[1] = GyY/G_R;
//
// dt = (millis() - tiempo_prev) / 1000.0;
// tiempo_prev = millis();
//   
//   Angle[0] = 0.98 *(Angle0_Previous+Gy[0]*dt) + 0.02*Acc[0];
//   Angle[1] = 0.98 *(Angle1_Previous+Gy[1]*dt) + 0.02*Acc[1];
//
//Angle0_Previous=Angle[0];
//Angle1_Previous=Angle[1];
//
////Mostrar los valores por consola
//   Serial.print("Angle X: "); Serial.print(Angle[0]); //Serial.print("\n");
//   Serial.print("Angle Y: "); Serial.print(Angle[1]); Serial.print("\n------------\n");
//
//ang_x=Angle[0];
//ang_y=Angle[1]; 
//
//static char buffer[7];
//dtostrf(ang_x, 7, 2, buffer);
//Pitch.setText(buffer);
//
//dtostrf(ang_y, 7, 2, buffer);
//Roll.setText(buffer);
//
//if (ang_x >=0) {
//PitchG.setValue(ang_x);  
//}
//if (ang_x < 0) {
//PitchG_Val=map(ang_x, 0, -180, 360, 180);
//PitchG.setValue(PitchG_Val);
//}
//if (ang_y >=0) {
//RollG.setValue(ang_y);  
//}
//if (ang_y < 0) {
//RollG_Val=map(ang_y, 0, -180, 360, 180);
//RollG.setValue(RollG_Val);
//}
//  if (ang_y >= 30){
//    Roll.Set_font_color_pco(63488);
//  }
//  if (ang_y < 30 && ang_y > -30){
//    Roll.Set_font_color_pco(1917);
//  }
//  if (ang_y < -30){
//    Roll.Set_font_color_pco(63488);
//  }
//  if (ang_x >= 30){
//    Pitch.Set_font_color_pco(63488);
//  }
//  if (ang_x < 30 && ang_x > -30){
//    Pitch.Set_font_color_pco(1917);
//  }
//  if (ang_x < -30){
//    Pitch.Set_font_color_pco(63488);
//  }
//delay(10);
  
  }

if (Page==3){
  Serial.println(F("pagina 3"));
  Temp.setValue(bme.readTemperature()*100);
  Altitud.setValue(bme.readAltitude(SEALEVELPRESSURE_HPA)*100);  
  Humedad.setValue(bme.readHumidity()*100);
  Altitud_Escalado = map(bme.readAltitude(SEALEVELPRESSURE_HPA), 0, 3000, 0, 121);
  WaveForm.addValue(0, Altitud_Escalado);
  delay(400);
  };

delay(100);

}
//***FUNCIONES DE BOTONES***

void Funcion_Boton1(void *ptr)
{    
  uint32_t Estado_dual;
    Boton1.getValue(&Estado_dual);
      if(Estado_dual) 
      { digitalWrite(Relay1, LOW);}
      else
      { digitalWrite(Relay1, HIGH);}
}

void Funcion_Boton2(void *ptr)
{    
  uint32_t Estado_dual;
    Boton2.getValue(&Estado_dual);
      if(Estado_dual) 
      { digitalWrite(Relay2, LOW);}
      else
      { digitalWrite(Relay2, HIGH);}
}

void Funcion_Boton3(void *ptr)
{    
  uint32_t Estado_dual;
    Boton3.getValue(&Estado_dual);
      if(Estado_dual) 
      { digitalWrite(Relay3, LOW);}
      else
      { digitalWrite(Relay3, HIGH);}
}

void Funcion_Boton4(void *ptr)
{    
  uint32_t Estado_dual;
    Boton4.getValue(&Estado_dual);
      if(Estado_dual) 
      { digitalWrite(Relay4, LOW);}
      else
      { digitalWrite(Relay4, HIGH);}
}

void Funcion_Boton5(void *ptr)
{    
  uint32_t Estado_dual;
    Boton5.getValue(&Estado_dual);
      if(Estado_dual) 
      { digitalWrite(Relay5, LOW);}
      else
      { digitalWrite(Relay5, HIGH);}
}

I used the libraries I don't know why, but seems to me easy to understand at this first approach.

Yes, they probably are easier as they do a lot for you. You have to choose the methods that work for you, but also consider the support available, which on here is mostly me and Seithan.

I notice that this libraries uses a lot of memory of the Arduino just to be added to the code. I tried at the frst post to add my code directly but it has more than 9000 characteres so I attached it. Will try again. Do you think that is to much code to be handled by the One board?

When you say 'a lot of memory' are you referring to the message at the end when the complier finishes compiling? You should see something like this (this is not from your code):

Sketch uses 1936 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 238 bytes (2%) of dynamic memory, leaving 7954 bytes for local variables. Maximum is 8192 bytes.

Are you saying the percentage of program space used is close to 100%? That's the important figure, not the number of characters in the text.

Or there are bugs in my code?

Don't know, sorry, you are using a library, I don't use the library...

I will take a look to the Ray Livingston libraries and learn to use your methods too

Probably wise, Ray found a lot wrong with the official Nextion libraries. You could also try Seithan's library, I know nothing about it other than he wrote it himself (see my reply #1).

The One board

I find this amusing, I realise that 'Uno' translates into English as 'one', but in this case 'Uno' is being used as the name for the board, not its number, so there is nothing to translate; the name of the board is also Uno in English, and I imagine in any other language.

Thanks again Perry.

When you say 'a lot of memory' are you referring to the message at the end when the complier finishes compiling? You should see something like this (this is not from your code):

Yes, exactly. The sketch uses 21872 bytes (67%) and the global variables uses 1818 bytes (88%) and this is with all the code to use the MPU6050 commented. When I start to uncomment line by line at some point some of the commands to the Nextion that was working before stop to work. When I uncomment all the code it reaches around 90%.

If I just include the Nextion library to the code, the global variables increase to use 38% of the dynamic memory. Just this line:

#include "Nextion.h"

I find this amusing, I realise that 'Uno' translates into English as 'one',

Yes you are totally right I was checking my spelling and I didn’t notice that. Sorry for that!

My first idea was use a bigger board such as the Mega, but maybe this will not solve the problem and Nextion libraries are a problem by itself. :confused: