I have recently bought a Nextion Enhanced 3.5" display and have been doing some initial testing/learning ready for starting a project.
I am not an experienced programmer but manage to fumble my way through but this issue has cost me 2 days with no results! I have read just about every web page I can find on the topic and have run around in circles.
I have 3 pages in Nextion (attached) and I want to send the current page number to Arduino. My project Arduino code will be controlled by the Nextion page that is displaying (the Nextion can change pages independently of the Arduino hence the need for Arduino to know what page is displaying).
I have followed example in the standard Nextion library (GitHub - itead/ITEADLIB_Arduino_Nextion - CompPage_v0_32.ino).
I have both a push and a pop event attached to page 0 that upon operating causes the variable CurrentPage to change to the page number i.e. 0. This then prints in the debugger. As a further visual prompt I have tried to change the text in Select1.txt_Stage1 to something.
As it stands neither the CurrentPage variable is changing nor the text box updating.
I have tried changing the Nextion preinitialisation event to "sendme" or to "printh 66 01 FF FF FF" but neither is recognised. I also tried checking the "Send Component ID" in the Touch Press/Release events. I have been very thorough in trying things before reverting to here...
#include "Nextion.h"
int CurrentPage = 9; // Create a variable to store which page is currently loaded
// declare 1 text box on each page for testing purposes
NexText txt_stage1 = NexText(0, 2, "txt_stage1");
NexText txt_stage11 = NexText(1, 2, "txt_stage11");
NexText txt_StageSel = NexText(2, 2, "txt_StageSel");
// Declare pages:
NexPage Select1 = NexPage(0, 0, "Select1"); // Page added as a touch event ** this nexpage uses the actual page name as I wanted to see if that made a difference
NexPage page1 = NexPage(1, 0, "Select2"); // Page added as a touch event
NexPage page2 = NexPage(2, 0, "NoRecord"); // Page added as a touch event
char buffer[100] = {0}; // copied from other code but it does not do anything yet
// Declare touch event objects to the touch event list: not sure if you need to listen to a text box? Prob not but put there just incase
NexTouch *nex_listen_list[] =
{
&Select1, // Page added as a touch event
&page1, // Page added as a touch event
&page2, // Page added as a touch event
&txt_stage1,
NULL // String terminated
}; // End of touch event list
// Page change event:
// both push and pop included for page 0 as I cannot work out which is needed
void Select1PopCallback(void *ptr) // If page 0 is loaded on the display, the following is going to execute:
{
CurrentPage = 0; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
txt_stage1.setText("hello1");
dbSerialPrintln("Select1PopCallback");
} // End of press event
void Select1PushCallback(void *ptr) // If page 0 is loaded on the display, the following is going to execute:
{
CurrentPage = 0; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
txt_stage1.setText("hello2");
dbSerialPrintln("Select1PushCallback");
} // End of press event
// Page change event:
void page1PopCallback(void *ptr) // If page 1 is loaded on the display, the following is going to execute:
{
CurrentPage = 1; // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
} // End of press event
// Page change event:
void page2PopCallback(void *ptr) // If page 2 is loaded on the display, the following is going to execute:
{
CurrentPage = 2; // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}
// End of press event
void setup() { // Put your setup code here, to run once:
//Serial.begin(9600);
//nexSerial.begin(9600); nexInit does this hence commented out
nexInit();
dbSerial.println("setup start"); // debug
// Register the event callback functions of each touch event:
// You need to register press events and release events seperatly.
// Format for press events: <object name>.attachPush(<object name>PushCallback);
// Format for release events: <object name>.attachPop(<object name>PopCallback);
Select1.attachPop(Select1PopCallback); // Page press event
Select1.attachPush(Select1PushCallback);
page1.attachPop(page1PopCallback); // Page press event
page2.attachPop(page2PopCallback); // Page press event
// End of registering the event callback functions
dbSerial.println("setup end"); // debug
}
void loop() { // Put your main code here, to run repeatedly:
nexLoop(nex_listen_list);
dbSerial.println(CurrentPage); // debug
}