Start a new topic

create nextion component from arduino code

how to create nextion component, example: create button from arduino code, directly?? thx


One function of the Nextion Editor (Windows based software program) is to design the HMI.  The other two functions of the Nextion Editor, one is to compile the design into machine code and the other transfer this compiled machine code to the Flash storage on the Nextion Display.  When running, the Nextion Display will grab 12-byte chunks from the Flash as needed and process, and as such the HMI functions.


If you are already writing for Arduino, then is that Arduino design also flexible and on the fly as well, or are you committing to a static version of your Arduino code. If it is static, you might consider furthering the original HMI design so that you can avoid the need to create the component on the fly. If you know for certain which components and how many of each might be needed to be dynamically created, you could possibly pre-create them in your original HMI design and then merely hide them from the view of the user.


When you need to create the illusion of "made on the fly", you simply expose your pre-created component, set its variables, set its to visible to true, and refresh the component. In this manner, the compiled code and logic in the touch/release/preinitalize and postinitial events are already compiled and exist somewhere in the your HMI file already on the Flash. You will not have any ability to change the logic coding in the events, but you do get the appearance of on the fly creation.


To accomplish this, create within your page your "hidden component" h0, remember to pre-set as many of the component attributes that wouldn't change, vscope, sta, width, height, x,y screen placements and txt-maxl length. In the page preinitialization event you want to include vis h0,0. Something triggers the need for your hidden component to magically appear - you can probably capture that in sendme and sendxy data being returned from the Nextion over serial.


When you have realized your trigger, in Arduino, you can now send the following commands


h0.bco=2015

h0.pco=63500

h0.font=0

h0.xcen=1

h0.ycen=0

h0.txt="Text to Display"

vis h0,1

ref h0


It is important to note that the x,y,w and h of the components are not adjustable while HMI is running.


To actually create a component on the fly via the Arduino, one would have to create their desired component, assign the component attributes, write, check and correct for any errors in the pre-initialize, post-initialize, touch-event, release-event routines, insert the new component into an existing copy of the HMI design on the Arduino, re-compile the merged copy of HMI and then transfer the newly compiled copy over the much slower serial connection and store this in an unused portion the Flash on the Nextion Display.


So basically create a non visual Arduino version compiler.  The tricky part will be switching from the new HMI and the currently running HMI without alerting or disrupting the user experience on the HMI.  The user data collected so far, stacks, states, and values from the in-use HMI will have to be captured and stored so they can be applied to the new HMI, and then get the internal Nextion MCU to change the program and stack pointers to the new HMI.  You would also need to consider clearing the original HMI from Flash and relocating the new and now currently in-use HMI lower in Flash so the next iteration of a dynamically created HMI will also have a portion of unused Flash so it too can be stored.


Possibly you could accomplish this during known intervals when user interactions are known to be highly unlikely as in during the over night for an environment of daytime users. I certainly would call your undertaking nothing less than ambitious to create a port for Arduino.  Seriously it could be possible.



I am now reviewing all of the Feature Requests, this will take some time, patience please.

With minimal variable components, there are also plenty of gui commands to create a component.

This post has been noted for the overall report.

Login or Signup to post a comment