Start a new topic

Arduino and Nextion Display

 Hi every one.
I got a nextion display recently to learn and it work and create projects with it, I was wondering how can I control the page shown on the display from arduino, meaning that if there is page 0, page 1, and page 2, how can I switch between them from an arduino input like a sensor or a switch.
I tried but I does not work, I do not know why, can any body help me with that, including the library link you are using for that, because I there might be a different library for nextion.

From within the Nextion Editor

  Help menuitem of the Help menu

   - launches Nextion HMI Solutions page

First link of Related Downloads

  Itead's Arduino Library for Nextion 

examples folder contains CompPage.ino

 - uses Release Event of Page

   Send Component ID checked in HMI designed file to trigger if viewed in NexPage.h and NexPage.cpp

  basically parses out to serially send page page0ÿÿÿ

See example code, see library code compare 
Must declared properly
  NexPage page0 = NexPage(0, 0, "page0");

well I downloaded the library. 
once I try uploading an example, I get error compiling regarding dbserial : dbSerialPrintln("setup begin");

I think it has to do with serail 1 and 2, because I am using Arduino Uno which has only serial 1.
I do not know what to do about that either 

Consult the Uno documentation.  Consult the library files.

Perhaps the instructions in the library (text file)

   may shed light on how to configure for Uno.

Certainly when serials are not configured correctly

  one can indeed see failures as an expected behaviour.

Examine file named NexConfig.h

When Uno has only one serial it cannot be used for two purposes

   so dbserial to Serial Monitor will need to be disabled.

nexSerial will need to be set to the correct Serial as per MCU.

Thank you for the help I re configured the file, and the function is working easily.
just right now the other function I was using " Dim" for brightness stopped working because it uses another version of the library.  


  //Dim display.Power save.

    int bright = map(15, 0, 1024, 0, 100);

  String dim = "dim=" + String(bright);



is there any other function for dimming display than this one ? 

Why is there another function needed?

the Iteadlib library's sendCommand is able to send 

how ? 
when I use :  Nextion.sendCommand(dim.c_str());
it give me an error compiling that : 

exit status 1

'Nextion' was not declared in this scope

Examine the code as told earlier.

Even in NexPage.cpp you'll see how to use sendCommand.

Forget myNextion and just use the Iteadlib library

Examine the code

  I use code attached from Nextion Arduino example and the only thing that happens, is my relay 1 turns on by itself and does not turn off.  I have a MEGA 2560 connected at TX2/RX2. I use an 8 channel relay module(thought that may be a problem),so i used a 1 channel relay module. I get same result.

  I remember this being my problem in the beginning which sent me on a 2 month reading and writing athon to come back full circle with the same relay doing the same thing. 

  The only thing I change in the code is the bit of code posted below with the buttons. 


NexButton b0 = NexButton(1, 1, "b0");

NexButton b1 = NexButton(1, 2, "b1");

NexButton b2 = NexButton(1, 3, "b2");

NexButton b3 = NexButton(1, 4, "b3");

my page 0 is a title page with page button to go to page 1 with menu buttons and i am trying to get those buttons to turn on relays. I can not see anything I am doing wrong because I have yet to do anything right to see what is wrong.

  I am extremely happy with the way my HMI looks and want to spend more time improving it, but I am stuck trying to get it to work with my Arduino.  (if) i can even figure out how to get it to work, i know my next issue will be formatting other code examples to fit within my buttons. But, I believe if I can get passed this issue, I can have a better understanding of how it all may fit and work together.  

Again I appreciate any time and assistance for help trying to figure this out.  

Thank you 


O got it to work. 
sending serial data dim=.... followed by 0xff 0xff 0xff 
thanks again for the help 

Follow up on question above:

I have opened Serial Monitor on Arduino side and get message:

recvRetCommandFinished ok

again the only thing i am changing in the code is the declare NexButton values, in which i have left it the same and changed it to match my HMI file. my NexButton component id does not change 

I have done both for page id...0 and 1. I see in my Nextion editor, all page id;s show 0, but when i push NexButton in debug i see the value page id is changed with corresponding NexButton on that page. 

My problem is:  My relay will not turn on.

I believe i am doing everything correct.  I double checked all my connections and grounds and code and even checked the communication between Nextion and Arduino.  It all looks good. there anything i could be missing or not seeing?  I do not know. Could use some assistance with this pls.

Thank you

"I believe i am doing everything correct." ....

when all done correctly, all would work as expected ... :-)


from prior posted code

 - remove #include "NexButton.h" and "NexText.h"

 - replace with #include "Nextion.h"

All page's id is 0 for their component id

Page number is gray left of pagename in Nextion Editor

On the wrong page, it will not accept

as defined in code above posted prior

   b0,b1, b2, b3 deal with page number 0 

If on page number 1 ... attachpop will have no effect.

Why compare button's text on Nextion as to whether relay should be on or off?

 I have a question about-

if (strcmp(buffer, "ON")

Is it reading the actual text within the b0 button?


is that just how Nextion reads to say that the b0 button has been pressed?

Login or Signup to post a comment