** Function begin();**
unsigned long: unsigned long baud = 9600 (default) if nothing is written in the begin() function
The begin() method of the class in which we pass the initialization data to the objects.
myObject.begin(115200); // for baud rate 115200
Associated Library’s Code Example: ReadAndWriteNumber
String = objectname.numericAttribute (example: “n0.val” or “n0.bco”…etc)
uint32_t = value (example: 84)(number from 0 to 4,294,967,295)
For writing numbers in numeric attributes in components of any kind (variables, textboxes, etc.).
- We can change the shown value of a number box
- We can change the value of a numeric attribute of the design editing commands. Those commands are used to change the attribute of the components (button, text box, timer, numeric etc.) while Nextion is running.
||Result on n0 comp.
||Sets n0 component’s shown value to 30
||Sets background color to red
||Sets font to font style with Id = 0
||Sets font color to blue
||Sets value format to decimal
With the same way you can change the xcen, ycen, length and isbr
TIP: In a timer component, at the attribute < en >, you can start or stop the timer by writing 0 or 1
Also, at the attribute < tim >, you can set the time the timer is going to repeat the commands written in timer’s User-code event.
myObject.writeNum("tm0.en", 0); // 0 = stop, 1 = start
myObject.writeNum("tm0.tim", 1000); // repeat code every 1000ms
myObject.writeNum("n0.val", 30); // Set the value of numeric n0 to 30
myObject.writeNum("n0.bco", 63488); // Set background color of n0 to 63488(red)
myObject.writeNum("n0.font", 0); // Set font to font style with ID 0
myObject.writeNum("n0.pco", 1055); // Set font color to blue
myObject.writeNum("n0.format", 0); // Set value format to decimal
NOTE: Only attributes shown in green in the Editor can be both read and changed by user code at runtime.
Send floating-point numbers, a number that has a decimal point.
Nextion DOES NOT SUPPORT float numbers. Instead, it uses integer math and does not have real or floating support.
The Xfloat component is used for signed 32-bit integer values.
.vvs0 sets the number of digits shown to the left of the decimal (useful for leading zeros).
.vvs1 sets the number of digits shown to the right of the decimal.
You can send Floating-point numbers with these two ways:
*** On a Xfloat component after you convert the float to int and multiply it by 10, the Xfloat component will put a comma
, (decimal point) automatically after the last digit if attribute
vvs1 is set to 1.
int tempInt = temperature*10; // Convert the float to int. Multiply it x10
myNex.writeNum("x0.val", tempInt); // Write it to x0 Xfloat component on Nextion
it is obvious that if you want 2 decimal places, you will set the attribute
2 and you will multiply by
*** On a Textbox component after you convert the float value to String
String tempString = String(temperature, 1); // Convert the float value to String, with 1 decimal place
myNex.writeStr("t0.txt", tempString); // Write the String value to t0 Textbox component
Associated Library’s Code Example: WriteTextAndCommands
String). To change the
.txt attribute in components
String No1: objectname.textAttribute (example: “t0.txt” or “b0.txt”)
String No2: value (example: “Hello World”)
String). To send Designing and System Commands
String: The command to send on Nextion
For writing text in Nextion. The two ways the command can be used:
First Usage of the command:
String) to change the text in a textbox
Use both parameters to write text in textboxes.
- In the first parameter, write the objectName.textAttribute example: t0.txt or b0.txt
- In the second parameter, write the text you want to “print”
myObject.writeStr("t0.txt", "Hello World"); // Change t0 text to "Hello World"
Any previous text on the textbox is deleted
Avoid using very big text Strings in the same command, as Nextion will not recognise them. Istead use a second command and in order to add to the existing text, use the **
+**symbol, after the
myObject.writeStr("t0.txt", "You are now transferred to page2\rThank you\rfor choosing my library!!!");
myObject.writeStr("t0.txt+", "\rEnjoy the library!!!");
myObject.writeStr("t0.txt+", "\r\rAthanasios Seitanis\firstname.lastname@example.org");
/* By writing \r, you send Nextion the change line character < r >
* The second is required, in order to print the as character
* and not as an escape character.
Second Usage of the command:
String) to send Designing and System Commands according to the instructions of Nextion’s Instruction Set.
Use only the first parameter to send a command to Nextion. The commands must be syntaxed according to Nextion’s Instruction Set.
Design Commands Example:
myObject.writeStr("page page0"); // Sending this command to change the page we are on Nextion using pageName
myObject.writeStr("page 1"); // Sending this command to change the page we are on Nextion using pageId
myObject.writeStr("cir 100,100,30,RED"); // Renders a hollow Red circle with circle center at (100,100)
System Commands Example:
myObject.writeStr("dim=50"); // Set the current brightness level to 50%
myObject.writeStr("sleep=1"); // sleep=1 (Enter sleep mode) or sleep=0 (Exit sleep mode)
NOTE: When you decide to exit the sleep mode on Nextion, use a small delay of 50ms, in order to give Nextion some time to Exit sleep mode.
WARNING: DO NOT send commands that causes data return over Serial to MCU, unless you know what you are doing.
Commands that cause data return over serial:
TIP: Write in the debug mode the command to check if it is written correctly