In this video, I will show how to have two separate passcodes. One for a general user that allows access to a few pages. One for an admin user that allows access to more administrative pages. Using a Nextion to control locks or relays, you could have a page to add users and even schedule access.
Some of my videos translate to blog posts better than others. I am not sure how to describe this video in a post. I will cover the main points that I used. This should help you take what I reviewed in the video and apply the concepts to other projects.
I focused on three different aspects of the display to create the video.
- The “vis” command to show and hide an object on the display
- In the “program.s tab” I created two variables that were used across the pages
- EEprom was used to store the password during power down
- I showed how to treat a page reload differently than entering it the first time.
THE “vis” command
The “vis” command hides or displays an object on the screen. The nice part about this command is that it will work in runtime. You can hide and display objects based on variable or button states. The applications for this command are vast. The structure for “vis” is the command followed by the object name, a comma, and a zero or one. Using a zero hides the object while using a one will display the object
- COMMAND “vis t1,0” – will hide textbox 1
- COMMAND “vis n5,1” – will display number field 5
- A variable “vis b1,va1.val” – will display or hide button 1 if va1.val is 1 or 0 respectively
- A dual state button, “vis t3,bt0.val” will make textbox 3 dependent on button 0
As you can see, the vis command can be used in many different ways. I use it along with a variable that I placed in the program.s tab. I will cover that next.
Program.s Tab
The program.s tab is a place to create system variables. The difference between a system variable and a regular global variable is the extension preceding the variable name. A system variable can only be an integer. You do not need the “.val” after it. You will get an error if you try. They can be very convenient once you start using them.
The “int” is used in the second line of the file. The first line is a comment. Essentially, the int is defined in the first line of code. You can add more integers to the same line using a command to separate the different variables. Below is an example.
I added the variable “cc” and the variable “flutterbird” after the standard sys variables. This is a convenient way to add variables, but I prefer to separate my variables from the Nextion-defined ones. You can add them all on another line or have each one on a separate line. If you place each variable on its own line, you can add a comment to help identify them as you build your application. Below are two images showing multiple variables on a single line and multiple lines for single variables.
I used the same variables for all three images. You will get an error if you create a new integer variable after the baud or any other code in the program.s tab. The image below illustrates this error.
The error message is in read and points out the integer needs to be declared before the code.
BUILT-IN EEPROM
The Nextion displays have built-in EEPROM. The Itead website states that both the Intelligent and the Enhanced displays contain 1024 bytes of EEPROM. The other models do not have any EEPROM. There is a designated limit to the number of reads/writes that Nextion recommends for their displays. With those recommendations in mind, I build projects that use EEPROM to use limited access. If an application needed a lot of reading and writing, you could use an SD card and Filestream instead. That is a discussion for another day.
The two commands used when accessing the EEPROM are REPO and WEPO. REPO reads data, and WEPO writes data. They both use a similar command structure. The value or text that you want to write and the location you want to write to. You have to be careful that you do not overwrite existing data. I will provide a video on reading and writing if you want to see it below.
A second video shows what happens if you don’t pay attention to where you write the data in EEPROM.
RELOADING PAGES
When you are on the admin page, you can adjust the passcode. I use the built-in keypad to accomplish this. If you are on the user page and you select the admin page, I have the password fields populated from EEPROM. When you select a field to populate with the Nextion keypad, the page will refresh after you press “OK.” This refresh will result in the field reverting back to the value stored in EEPROM instead of the value you entered into the keypad. I handle this problem by adding a “currentPage” variable to the program.s tab.
After the post-initialization code runs, I set the currentPage value to the currently selected page. You can test for the value stored, and if it matches the current page, you know that you have refreshed the page and did not come from another page. Once you test for the value, you can add an if statement to skip reading the EEPROM if the currentPage variable matches the actual current page. This concept is more apparent in the video.
If you have any questions about this video or blog post, please go to the help page and submit a question. Let me know if you notice any errors on this page so I can fix this post.