Access slider value on page 2, being on page 0 (nextion)

Hello everyone.
When I turn on my project, it displays page 0 of Nextion.
I have page 2 that contains several sliders (h4, h1, h2, h3) with fine adjustments.
The problem is that when I turn it on, it goes to page 0, but it doesn't send the values ​​of the sliders on page 2.
Interestingly, if I click the button to go to page 2 in Nextion, it sends the values ​​I need.
But I want it to send the values ​​of the sliders on page 2 without loading it.
Without this, the PWM output doesn't work; it only works when I click the button to go to page 2 and display it.
I attached the values ​​of the sliders on page 0 to the Nextion EEPROM, so the slider values ​​are saved with the last adjustment I made. I also set the sliders in Nextion to (global vscope) so that they are visible on another page, but somehow it doesn't work. Could someone help me? I'm a beginner.

I bet it's the code causing the issue, but as you have not posted any, who knows!

Ok, here's my code...
I'm using esp32 with 32khz pwm output






#include <Nextion.h>   // https://github.com/itead/ITEADLIB_Arduino_Nextion
#include <DHT.h>       // DHT sensor library by Adafruit
#include "max6675.h"                    // Biblioteca sensor Termopar K

//INSTALAR A BIBLIOTECA EspSoftwareSerial by Dirk Kaar

//INSTALAR PLACA " esp32 por Espressif Systems" versao 2.017 para funcionat LEDC

const int ledPin = 23;  // Example: GPIO 23
const int pwmChannel = 0;
const int pwmFrequency = 32000; // 32 kHz
const int pwmResolution = 8;   // Example: 8-bit resolution



#define DHTPIN 26
#define DHTTYPE DHT22


int chave = 0;
int G = 0;
int M = 0;
int X = 0;
int N = 8;
int F = 0;
uint32_t dual_state0=0;
uint32_t cursore     = 0;
uint32_t delta    = 0;
uint32_t P = 0;
uint32_t I = 0;
uint32_t D = 0;



// INSTANCIANDO OBJETOS
DHT dht(DHTPIN, DHTTYPE);

//  Configura portas para sensor Termopar K
int ktcSO = 27; //PINO DIGITAL (SO)
int ktcCS = 14; //PINO DIGITAL (CS)
int ktcCLK = 12; //PINO DIGITAL (CLK / SCK)

MAX6675 ktc(ktcCLK, ktcCS, ktcSO); //CRIA UMA INSTÂNCIA UTILIZANDO OS PINOS (CLK, CS, SO)

//PID variaveis
float temperature_read = 0.0;
float PID_error = 0;
float previous_error = 0;
float elapsedTime, Time, timePrev;
float PID_value = 0;
float last_set_temperature = 0;

//PID constantes
//////////////////////////////////////////////////////////
int kp = 90;   int ki = 30;   int kd = 80;
//////////////////////////////////////////////////////////

int PID_p = 0;    int PID_i = 0;    int PID_d = 0;
float last_kp = 0;
float last_ki = 0;
float last_kd = 0;

int PID_values_fixed =0;


NexPage page0    = NexPage(0, 0, "page0");
NexPage page1    = NexPage(1, 0, "page1");
NexPage page2    = NexPage(2, 0, "page2");

NexDSButton bt0 = NexDSButton(0, 9, "bt0");
NexSlider h0      = NexSlider (0, 10, "h0");
NexSlider h4      = NexSlider (2, 8, "h4");
NexSlider h1      = NexSlider (2, 2, "h1");
NexSlider h2      = NexSlider (2, 4, "h2");
NexSlider h3      = NexSlider (2, 5, "h3");
NexNumber      nUmid      = NexNumber(0, 2, "n1");
NexNumber      nTemp      = NexNumber(0, 1, "n0");
NexNumber      nBlock      = NexNumber(0, 3, "n2");
NexNumber      nDew       = NexNumber(0, 7, "n3");



char buffer[100] = {0}; //buffer para armazenar dados string


void setup(){

//Nextion Sequence start
nexInit();
page0.show();

  
// inicia protocolo Serial
HardwareSerial Serial2(2);
dbSerial.begin(9600, SERIAL_8N2, 16, 17);



  dht.begin();
  delay(100);

  ledcSetup(pwmChannel, pwmFrequency, pwmResolution);
  ledcAttachPin(ledPin, pwmChannel);
  // Seta peltier como apenas saida
   


 
}

void loop() {

  //Leitura objetos nextion
bt0.getValue(&dual_state0);
chave = dual_state0;


h0.getValue(&cursore);
F = cursore;
M = F + N ;

h4.getValue(&delta);
N = delta;

h1.getValue(&P);
h2.getValue(&I);
h3.getValue(&D);

kp = P;   
ki = I;   
kd = D;

  
  static float umidade;
  static float temperatura;
  static float umidadeAnt;
  static float temperaturaAnt;
  
    umidade = dht.readHumidity();
    temperatura = dht.readTemperature();
    

    if (isnan(umidade) || isnan(temperatura)) {
      //Erro de leitura
      page1.show();

      delay(100);
     
     }
  

  
  if ((umidade != umidadeAnt)) { 
    //Atualiza os números do display
    nUmid.setValue(umidade);
  }
  umidadeAnt = umidade;

   
  if ((temperatura != temperaturaAnt)) { 
    //Atualiza os números do display
    nTemp.setValue(temperatura);
  }
  temperaturaAnt = temperatura;

  float Tempblock = ktc.readCelsius();
  float TempblockAnt;

  if ((Tempblock != TempblockAnt)) {
   nBlock.setValue(Tempblock);
  }
  TempblockAnt = Tempblock;

////////////////////////////////////
  int DewPointAnt;
  float DewPoint = dewPoint(temperatura, umidade);

  G = DewPoint;
  X = G + N;

  if ((DewPoint != DewPointAnt)) {
   nDew.setValue(DewPoint);
  }
   DewPointAnt = DewPoint ;

   //Histerese chave auto / manual
if(chave==1) 
  {
    PID_error = M - Tempblock  ;     
  }
  else if(chave==0)
  {
    PID_error = X - Tempblock ;   
  }

// Calculando valor "P" (Proporcional Delta)
 PID_p = 0.01*kp * PID_error;

// Calculando valor "I" (Integral Delta )
 PID_i = 0.01*PID_i + (ki * PID_error);
  
// Para Calcular "D" usa base tempo da diferenca de (P -I)
 timePrev = Time;                            // Armazena valor do tempo do ciclo anterior
 Time = millis();                            // Seta tempo em millisegundos
 elapsedTime = (Time - timePrev) / 1000; 

// Agora calculamos valor cheio de "D" (Derivativo Delta)
 PID_d = 0.01*kd*((PID_error - previous_error)/elapsedTime);

// O valor total final do PID é a soma de P + I + D
 PID_value = PID_p + PID_i + PID_d;

// Definimos a faixa de PWM entre 0 and 255
 if(PID_value < 0)
 {    PID_value = 0;    }
 if(PID_value > 255)  
 {    PID_value = 255;  }

// Computar valor "PID" para PWM na saida Peltier
 
 ledcWrite(pwmChannel, 255-PID_value);


//Lembre-se de armazenar o erro anterior para o próximo loop
 previous_error = PID_error;    

}
 // Calculo Dewpoint
double dewPoint(double celsius, double humidity) {
  // (1) Pressão de Vapor de Saturação = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);

   // fator -3 é para ajustar unidades - Pressão de Vapor SVP * umidade
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP/0.61078);   // temp var
  return (241.88 * T) / (17.558 - T);
}

As well as the Arduino code, please put your HMI file in a zip file and upload it to the forum.

What model Nextion are you using? What Arduino?

OK sr.

here hmi

I'm currently using the Nextion NX4832K035 display for testing.
But later I want to implement it on an Intelligent NX8048P050 display, because in addition to the larger screen, it allows for videos.

For people who can access the drop box link to @evilhf HMI file, here is a zip version.
99.zip (28.9 KB)

1 Like

#include <Nextion.h> // https://github.com/itead/ITEADLIB_Arduino_Nextion

The ITEAD Nextion.h has a poor reputation and you will not find many people to support you with it on this site.

Far more help will be available is you use the methods of Perry Bebbington
[Using Nextion displays with Arduino]
(Using Nextion displays with Arduino)

or the EasyNextionLibrary by Seithan
https://forum.arduino.cc/index.php?topic=676240.0%22

Indeed, I do not use or understand Nextion.h and will not be able to help you.

1 Like

Thanks for the tip, I'll try using this other library

I personally use the Easy Nextion Library and should be able to answer questions about the syntax and structure using that library with your processor.

But later I want to implement it on an Intelligent NX8048P050 display.

The Intelligent Series is 5v Serial i/o and will require level shifters to use with an exp32.

There have been two postings in the past year on the Arduino forum about serial output issues with intelligent series displays. My conclusion is that there is some sort of hardware issue with that model.

EDIT:
Good discussion of slider methods here

1 Like

Regarding the serial interface for the Nextion Intelligent display, I'm aware of it.
If there are any problems, I can use a bidirectional 3.3V to 5V serial converter.

By the way, I found a guy on the forum with the same problem as me, he said he managed to solve it but didn't show the solution = /.

here: Nextion - gettng value from other page - #3 by Manuel_o

And to clarify, everything works perfectly if I put all the slides on page 0.
But with the slides on page 2 (even if the page and slides are declared as global), I have no PWM on the gpi23 output.
I assume this is because the slides on page 2 contain the values for PID, and without the values for this function, there is no PWM.

I had an idea...
In the Nextion editor, I'm saving the slide values to the EEPROM using the "WEPO" command and resetting the slider value when the display is turned on with "REPO."
So, it wouldn't be possible to read the EEPROM value corresponding to the slide, instead of directly trying to read the slide value.
Does anyone have any idea how to do this?

Why not send the values to the esp32 and store them in the non volatile memory on there?

1 Like

great idea! I'll try to implement this
Thanks

For simple non volatile storage like you want, you should take a look at Preferences.h which is a part of the Arduino core for the esp32. It uses a simple key value pair method for nonvolatile storage. You will find many online examples and tutorials of how to use Preferences.h.

https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/preferences.html

For example
https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/

Also in the forum search box you can enter Preferences.h to find examples of its use.

1 Like

Thank you, that's what I was looking for, I'll look into it.

I'm going to give up. I've tried everything, but my knowledge is limited. I tried chatgpt for help without success. I'll settle for putting all the sliders on page 0, unfortunately. A rant: I'm trying to fix something that should work natively in Nextion by setting the sliders on page 1 as global. If they ever fix this, I'll try again.

Thanks to everyone who tried to help me.

Page name prefixing is not supported by the Nextion.h library. This is what the link in post 11 with a solution modifying the library describes. In my opinion, Nextion.h has a deservedly poor reputation.

1 Like

Interestingly, I wrote the code with the help of the AI for the Arduino Mega 2560, saving the sliders to its own EEPROM, and it worked perfectly. The only thing I didn't like is that when there should be a low logic level on the PWM output, when its value is 0, there are small, very fast pulses typical of overshoot. I don't know if this is a characteristic of the OCR3A.

So, I tried to adapt the code to ESP32, but I encountered several limitations. The first is that I have to use the board version (esp32 by Espressif Systems" version 2.017), it is the latest version in which the LEDC pwm library works. If you install the most current version, the LEDC simply does not respond. And finally, there are bugs in the sliders, even though it does not send any command to the sliders and only receives values from them, when initializing the circuit and displaying page 1, the sliders change their position and the number that I put to mirror the value of each slider in the nextion itself through a timer created on the page, for example: n5.val = h0.val. N5 shows unreal values of the sliders, including with strange characters, and the slider goes to the maximum by itself. Now I do not know if it is a bug of using the old library in the esp32 with nextion, anyway.... I wanted to use the esp32 because it seems faster to me compared to the arduino mega 2560, the mega takes about 10 It takes 10 seconds to display the sensor values and release the PWM output. On the ESP32, the MAX6675 displays the temperature immediately and only 2 seconds later displays the DHT temperature and humidity, which is correct because this is its internal delay. Anyway, at least I have a working version on an Arduino board...

Below is the code created with the help of AI:
ARDUINO.ino (4.7 KB)