i have facing problem dtecting the page in nextion display .i have used following components
Uno
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);
}
}
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.