While building a clock, I wanted the clock to be working with a GPS module. Unfortunately I wasn't able to find on which would work, guaranteed, indoors. It would get the time, but not continuously.
So I had to use a RCT module which I could set withe the computer. But is wasn't as accurate as I had expected nor wished.
The programs I found were often too extensively equipped with displays etc. which made it more complex than necessary for my needs. And it never fully matched the modules I had bought.
As I had managed to create a program for the clock with the GPS as well a version with the RTC, I decided to combine the knowledge into setting the correct time into the RTC with the use of the GPS.
It will set the RCT as soon as the GPS has received a timestamp and will do it again after one minute. This will give me the opportunity to disconnect the Nano and terminate the program.
I realize the program does not meet all the conventions, but it works and in the end, that is what matters.
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
static const int RXPin = 15, TXPin = 16;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps; // The TinyGPSPlus object
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device
int uurReal;
int minuutReal;
int uur;
int minGPS;
int minTien;
int minEen;
int secGPS;
int prevMinGPS;
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("GPS naar RTC.ino"));
Serial.println(F("theo6772"));
Serial.println();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void loop() {
while (ss.available() > 0) // Dispatch incoming characters
gps.encode(ss.read());
if (gps.time.isUpdated()) {
uurReal = gps.time.hour();
minuutReal = gps.time.minute();
minEen = gps.time.minute() %10;
minGPS = gps.time.minute();
minTien = minGPS / 10;
secGPS = gps.time.second();
}
minEen = gps.time.second() %10;
if(minGPS != prevMinGPS){
if (minEen == 0 && gps.time.hour() != 0) { // To prevent ruining a setting because the GPS has no timestamp
RTCset();
}
}
prevMinGPS = minGPS;
}
void printDigits(int digits) { // Utility function for printing two digits
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
void RTCset(){
DateTime now = rtc.now();
rtc.adjust(DateTime(gps.date.year(), gps.date.month(), gps.date.day(), gps.time.hour(), gps.time.minute(), gps.time.second()));
Serial.print("- ");
Serial.print("Time GPS : ");
Serial.print(uurReal);
Serial.print(':');
printDigits(minuutReal);
Serial.print(':');
printDigits(secGPS);
Serial.print(" Time RTC : ");
Serial.print(now.hour());
Serial.print(':');
printDigits(now.minute());
Serial.print(':');
printDigits(now.second());
Serial.println();
}
A lot of the code I've used in different programs comes from forums like this. So I decided to 'repay' all those who have, unknowingly, contributed to them. And of course to the benefit of all who look for solutions to their problems/challenges.
Why don't you consider using an NTP server (i.e. internet synchronization)?
For indoor use I did this and it works perfectly, obviously as long as there is an internet connection (maybe via WiFi, to simplify connections)...
My brother had also suggested this but I couldn't find WiFi examples that I could understand. Let alone modifying it into my project. And I don't want to use any more cables. Eventualy I want a clock with internal power that I can hang up to the wall.
Maybe further in time I will try to work this out but at the moment I'm focusing on other projects.
Despite the RTC definition in the code, I use the DS3132 module to limit the alterations in the time.
WiFi with NTP is much easier for an indoor clock.
Then you don't need a RTC either.
An ESP32C3 with a few lines of code is all you need.
Basic example that prints the time to the serial monitor.
#include <WiFi.h> // ESP32
unsigned long prevTime;
time_t now; // holds epoch
tm tm; // create time structure
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PW"); // WiFi credentials
configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}
void loop() {
time(&now);
if (now != prevTime) { // time has changed?
prevTime = now; // remember
localtime_r(&now, &tm); // compute local time
printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print some of the available time elements
}
}
Don't know what RTC you used, but the DS3231 is accurate to within +- 2 parts per million, or +- 1.2 secs per week. By using something like NTP to make an adjustment once a week, it can be considered to be 100% accurate. I would never rely on GPS, the sync up time can vary widely and indoors is almost impossible.
The above NTP code re-syncs every three hours.
An ESP32 keeps time within one or two seconds between those updates.
A RTC complicates things, and is only needed for portable projects.
Leo..
An RTC keeps working across power outages and is often the main reason to choose it; otherwise, NTP or a built-in clock, as you suggest, is fine. The OP didn't mention power outages, but as it's an indoor project, it is likely mains powered; therefore, it will need to deal with power outages.
not the OP. reason 1: in my lengthy career as an electronics technician I was always required to have a backup.
reason 2: I anticipate electric grid failures in the future. we had a brief failure about a year ago. the internet failed, and about a minute later the lights went out. demonstrated failure proves the requirement for backup.
Third world country?
Power interruptions here (NZ) only happen during natural disasters or maintenance, which is announced by email a month ahead. I never see any dips or brownouts here.
No point keeping the time if you can't display the time.
A powerbank between supply and LED clock can keep everything running.
Leo..
When we get a (rare) power failure, I just reset the clocks upon its return. It's not that onerous and it's not a mission-critical thing. A clock sync'd with NTP will adjust to correct time as soon as power (and WiFi) returns.
I don't think anyone is talking about dips or brownouts. The issue is that in at least the part of the world I live in and for the other 100+ million folks, natural disasters are increasing due to climate change. Maybe in NZ you are avoiding that but in central North America it is increasing.
This is the clock I made. 20 by 22 cm.
Leo, you're right. I live in The Netherlands. So the Tekstklok will light up the words that describe the time. This picture is taken at 21:13h. "It is almost a quarter past nine".
I will definitely keep all your remarks and recommendations in mind when I'll build one big enough to hang on the wall. If my wife permits
To make it cordless, my biggest challenge will be how to power it without having to charge it every one or two days.
Nice project.
Mains power would be needed for a LED project like that.
I have code for an ESP32C3 that turns off WiFi when not in use.
Then the ESP will draw about the same current as a Nano.
Let me know if/when you need it.
Give the clock to your wife as a present, so she can't refuse.
Leo (Dutchie in NZ)..
I agree with @Wawa , no way to make it cordless with batteries and last for more than a bunch of hours. And for a clock isn't quite a good thing, right?
Getting back to the time sync. Just to add my five cents, a digital clock I had for yeas in my living room died, so I took one of my WeMos D1 mini I have in my home lab, an LCD, and in half an hour I have built up a brand new digital clock! With just an USB power cord, I used NTP to regularly update the time, the result is this below:
It works fine.
If some code is needed to start with NTP, just ask.
PS: ok, I know, it's still "naked" aka no box bc I'm lazy... I need to find out a proper box which fits (behind there's the dead clock, alas it's too small to fit that LCD).
I promised my wife I'll do it soon! ):