Nextion Serial Printing Problem

Hello,

I am working on a project that has 4 different pages. With these 4 pages, I need to get the current page of the Nextion on to the Arduino Mega so it can run certain codes depending on the current page. On the 4th page I need to get the textbox information when a button is pressed.

I have got both a program for the current page and the textbox information working when they are in separate programs. I have attached the working textbox program into the zip.

The problems I get is when I try to combine the two programs together. When I do this, the current page program still works and the current page number variable I have set changes along with the variable I have set to change under those pages. But, the Arduino doesn't read the textbox anymore when the button for it is pressed.
Also, in the current page number program, I get messages such as "[1843:1,0,Keyboard] [1831:2,0,Leaderboard] [1819:3,0,Game]" printed out in the serial monitor when I change the pages with the combined program but it does not happen when it's just the textbox program.

So I would like help on figuring out why

  1. those above messages display only when the programs are combined and how to get rid of them
    2.when the programs are combined the textbox portion of the program no longer works

Thank you.

Nextion Files and Programs.zip (7.3 KB)

Is your Nextion display connected to Serial2 ?
Then you should not read it yourself.
The "nexLoop(nex_listen_list);" deals with all the serial data that the displays sends.

Remove any delay in the loop().
Use a millis() timer to limit the amount of data written to the display.

Your variable 'CurrentPage' is very important.
If you change to another page, set the variable to the new page, then set some things properly on that page (if needed), then deal with all the data, and simply ignore the buttons from other pages.

That means you need code for the administration of the pages. I have put them in separate tabs in the Arduino IDE. Each tab has an initialization function for that page and a update function that runs from the loop().

I put all the code on the Arduino and as little as possible on the Nextion. The Arduino detects the button for another page, changes a page and put that on the screen.

I should tell you that I no longer use the ITEAD Nextion Arduino library and I have never used a textbox :face_with_diagonal_mouth:

Hello thank you for responding. I would like to preface that I am very new to the Nextion stuff. And yes it is connected to Serial 2 port of my Arduino.

I am unsure by what you mean by the nexLoop comment, from my understanding this is the thing that checks the current page number of the nextion and updates it in the arduino. I don't know what else you would replace it with if it is the wrong command to use in this situation.

Again, sorry I am not very knowledgeable on this matter so I do not understand what you are saying I should do. From my understanding on what you are trying to say is that I should keep them as separate files and then call ,for example the textbox file, when I need it for the current page from the loop function of another program?

If you create an object:

NexPage page1 = NexPage(1, 0, "Keyboard");

And if you have a callback function:

void page1PushCallback(void *ptr)
{
  ...
}

And if all the objects are added to the list:

NexTouch *nex_listen_list[] = {
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  &page3,
  NULL  // String terminated
};  // End of touch event list

And the callback function is connected to the object:

page1.attachPush(page1PushCallback);

Then that list is used to call the callback function.
The function that uses that list and calls the callback function is the nexLoop().

void loop()
{
  nexLoop(nex_listen_list);  // Check for any touch event
}

If you call the nextLoop() as often as possible in the loop() and the loop() has no delay(), then everything will work smooth.

You should do everything via the callback functions and not try to read the Serial2 yourself.
Is there an example for the textbox ?

I did not tell you what to do, I told you what I did :wink:
You need a good administration anyway.
I can not test it together with your HMI file. Who is changing the page ? If you do that on the Nextion display, then how will the Arduino know that the page has changed ?

You have already a combination of the ITEAD Nextion Arduino library with your own commands if you do this:

    Serial2.print("n0.val=3");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);

You can do that with the objects as well.

The ITEAD Nextion Arduino library is not a good solution. Many don't like it. If you want something else, then you could read this: https://forum.arduino.cc/t/using-nextion-displays-with-arduino/580289

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