Unofficial Nextion/TJC User Forum

Global variables

Hello all,

Wow. Glad I found this forum before risking life and limb on Nextion’s forum.

This is my first project and it’s a simple one. Three pages; Local Weather (from a 7021), Forecast (from Accuweather), and Solar (summary of our ranch solar system). The only screen inputs are the three buttons to select the correct page and display the appropriate info from the Arduino.

All of that works fine and I even figured out how to get a background image and “transparent” text boxes using cropped image. Quite pleased. Using Helvetica Bold anti-aliased and it looks quite nice.

Now, my question. On each page in the upper corners I have date on one side and time on the other. In spite of Googling for days, I can’t figure out how to make the date/time text fields global so the info is carried to each page and continue to get date/time updates from the Arduino on that page.

I can send the info to each ..txt during the time the Arduino time updates, but that intuitively seems wrong. I have also tried setting the objnames the same on each page (t4 and t5) and I can send and update all pages with a simpler .txt. That however also seems wrong.

Is there a global solution that would let me update two global text fields (date and time) and have each page read, display and update the text fields from those two globals?

Thanks for your help and advice.

Jim

Hi Jim,

Welcome to the Nextion & TJC unofficial user forum!

How about this? In each button that changes the page, you store the date and time fields into a global placeholder right before changing pages:

globals.date.txt=t0.txt // save current date field
globals.time.txt=t1.txt // save current time field
page 0

Then on each page, define a Preinitialize Event that restores the value of the previous page again:

t0.txt=globals.date.txt  // Restore date
t1.txt=globals.time.txt  // Restore time

The trick is to create a page called globals (or any name will do really) and put 2 variable objects or text fields on it. One called ´date´ another called ´time´. You will also need to set the vscope property to global:

image

It doesn’t have to be a separate page, but it’s a good way to separate the fields from the normal pages and make the purpose clear…

Then the Arduino can keep updating t0.txt and t1.txt to display the information on the current page. When a click event changes the page, the information is carried over with the placeholder variables. Then the Arduino continous to update the fields on the new page, etc…

There are other ways to do it, but this seems easy and straightforward to get you started.

Alternatively, you could set the vscope of all the date/time fields to global. And on clicking a button, update the date/time fields on the destination page before switching…

Best regards,
/fvanroie

You don’t have to incur a press/touch event to receive data from an Arduino. I would create a date/text object on each page and just have the Arduino update each. It’s not that much code; it may seem unintuitive as you mention, but easy.

Serial.print(“p[0].b[id#ofdateobject].txt=”);
Serial.print(date);
Serial.print(""");
Serial.print(“p[0].b[id#oftimeobject].txt=”);
Serial.print(time);
Serial.print(""");

Serial.print(“p[1].b[id#ofdateobject].txt=”);
Serial.print(date);
Serial.print(""");
Serial.print(“p[1].b[id#oftimeobject].txt=”);
Serial.print(time);
Serial.print(""");

Serial.print(“p[2].b[id#ofdateobject].txt=”);
Serial.print(date);
Serial.print(""");
Serial.print(“p[2].b[id#oftimeobject].txt=”);
Serial.print(time);
Serial.print(""");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

If you did make the date/time text field objects the same ID on each page, the .b[id#ofdateobject] code could be even simpler.

Serial.print(“p[0].date.txt=”);

Thank you both fvanroie and Traxxtar for those replies. These and a reply by luma on updating a text field while the page is not active will give me enough to get past my global obsession. I do like the suggestion of loading the variables and sending one set of 0xff’s. Nice. I thank you for that.

I’ve had this 480x320 3.5" Nextion sitting on my desk for months and kept telling myself it would never work, especially after looking at the editor. I had watched a Youtuber, GreatScott!, put together a weather station with a Nextion and the fonts were horrible. I guess Nextion fixed that since the .59 editor seems to produce and display perfect anti-aliased proportional fonts.

Again, thank you all and stay well.

Another approach would be using the Nextion’s internal RTC, using a timer object updating the (made global) time and date fields on every page once a second. If the pages are consecutively indexed, a simple for() loop can do this with p[i].datefld.txt= and p[i].timefld.txt=

The internal RTC is not very accurate.

I just have a Basic connected to a Nano (the receiver side) with a 3231. My plan was to have the transmitter with an ESP32 connect to NTP, load its 3231 and send epoch to the receiver. The receiver would load its own 3231 and carry on sending date/time to the Nextion.

The transmitter has a 3231 in case we have no internet - which happens frequently in beautiful Park County, Colorado.

Time to watch anime and unravel the mysteries of time.h

Jim

That is quite right. But for civil applications, I found that syncing the RTC from the MCU once a day was largely sufficient.


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).