Hello, is there is a RV-8803 RTC (or RTC that can do samplings at 1msec to 10msec intervals) in the Feather form factor so I can stack on or below a ESP32-S3 Think Plus?
Just search “RV-8803” on their website and you can see for yourself.
I did before posting. There are two items. One looks like Feather form factor but it is called smol and also discontinued. Sometimes the search function of company websites doesn’t work properly and not listing the correct corrently. I want to double check with Sparkfun’s staff to make sure.
By “The RTC module has counters for hundredths of seconds…” Does that mean it can output time in 10msec interval (i.e. 0, 10msec, 20msec, 30msec, etc.)?
Thank you for the answers.
Please see the example below. It is written for ESP32 but should work OK on ESP32_S3. Maybe you do not need an external RTC?
Best wishes,
Paul
Thanks Paul. The program works on my ESP32-S3 Think Plus.
How do I print the time in msec? Is there an internal built-in function to retrieve time in msec? I want to get data from multiple sensors with a time interval of 1msec.
0msec sensor1, sensor2, sensor3, ….
1msec sensor1, sensor2, sensor3, ….
2msec sensor1, sensor2, sensor3, ….
I am using micros() instead of millis() for higher accuracy. It prints in date and time followed by a number. Should it be reset to 0 at some point or I need a hardware pin to set it to zero?
#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"
const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASS";
const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
unsigned long lastNtpUpdateTime = 0; // To store millis() at the last successful NTP sync
unsigned long currentEpochSeconds;
const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("No time available (yet)");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
Serial.println("Got time adjustment from NTP!");
printLocalTime();
}
// Call this after a successful sync to store the sync time
void recordNtpSyncTime(long epochSeconds) {
//lastNtpUpdateTime = millis();
lastNtpUpdateTime = micros() / 1000;
// Store the epoch time in a global variable (e.g., unsigned long currentEpochSeconds)
currentEpochSeconds = epochSeconds;
}
uint64_t getEpochTimeMs() {
// Calculate the time elapsed since the last NTP update using millis()
//unsigned long elapsedMs = milli() - lastNtpUpdateTime;
unsigned long elapsedMs = micros() / 1000 - lastNtpUpdateTime;
// Combine the last known epoch seconds with the elapsed milliseconds
uint64_t totalMs = (uint64_t)currentEpochSeconds * 1000 + elapsedMs;
return totalMs;
}
void setup() {
Serial.begin(115200);
// First step is to configure WiFi STA and connect in order to get the current time and date.
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
/**
* NTP server address could be acquired via DHCP,
*
* NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
* otherwise SNTP option 42 would be rejected by default.
* NOTE: configTime() function call if made AFTER DHCP-client run
* will OVERRIDE acquired NTP server address
*/
esp_sntp_servermode_dhcp(1); // (optional)
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
// set notification call-back function
sntp_set_time_sync_notification_cb(timeavailable);
/**
* This will set configured ntp servers and constant TimeZone/daylightOffset
* should be OK if your time zone does not need to adjust daylightOffset twice a year,
* in such a case time adjustment won't be handled automagically.
*/
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
/**
* A more convenient approach to handle TimeZones with daylightOffset
* would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
* A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
*/
//configTzTime(time_zone, ntpServer1, ntpServer2);
}
void loop() {
//delay(5000);
printLocalTime(); // it will take some time to sync time :)
Serial.println(getEpochTimeMs());
}
It might be easier to use a library like ESP32Time. It supports both millis and micros.
Yes, this library is very useful. It solved my main problem. Thank you very much.
Hi @sparkfun_user ,
Thank you for the update. I am glad the library is working for you. Here is the code we use to print the RTC time with milliseconds:
#include <ESP32Time.h> //http://librarymanager/All#ESP32Time by FBiego
ESP32Time rtc;
// Get the timestamp
const char *getTimeStamp()
{
static char theTime[30];
// 1 2 3
// 123456789012345678901234567890
// YYYY-mm-dd HH:MM:SS.xxx0
struct tm timeinfo = rtc.getTimeStruct();
char timestamp[30];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &timeinfo);
snprintf(theTime, sizeof(theTime), "%s.%03ld", timestamp, rtc.getMillis());
return (const char *)theTime;
}
Serial.println(getTimeStamp());
Best wishes,
Paul
Thanks. I tried your program but it gave me a compilation error:
C:\Users\user1\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.3.6\cores\esp32/HardwareSerial.h:432:16: error: ‘HWCDCSerial’ does not name a type
432 | #define Serial HWCDCSerial
| ^~~~~~~~~~~
C:\Users\user1\AppData\Local\Temp.arduinoIDE-unsaved202617-33104-k6usoa.amxjr\sketch_feb7a\sketch_feb7a.ino:21:1: note: in expansion of macro ‘Serial’
21 | Serial.println(getTimeStamp());
| ^~~~~~
exit status 1