This is a few years old, and it seems things have changed, i couldn't get this to work. So, this is possibly the least graceful way of getting the time w/ an ESP8266, but after spending days trying to learn how to program Arduino, most everything above was too advance for me to figure out, except that I could tell the time from the NTP server was not always coming back in the same position, and was taking more than just 4 bytes. I have no idea why, but I can copy and paste hex into an online converter:)
Here is my solution to getting the current time, hopefully it helps somebody else.
ESP8266 is connected to a Mega2560 via Serial 3
If you have a Mega w/ builtin WiFi, it seems they are also connected to Serial 3
HardwareSerial & dbgTerminal = Serial;
HardwareSerial & espSerial = Serial3;
String sntptime, day, month, daynum, hour, minute, second, year;
// Get the data from the WiFi module and send it to the debug serial port
String GetResponse(String AT_Command, int wait){
String tmpData;
espSerial.println(AT_Command);
delay(200);
while (espSerial.available() >0 ) {
char c = espSerial.read();
tmpData += c;
if ( tmpData.indexOf(AT_Command) > -1 )
tmpData = "";
else
tmpData.trim();
}
return tmpData;
}
String GetTime()
{
// Uses the modem's SNTP feature to get the time
//the time always comes back 3 letter day, 3 letter month, 2 digit day number, 2 digit hr, : 2 digit minute, : 2 digit second, 4 digit year
sntptime = GetResponse("AT+CIPSNTPTIME?", 100);
// Serial.println(sntptime);
sntptime.remove(0,13); // Remove 13 characters starting at index=0
sntptime.remove(20);
// Serial.print("removed first 13 letters from sntptime = ");
// Serial.println(sntptime);
day=sntptime;
day.remove(3); // Remove from from index=3 through the end of the string
// Serial.print("Day = ");
// Serial.println(day);
month=sntptime;
month.remove(0,3);
month.remove(3);
// Serial.print("Month = ");
// Serial.println(month);
daynum=sntptime;
daynum.remove(0,6);
daynum.remove(2);
// Serial.print("Day number = ");
// Serial.println(daynum);
hour=sntptime;
hour.remove(0,8);
hour.remove(2);
// Serial.print("Hour = ");
// Serial.println(hour);
minute=sntptime;
minute.remove(0,11);
minute.remove(2);
// Serial.print("Minutes = ");
// Serial.println(minute);
second=sntptime;
second.remove(0,14);
second.remove(2);
// Serial.print("Seconds = ");
// Serial.println(second);
year=sntptime;
year.remove(0,16);
year.remove(4);
// Serial.print("Year = ");
// Serial.println(year);
return sntptime;
}