Start a new topic

RTC and Nextion Enhanced

 Sorry if this has been answered elsewhere without me able to find it via search.


I have played a bit with the RTC on the Nextion 7".


From samples and reading here I managed to get Date, Time and Day to display in txt boxes t0 - t2.


I know this code might not be the best approach, but for getting a novice like myself started, it works well.

The attached code below is in the p0 Pre-initialise event box.


My question is mainly relating to the time component.

Once I open the page, Date, Time and day display correctly.

How do I get the clock to run for example increment every second or every 5 seconds as example?


When I navigate away from page, and back again, the imer has progressed and displays current time etc correct.


Shoud I stay on this page for an hour, It basically means my clock is one hour behind (Visually), although in the background still correct.


Once page navigated away and back, time is current again.


Anybody that can assist me with what/How to timer event on page, and where I need to insert this?

 

// get Day
cov rtc2,va0.txt,0
t0.txt=va0.txt
//
//  get Month
if(rtc1==1)
{
  t0.txt=t0.txt+" JANUARY"
}
if(rtc1==2)
{
  t0.txt=t0.txt+" FEBRUARY"
}
if(rtc1==3)
{
  t0.txt=t0.txt+" MARCH"
}
if(rtc1==4)
{
  t0.txt=t0.txt+" APRIL"
}
if(rtc1==5)
{
  t0.txt=t0.txt+" MAY"
}
if(rtc1==6)
{
  t0.txt=t0.txt+" JUNE"
}
if(rtc1==7)
{
  t0.txt=t0.txt+" JULY"
}
if(rtc1==8)
{
  t0.txt=t0.txt+" AUGUST"
}
if(rtc1==9)
{
  t0.txt=t0.txt+" SEPTEMBER"
}
if(rtc1==10)
{
  t0.txt=t0.txt+" OCTOBER"
}
if(rtc1==11)
{
  t0.txt=t0.txt+" NOVEMBER"
}
if(rtc1==12)
{
  t0.txt=t0.txt+" DECEMBER"
}
//
//  get Year
cov rtc0,va0.txt,0
t0.txt=t0.txt+" "+va0.txt
//
// get Hour
cov rtc3,va1.txt,0
t1.txt=t1.txt+" "+va1.txt
//
// get Min
if(rtc4<10)
{
  cov rtc4,va1.txt,0
  t1.txt=t1.txt+":0"+va1.txt
}else
{
  cov rtc4,va1.txt,0
  t1.txt=t1.txt+":"+va1.txt
}
//
// get Sec
if(rtc5<10)
{
  cov rtc5,va1.txt,0
  t1.txt=t1.txt+":0"+va1.txt
}else
{
  cov rtc5,va1.txt,0
  t1.txt=t1.txt+":"+va1.txt
}
//
// get DayofWeek
cov rtc6,va2.txt,0
t2.txt=va2.txt
//
if(rtc6==1)
{
  t2.txt="Monday"
}
if(rtc6==2)
{
  t2.txt="Tuesday"
}
if(rtc6==3)
{
  t2.txt="Wednesday"
}
if(rtc6==4)
{
  t2.txt="Thursday"
}
if(rtc6==5)
{
  t2.txt="Friday"
}
if(rtc6==6)
{
  t2.txt="Saturday"
}
if(rtc6==7)
{
  t2.txt="Sunday"
}

 

JqsQwlkc_Jd9_2cjM-h60TLTqyafVbbotw.JPG



Is your Nextion 7" an Enhanced Model?  

If it is it has an actually RealTimeClock (RTC) on the backside.

It is the RTC that is keeping time.


Your request to retrieve the time from the RTC and display to the screen in intervals is up to you.

On the page displaying the time (or every page if that is the case) you would place a Timer component

You would place your code in the Timer Event (or in each timer event on all pages).

You would set the .tim attribute of the Timer components to 1000 or 5000 for 1s or 5s as desired.


There would be no need for any RTC related code in the Pre/PostInitialization Event


Patrick
Thank you so much for the response.

 I really appreciate the time taken to respond and assist newbies like myself. From browsing the forum, One can quickly see who the "assisting guru's" are.


Yes I have the 7" Enhanced, and calling the RTC.


I have moved my code portion from the page Pre-initialization to the timer event created, thanks.


One more simple question, the refresh now obviously work, yet the following:


In t2 that display the time, it leaves the time as displayed in the txt field there, and rather that updating it, it adds to the end of the initial txt.

Eg: 19:53:10 19:53:11 19:53:12 etc etc until the field is full.


I obviously need to add a clear txt code line in there somwhere.


Any suggestions?


Regards

Riaan


 

 

//  Note that va0, va1 and va2 are String Variables
//  Note that the last number in cov is how many places
//     cov number,text,0 .. gives no leading zeros but
//     cov number,text,2 .. gives a leading zero if under 10
//  Note that when combining into t0, t0 can not be one of them
//      (or atleast without causing problems)
//
//
// get Day and store in String Variable va0 with 2 digits
cov rtc2,va0.txt,2
//
// get Year and store in String Variable va2 with 4 digits
cov rtc0,va2.txt,4
//
//  get Month from RTC and combine Day Month Year into t0
//       we do this in one shot using our va0 and va2
if(rtc1==1)
{
  t0.txt=va0.txt+" JANUARY "+va2.txt
}
if(rtc1==2)
{
  t0.txt=va0.txt+" FEBRUARY "+va2.txt
}
if(rtc1==3)
{
  t0.txt=va0.txt+" MARCH "+va2.txt
}
if(rtc1==4)
{
  t0.txt=va0.txt+" APRIL "+va2.txt
}
if(rtc1==5)
{
  t0.txt=va0.txt+" MAY "+va2.txt
}
if(rtc1==6)
{
  t0.txt=va0.txt+" JUNE "+va2.txt
}
if(rtc1==7)
{
  t0.txt=va0.txt+" JULY "+va2.txt
}
if(rtc1==8)
{
  t0.txt=va0.txt+" AUGUST "+va2.txt
}
if(rtc1==9)
{
  t0.txt=va0.txt+" SEPTEMBER "+va2.txt
}
if(rtc1==10)
{
  t0.txt=va0.txt+" OCTOBER "+va2.txt
}
if(rtc1==11)
{
  t0.txt=va0.txt+" NOVEMBER "+va2.txt
}
if(rtc1==12)
{
  t0.txt=va0.txt+" DECEMBER "+va2.txt
}
//
//
//  With Day Month Year completed
//      we can reuse va0 and va2
//
// get Hour and store in String Variable va0 with 2 digits
cov rtc3,va0.txt,2
//
// get Min and store in String Variable va1 with 2 digits
cov rtc4,va1.txt,2
//
// get Sec and store in String Variable va2 with 2 digits
cov rtc5,va2.txt,2
//
// Combine Hour Min Sec and store in t1 in one shot
t1.val=va0.txt+":"+va1.txt+":"+va2.txt
//
//
//  and finally deal with DayofWeek
//
// get DayofWeek from RTC and store in directly into t2
//      note that Sunday is 0 and not 7
//
if(rtc6==0)   {
  t2.txt="Sunday"
}
if(rtc6==1)
{
  t2.txt="Monday"
}
if(rtc6==2)
{
  t2.txt="Tuesday"
}
if(rtc6==3)
{
  t2.txt="Wednesday"
}
if(rtc6==4)
{
  t2.txt="Thursday"
}
if(rtc6==5)
{
  t2.txt="Friday"
}
if(rtc6==6)
{
  t2.txt="Saturday"
}

 

 


Thank you SOOO much Patrick
Appreciated. The //Comments guiding me throug the process helped tremendously.
I now does exactly what I needed it to do.
Next is simply to sync the time and date on the 7" Enhanced itself now.
I do not have a USB Serial connection with it at the moment, but will most likely procure one tommorrow.
I will first just scan through the forum again, as I recall seeing a thread regarding sync without serial cable, but from Arduino or some other commands.
Once again Thank You so much for taking the time to assist.

Regards
Riaan

 

The more advanced method requires use of the b[.id] component Array.

I like this method as the HMI can then be adjusted on the fly for various languages

- but one has to be extremely careful with the ordering of the components

The Nextion Editor has two arrows on the toolbar to assist in reordering

- Arrow up Bring Top - sets the component .id to the last and highest component .id

- Arrow down Bring Bottom - sets the component .id to the first and lowest id of 1

   (The Page component always holds the id of 0)


Global Variables for Month and Weekday name abbreviations

So if I add va0 .. va11 as text components .txt-maxl of 3 to hold month name abbreviations

- set their ids 1 to 12 respectively, set .objname to mo1 through mo12 respectively

then add va12..va18 as Text Components .txt-maxl of 3 to hold weekday abbreviations

- set their ids 13 to 19 respectively, set .objname to wd0 through wd6 respectively


Time Display Components on each page needing to show the time

Add Number Components Year(n1), Day(n0), Hour(n2), Minute(n3), Seconds(n4)

- set year to .lenth to 4, and the day/hour/minute/seconds .lenth to 2

Add Text Components Month(mo) and DayofWeek(wd)

- set .txt-maxl to 3

Add Text Components for spacers and : time seperators

And finally add a timer on each page with the Timer Event of

n0.val=rtc2

n1.val=rtc0

n2.val=rtc3

n3.val=rtc4

n4.val=rtc5

wd.txt=page0.b[13+rtc6].txt

mo.txt=page0.b[rtc1].txt


Now with this, I quickly get the time date displayed on each page I want with just 1 timer and the code.

I can set the default language month and weekdays abbreviations in page0 PreInitialize Event

But I can also send language changes over serial to set mo1 to mo12 and wd0 to wd6 and not change the HMI


Page2 will allow the user to adjust the RTC, each change being immediate.

- (It needs a few extra checks and more code to do "wrapping" ... but it functions)




HMI
(80 KB)
Hell, thanks a lot Patrick.
Sorry for the delayed response, believe there is som time difference in our locations.
I thank you once again for your time and effort.
I managed to build  seperate "Settings" page quickly following the ideas you noted.
Never thought it would be as easy as button changing value of rtc x +1.
Took a couple of shortcuts at first copying my strings to the setting page, but soon found out that the buttons mis-align seriously betwee something like April and November :-)

Thanks again
Regards
Riaan

 

tvzOuvWO2e36zxr4lZFdFnYS6x1Htmu2Nw.JPG


4BM6IdXz2aCPBcS12XYdJ0FB3j4ph7RpDg.JPG


 

I started an application where i want to use a RTC.
It is a 7" enhanced and with a battery in the back.
I follow the example here, but i cannot display the text  part, meaning the month and day of the week.
I attached the project and maybe somebody can give me a suggestion where to look to correct my mistake.
Thank you

 

HMI
(1.71 MB)

Asking hobbyist users to debug code for a commercial project

   for free is perhaps in the wrong manner.

Finding typos and where you didn't follow clearly posted instructions

   shouldn't be past onto forum users and expect that they perform

   basic code corrections for any commercial project.

When an advanced method is not understood

   - perhaps use a more basic approach, or send from MCU side code.


Dumping code in forum for others ... ahhh

   uploading on their request ... would be different.


Nextion forum is not free source of labour to cut development costs.


1 person likes this
Login or Signup to post a comment