Hi all.
I got the clock back in my home now, after it was with my father during his last time (he died last week, hence the simple code to just make it run, so he could see it in action).
It's been running in my home for about a week now, and the time drift quite a lot (around 30 seconds a week), so now its time to make it more accurate (NTP or External clock).
I'm thinking of using NTP given the accuracy that provides (with an external WiFi antenna, placed outside the metal enclosure). (my current board does not have a connector for an external antenna, but i will get one that does later)
Would i use my current code together with the NTP code, or should the "delay" values be exchanged with something else?
Current code:
const int in1 = 5;
const int in2 = 4;
void setup() {
// put your setup code here, to run once:
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void loop() {
// run
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(500);
// pause no pulse
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(500);
// change polarity
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(500);
// pause no pulse
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(500);
}
Will copy this (more or less) for the NTP: ESP32 NTP Client-Server: Get Date and Time (Arduino IDE) | Random Nerd Tutorials
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-date-time-ntp-client-server-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include "time.h"
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7200;
void setup(){
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
// Init and get the time
configTime(gmtOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void loop(){
delay(1000);
printLocalTime();
}
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
Serial.print("Hour (12 hour format): ");
Serial.println(&timeinfo, "%I");
Serial.print("Minute: ");
Serial.println(&timeinfo, "%M");
Serial.print("Second: ");
Serial.println(&timeinfo, "%S");
Serial.println("Time variables");
char timeSecond[3];
strftime(timeSecond,3, "%S", &timeinfo);
Serial.println(timeSecond);
Serial.println();
}
Not sure if i need that last part of the code (where it saves the time in variables - i modified the code to seconds). (or any of the above code relating to things other than seconds)
Am I on the right path here and how do i merge the two codes together?
I'm thinking that the NTP should pull the time once every hour (not sure if the board/code does this as standard).