NTP question

Hi all,

I am so confused. I have looked at tons of examples and read about getting time from a NTP server. I am overwhelmed and just dont understand time on arduino.

I am wanting to just set the time on the arduino mega 2560 when it powers up & then not contact the NTP server again, unless the arduino powers back up again. I do not really need accuracy above what the arduino can do on its own. I found one example that just counts the millis() and keeps the time that way & its perfect, other than it sets the initial time in setup(). So its in the code to start the time at 12:27 every time it powers up. If I could set this initial time from a ntp server that would be awesome.

All of the examples that I see, I am under the impression that they constantly call out to the ntp server and get the new time every minute or so. This keeps the time accurate, but I dont need all that.

I think I dont understand some of the libraries. They may be contacting the server once in the setup() & just using a function of the time library to get it from a millis() calculation. I may be assuming that the examples are calling to the server at intervals, but it really is just getting it from the library function. But I dont know.

Has anyone ran into this situation that could point me to an example or give me a few pointers on how to do that?

I am really not sure what "sets" the time on the microprocessor and once that is "set" does it keep track of it? and how do I determine what current time it is and converting it to a readable format. I am so confused.....

I used the example from

which uses Ethernet to connect to the NTP server
a run gives

Seconds since Jan 1 1900 = 3725591676
Unix time = 1516602876
The UTC time is 6:34:36
Seconds since Jan 1 1900 = 3725591687
Unix time = 1516602887
The UTC time is 6:34:47
Seconds since Jan 1 1900 = 3725591698
Unix time = 1516602898
The UTC time is 6:34:58
Seconds since Jan 1 1900 = 3725591710

Why are you using a Mega, and for what.

The Mega, with it’s ceramic resonator clock, is a bad timekeeper.

Getting the tiny time packets off the internet often is not a problem, so what is the problem.

You could use the WiFi module as a time keeper between NTP queries, since it has a much better crystal based clock.

You can set the NTP interval to whatever you like. Once a day, once an hour, whatever.

There is always the option of a DS3132 RTC module.
Leo…

I am overwhelmed and just don't understand time on arduino.

The Arduino doesn't have any knowledge of "clock time." All it knows about is "milliseconds since last reset."
If you want to get the clock time from NTP once, when the system comes up, you'd store that time in a variable that had "unix time" (seconds since some time in 1970):

unsigned long unixtime;

void setup() {
    unixtime = NTP.getunixtime();
    Serial.print(Time.formatUnixTime(unixtime);  // print boot time.
 }

Then each time you wanted to use a clock-style time instead of an interval, you'd probably do something like

loop() {
    :
    Serial.print(Time.formatUnixTime(unixtime + (millis()/1000));  // print current time

(These functions are just made up. I don't know offhand whether there's a time library that will make what you want to do easier or not. But the important thing is to realize that the Arudino does not have a clock. It just has a stopwatch.)

getting the time from ntp server and keeping the wall clock time are to separate things.

For example, I use TimeLib for keeping the time. I initialize it with a time I get from my solar inverter, because the project is around the inverter. The inverter has seconds from 2000, so I need to convert it to epoch (1970) before setting in into TimeLib.

  int regs[2];
  modbusRequest(1, 40222, 2, regs);
  setTime(EPOCH_2000 + (unsigned int) regs[0] * 65535 + (unsigned int) regs[1]);

In esp8266 sketches I tested NtpClientLib (from Library Manger). I didn't try it in Atmega yet.

Wawa:
Why are you using a Mega, and for what.

The Mega, with it's ceramic resonator clock, is a bad timekeeper.

Getting the tiny time packets off the internet often is not a problem, so what is the problem.

You could use the WiFi module as a time keeper between NTP queries, since it has a much better crystal based clock.

You can set the NTP interval to whatever you like. Once a day, once an hour, whatever.

There is always the option of a DS3132 RTC module.
Leo..

I have the Mega connected to 2 flow meters and I want to record the flow from my water softener and water filter. I want to send a mqtt message with the time of the day that they regen or backflush. I dont count the gallons, I just catch that the inputs are changing states and so there is water flow at that point.

I didnt know that I could set the interval for NTP, thats what I need, I will search for that!

Thanks!

Do you have an ethernet shield on the Mega, or some ESP WiFi module connected to it?

Only two flow sensors?
This sounds like a job for a WeMos D1 mini, or similar. Not a Mega with a lot of bagage.
Leo..

I could use a D1 mini, or a huzzah esp8266, or a nodemcu too. I have the mega on my bench and that is just what I was working with at the moment. I have a water pressure sensor that I need to read and it works better with the mega than the esp8266 boards with an I2C adc.

If I can get it going on the mega, swapping to the 8266 wont be an issue. I use the 8266 boards to read switches and other stuff around the house and mqtt message to home assistant.

I really need to get my head around this time issue. I may just add a real time clock, but I still need to understand the process of setting its time from a time server or something. If not the daylight savings time will be a problem.

Rob_O:
I have a water pressure sensor that I need to read and it works better with the mega than the esp8266 boards with an I2C adc.

True. Can’t find a simple external A/D breakout board without internal reference (for ratiometric sensors).
The A/D of the ESP8266, and boards like the ADS1115 can’t do this.
Leo…

Wawa:
True. Can’t find a simple external A/D breakout board without internal reference (for ratiometric sensors).
The A/D of the ESP8266, and boards like the ADS1115 can’t do this.
Leo…

I didnt know what caused the erratic results from the esp8266 + adc. I only knew from my tests, that the arduinos were dead on reading the pressure sensor, and no amount of averaging would smooth out the esp8266+adc results.

Thanks for your input on everything! I appreciate it!