Page detection problem in Nextion enhanced display [solved]

i have facing problem dtecting the page in nextion display .i have used following components

  1. Uno
  2. Nextion enhanced display-NX3224K028-2.8" Touch display
    3)Potentio meter
    4)Temp sensor.

In my display have two pages page0 and page1 Respectively.
Page0 contains Numeric value of Temp sensor and send the value "0" to Arduino.
page1 contains Pot value in Gauge and Temp sensor value in Progress bar and send the value "1" to Arduino.
My motive is that , make the code like UNO will send the values to Nextion display according to the which page is selected on display.

I made one program. But i have facing the problem is page1 is not detecting that means gauge and progress bar not showing value.

Here is my code

#include <Wire.h>
#include <OneWire.h>/////////////////////Temp sensor
#include <DallasTemperature.h>///////////Temp sensor
#define ONE_WIRE_BUS A0//////////////////Temp sensor
OneWire oneWire(ONE_WIRE_BUS);//////////Temp sensor
DallasTemperature sensors(&oneWire);////Temp sensor
int sensorPin = A1;/////////////////////potentio meter
int sensorValue = 0;
int pin = 3;
int light = 13;
int current_page = 0;
float temp;
int valTemp = temp;
int Sensr;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sensors.begin();
  Wire.begin();
  pinMode(sensorPin, INPUT);
  pinMode(light, OUTPUT);
  
}

void loop() {
 
  if (Serial.available()) 
  {
    String data_from_display = "";
    delay(50);
    while (Serial.available()) 
    {
      data_from_display  = char(Serial.read());
    }
    if (data_from_display == "0")
    {
      current_page = 0;//////////////////page identified
    }
    if (data_from_display == "1") 
    {
      current_page = 1;//////////////////page identified
    }
    
  }

  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  valTemp = temp;
  sensorValue = analogRead(sensorPin);
  int Sensr = map(sensorValue, 0, 1023, 314, 730);

  if (Sensr > 359)
  {
    Sensr = Sensr - 360;
  }

  if (current_page==0)////////////////page0
  {
    String Command= "trm.txt=\""+String(temp,2)+"\"";
    Serial.print(Command);//////////////////////////////temperature sensor nuemeric value in page0
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    digitalWrite(light , LOW);
  }
  if (current_page == 1)//////////////////page1
  {
    Serial.print("eop.val=");///////potentio meter value in gauge
    Serial.print(Sensr);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.print("Temp1.val=");///////////////temperature sensor value in progress bar
    Serial.print((valTemp));
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    digitalWrite(light, HIGH);
  }
  
}

kindly help me for the solving this problem.

google is always worth a five-minute-search.

my straetgy: start with a lot of words and let's see if I have a lucky hit.
reducing words can always be done. And is done by google itself.
This was two minutes
www.google.de/search?as_q=Nextion+enhanced+display+retrieve+value+of+pagenumber

to find these links

best regards Stefan

StefanL38:
google is always worth a five-minute-search.

my straetgy: start with a lot of words and let's see if I have a lucky hit.
reducing words can always be done. And is done by google itself.
This was two minutes
Nextion enhanced display retrieve value of pagenumber - Google Search

to find these links
https://www.youtube.com/watch?v=mdkUBB60HoI

Nextion Display with Arduino - Getting Started | Random Nerd Tutorials

best regards Stefan

Now its working.. suggested video link,It was very help full. Thank you

Here is my code

#include <Nextion.h>
#include <Wire.h>
#include <OneWire.h>/////////////////////Temp sensor
#include <DallasTemperature.h>///////////Temp sensor
#define ONE_WIRE_BUS A0//////////////////Temp sensor
OneWire oneWire(ONE_WIRE_BUS);//////////Temp sensor
DallasTemperature sensors(&oneWire);////Temp sensor
int sensorPin = A1;/////////////////////potentio meter
int sensorValue = 0;
int pin = 3;
int light = 13;
int current_page = 0;
float temp;
int valTemp = temp;
int Sensr;

NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(2, 0, "page1");  // Page added as a touch event

NexTouch *nex_listen_list[] =
{

  &page0,
  &page1,

  NULL
};
void page0PushCallback(void *ptr)
{
  current_page = 0;
}
void page1PushCallback(void *ptr)
{
  current_page = 1;
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sensors.begin();
  Wire.begin();
  pinMode(sensorPin, INPUT);
  pinMode(light, OUTPUT);
  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
}

void loop() {

  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  valTemp = temp;
  sensorValue = analogRead(sensorPin);
  int Sensr = map(sensorValue, 0, 1023, 314, 730);

  if (Sensr > 359)
  {
    Sensr = Sensr - 360;
  }

  if (current_page == 0) ////////////////page0
  {
    String Command = "trm.txt=\"" + String(temp, 2) + "\"";
    Serial.print(Command);//////////////////////////////temperature sensor nuemeric value in page0
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    digitalWrite(light , LOW);
  }
  if (current_page == 1)//////////////////page1
  {
    Serial.print("eop.val=");///////potentio meter value in gauge
    Serial.print(Sensr);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.print("Temp1.val=");///////////////temperature sensor value in progress bar
    Serial.print((valTemp));
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    digitalWrite(light, HIGH);
  }
  nexLoop(nex_listen_list);
}

Happy to help.

karma for posting your code
maybe you like to change your title to include [SOLVED] to indicate here was a question and it has a solution.

best regards Stefan

That's better :smiley:

I use a macro for simple command: Questions about Nextion display - #4 by Koepel - Networking, Protocols, and Devices - Arduino Forum.

You could request the temperatures every 2 seconds and update it to the display every 2 seconds with a millis() timer.

Sometimes a new page on the display requires to run code to set things right for that new page. Then you have do that in the Callback functions. I have a initialization functions for every page.
Do you switch to a new page on the Nextion display itself ? I capture pressing the button and I send to command to show a new page. I prefer to do everything from the Arduino.

The Nextion library is not very good. It has delays in it and it parses the incoming data not very efficient. It is a lot of work, but it is possible to read the data from the display in the sketch.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.