I hope this does not display my learning curve.
Can a sketch make a request via the USB port to obtain the PC's Date and time and then use it to update the Arduino's RTC? I do not know whether that would be via the IDE or the PC directly.
None of the Arduino have any inbuilt rtc that i am aware of.
The ds3231 as an add on can be set via the pc and then the Arduino is reprogrammed with the time set disabled.
Again as far as I am aware.
I am aware of the Time Set at time of compiling the sketch. The RTC would be an add on (either I2C or SPI).
Read the spec perhaps on whatever rtc you decide to use. Some may be diffrerent from the ds3231 I referred to.
The answer to your question is surely: yes.
Your next question will be all about writing the programme on the PC that constantly provides the required data on its USB port.
After that you might spend some time reflecting on whether it's worth the effort.
If you really need something like this, you might be better off ditching Arduino for an ESP and getting regular time updates directly over the Internet. No RTC needed - and no PC either.
My thought was to have either an automatic or a manual request every week/month. I presume that functionality does not yet exist in the IDE, short of reloading the entire sketch. Should it?
I set up something like that a while back in Windows. It was a .vbs script that called Nettime to update the computer's time from NTP, then send timestamp data to the Termite terminal program, which has to be running, and not minimized. Then Termite would send it to the COM port over which it was connected to the Arduino. Actually, it could have been the serial monitor instead of Termite under IDE v1.8, which invoked the serial monitor as a separate task under Windows. But in v2 the serial monitor is just a window in the IDE, so you need a terminal program.
The script made use of the SendKeys and TimeStamp functions of Windows scripting.
But it didn't work well because when you do an NTP update in Windows, it doesn't immediately change to the correct time. It averages its way over a period of minutes.
So I think the bottom line is that it's best to have the microcontroller do the NTP update if that's possible. That would be an ESP8266 or ESP32. Or, you could calibrate a DS3231SN RTC so that it only gains or loses a few seconds a year, and just go with that. I do the calibration for each RTC module using the PPS output of a GPS module.
Would I be correct in saying that the IDE acts like a "terminal Program" ?
Would it be nice to expand it's capabilities to perform such actions as
manually updating RTCs and down loading data to a txt or csv file on the PC?
- As mentioned, a DS3231 might be your best option only ~$3.00.
The IDE does indeed perform like a terminal program, but so far as I know neither v1.8 nor v2 has any provision to respond to a request from an Arduino for the current system time. But to be fair, I don't know of any terminal software that has that function built in.
Did you look into the TimeLib library?
It has an example for working with a serial port:
I understand, good info. I was suggesting a send from IDE to the Arduino. The Arduino would not be doing the request but rather reacting to it .
Could this be a logical upgrade to IDE???
excellent lead. ![]()
I presume that this would run in maybe windows terminal (power shell)???
What language is this? Will it run in windows terminal?
Sorry, I have never done this before.
Thanks Barry
That code is an arduino sketch, but it is expecting the computer to be running code that listens for an 0x07 request and responds with a "T1741484503"-like timestamp. There is a Linux "date +T%s%n" command that produces that message in the comments, and also a "processing" language example
included with the library:
You could write the computer-side listener/replier in whatever language suits you.
On the Windows side, I think this .VBS script works with the IDE v1.8 serial monitor to send the current date/time to the Arduino. It just sends the current timestamp to the keyboard input of the serial montitor. The serial monitor must be running in the foreground or background, but it can not be minimized.
' Timestamp.vbs
' VBScript for Windows
' This script sends the current system date/time to the keyboard input of the
' Arduino Serial Monitor. If more than one Serial Monitor is open, change the
' "COM" entry below to the specific COM port used for the RTC sketch (i.e. "COM3").
' The Serial Monitor line-end setting must be set to Newline or Carriage Return.
' The "Weekday(dDate,1)" entry is for Sunday being day 1.
' Change to "Weekday(dDate,2)" to make Monday day 1.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "COM"
oldTime = Time
While oldTime = Time
Wend
WshShell.SendKeys TimeStamp(Now)
Function TimeStamp(dDate)
TimeStamp = "T"&right("0"&second(dDate),2)&"{ENTER}"&"X"&right("0"&Minute(dDate),2) _
&right("0"&Hour(dDate),2)&Weekday(dDate,1)&right("0"&Day(dDate),2) _
&right("0"&Month(dDate),2)&right(Year(dDate),2)&"{ENTER}"
End Function
Any idea if it works on IDE2.3.4 ?
Thanks, looks like the simplest option.
I had some wonky formatting in that script. Here's the correct version with a simple "T" timestamp (TSSMMHHwDDMMYY{ENTER}), and with NOTEPAD replacing COM:
' Timestamp.vbs
' VBScript for Windows
' This script sends the current system date/time to the keyboard input of the
' Arduino Serial Monitor. If more than one Serial Monitor is open, change the
' "COM" entry below to the specific COM port used for the RTC sketch (i.e. "COM3").
' The Serial Monitor line-end setting must be set to Newline or Carriage Return.
' The "Weekday(dDate,1)" entry is for Sunday being day 1.
' Change to "Weekday(dDate,2)" to make Monday day 1.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "NOTEPAD"
oldTime = Time
While oldTime = Time
Wend
WshShell.SendKeys TimeStamp(Now)
Function TimeStamp(dDate)
TimeStamp = "T"&right("0"&second(dDate),2)&right("0"&Minute(dDate),2) _
&right("0"&Hour(dDate),2)&Weekday(dDate,1)&right("0"&Day(dDate),2) _
&right("0"&Month(dDate),2)&right(Year(dDate),2)&"{ENTER}"
End Function
If you run Notepad, you'll see what it does. It works with COM under IDE v1.8 because COM3, or whatever, is a separate program running under Windows, so AppActivate can find it. But for v2, you'll have to experiment and see what works. The serial monitor is no longer a separate program, but just a window within the IDE. Please report here if you find a v2 solution.
And I guess VBS is deprecated these days, but something similar in Powershell should work.
Ick. YYYYMMDDTHHMMSS is the way.
Please forgive my learning curve. I am not yet a programmer.
Is the command "send keys Timestamp" going directly to the SPI RTC attached to the Arduino or are additional instructions required?
If the Arduino is presently running a sketch, does this "shell" program cause an interrupt pausing the sketch, runs the "shell" program on the Arduino and then the Arduino returns to it's original sketch?
Could the "shell" program download the current time and call a subroutine in the Arduino sketch to update the SPI RTC?
Again, I appreciate your patience but I want to truly understand this.
Barry
FYI The Arduino GIGA DOES have a built in RTC. And you can attach a small coin cell battery to keep it running even if the GIGA is powered down.
The GIGA also has WiFi built in so it's relatively easy to set the RTC time from the internet using NTP. Or just set it manually and let the battery backup keep it going for a year or more.
The GIGA is one of the pricier Arduino's but it sure comes with a LOT of capabilities.