12

I would like to get a timestamp with Arduino-ESP8266.

But I don't have any idea how to get that.

I suppose we have to get the time from the Internet since the ESP8266 doesn't have any clock. Do we need to do that only once or every time we need a timestamp?

I'm already connected by Wi-Fi to the Internet.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stallingOne
  • 3,633
  • 3
  • 41
  • 63
  • worth mentioning: you can can get a RTC module for under $1 shipped... http://www.ebay.com/itm/201577236297 – dandavis Nov 11 '16 at 20:22

4 Answers4

15

NTP is the proven way of getting time remotely. NTP libraries, like @Marcel denotes, is making UDP connections to a server with an interval. So you do not need to do any polling to the server before using it.

Here is the usage of an NTP library with a one-hour offset and one minute refresh interval:

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#define NTP_OFFSET   60 * 60      // In seconds
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "europe.pool.ntp.org"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

void setup(){
  timeClient.begin();
}

void loop() {
  timeClient.update();
}

To get a timestamp or formatted time anytime you want, use the functions:

String formattedTime = timeClient.getFormattedTime();
unsigned long epcohTime =  timeClient.getEpochTime();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cagdas
  • 1,634
  • 2
  • 14
  • 27
  • Thanks! I get an error at your 4th line : `error: 'WiFiUDP' does not name a type` Do I need another library for that ? – stallingOne Nov 11 '16 at 11:44
  • 4
    I added this: `#include ` and it's compiling – stallingOne Nov 11 '16 at 11:46
  • `timeClient.getFormattedTime();` = 00:04:49 when it's 12:46, how do I addjust this ? (I'm in europe Brussels) – stallingOne Nov 11 '16 at 11:47
  • You are right! I've just added the missing library to the answer. So, your offset is setting with NTP_OFFSET and it is in seconds! So we gave 120 seconds which i 2 minutes to the offset. I've corrected it with GMT+1 by setting 3600 which should be valid for Belgium. – cagdas Nov 11 '16 at 12:10
  • @ooo Any results? – cagdas Nov 11 '16 at 15:16
  • Still need `#include `. This example works: https://github.com/arduino-libraries/NTPClient/blob/master/examples/Basic/Basic.ino and is just 1 hour off – stallingOne Nov 14 '16 at 09:10
  • so I added `#define NTP_OFFSET 1*60*60 ` like you said. Thanks ! (I think your respons also misses `timeClient.begin();`) – stallingOne Nov 14 '16 at 09:21
  • also `unsigned long epcohTime = getEpochTime();` is not working. I think you can remove it – stallingOne Nov 14 '16 at 09:25
  • Question : This gives me only the hh:mm:ss, I would also need the date. Is that possible too? – stallingOne Nov 14 '16 at 10:23
  • @ooo, thanks for your feedback. I did not like to share complete sketch, but if it is confusing, i'll update it of course. This NTP library has only a function in its public api which returns day number. It returns 0 for Sunday, 1 for Monday etc. If it is enough for you, use getDay() of the timeClient. – cagdas Nov 14 '16 at 10:56
  • Is there a way to get Date? – Lakshitha Wisumperuma Jul 14 '19 at 11:24
2

Looking at that code I don't think this microcontroller directly has a clock built in as mentioned. Since you have Wi-Fi though you could make a web query to get it instead.

I'd use a REST query to a place like this:

https://timezonedb.com/api

Which will give you back a JSON formatted time. If you only need accuracy +/- a few seconds that will be fine. You could lower the bandwidth / improve battery life by setting the time like this and then using an internal timer to calculate an offset instead of making a query every time you need a time stamp. Eventually you would need to requery the time and 'correct' it since your timer on that probably is not accurate for long periods of time, plus it could eventually roll over.

If you need the time more accurately then that you will likely need a clock. You could try to do a bit of correction based on ping, but all in all how accurate it needs to be is based on your project requirements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chase R Lewis
  • 2,119
  • 1
  • 22
  • 47
1

Contact a specific date/time API on the Internet as described by user2927848. Or send a simple HTTP request to a server you trust and read the Date response header.

If you need greater precision, you may want to use an NTP client for Arduino.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
0

There is also another solution for ESP8266/ESP32. The Espressif's time implemenation provides a configTime() method which alows configuring and using a NTP server (see SimpleTime.ino example by Espressif).

Assumption: You have already setup and connected to a WiFi network and UTP library is loaded.

First configure your timezone and NTP server f.e.:

const char* ntpServer1 = "pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const char* time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Berlin

configTime(time_zone, ntpServer1, ntpServer2);

After the time is fetched and set just call time(nullptr) you receive the current timestamp.

Bruce
  • 1
  • 1