1

I write code to upload to ESP8266. I wish to use time stamp in order to log events. I found an NTP sketch, I can work with ( since it has a wifi connection ), but I wish to keep my code as simple as possible.

So I'm thinking to use my MQTT server to send time updates from time to time ( have a certain topic for that ), and in between updates, use millis() .

Since I'm new here, I want to know from experienced programmer if it makes sense ? I would love to hear pros and cons ( before I start coding ).

Any help will be appreciated,

Guy

  • How important is it to you that the time is accurate? NTP will do a more reliable job than getting the time via MQTT. NTP is designed to compensate for the time it takes to get an answer from the NTP server and to deal with clock drift. With MQTT you won't be sure how long it's been since the MQTT server posted the time - which may be fine for your application, if you just need a timestamp that's roughly accurate. – John Romkey Sep 16 '18 at 18:24
  • @JohnRomkey -thank you for you answer. As I said, it is for a time stamp, meaning few minutes a day (say 2 ), will be acceptable. I'm sure that you are 100% correct but I wish to keep my code as lite as possible – Guy . D Sep 16 '18 at 19:02
1

Use the Time library a.k.a. TimeLib.h. It will keep time between calls to your source of time, if it is your MQTT call or NTP server.

In your code you will use the Time library to get time and the source of the current time is independent from the rest of the code.

The TimeLib has a function setTime to set the current time and then it keeps the time using millis(). You can update the time by calling setTime again. The timestamp value for setTime is 'Unix Epoch' - number of seconds from "1970-01-01 00:00:00".

If you decide to add NTP to your project, you can use NtpClientLib. It sets the time to TimeLib with one simple line of code in setup(). Example NTP.begin("pool.ntp.org", 1, false);.

  • down-voter, what is wrong? – Juraj Sep 16 '18 at 19:42
  • Maybe he downvoted because the question was aimed at pros and cons of getting the time through MQTT in relation to other methods of time sync instead of how to manage this time in the program. – chrisl Sep 16 '18 at 21:05
  • I was thinking the same- why the down vote ? I did asked for some guidance of assistance. I UP VOTE. thank you ( i'll be happy for more explain/ code if you have ) – Guy . D Sep 17 '18 at 4:43
  • @Juraj - thank you, i'll try using NTP as well. GUY – Guy . D Sep 17 '18 at 11:42
  • @Juraj - there is another function to set time interval- should I need to use it? what is the default time if I don't define? – Guy . D Sep 18 '18 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.