here you go. don't be afraid if it looks long - it's just the comments.
that full sketch once compiled uses up ~6500 bytes and global variables use 759 bytes. memory footprint could be reduced by F()-ing the char strings I'have hardcoded. I've been lazy
it's coded just as an example, so a lot of things are just hardcoded and there is no error handling
to ensure your ESP is connected to your wifi change the SSID and PWD in the following line
espATCommand("AT+CWJAP=\"[color=red][b]YOURSSID[/b][/color]\",\"[color=red][b]YOURPWD[/b][/color]\"", "OK", LONG_PAUSE); // connect to wifi
to get an NTP time, adjusted to French time (cf the UTC_DELTA stuff in the code for your time zone ) you just need to do
epochUnix = epochUnixNTP();
ntpHours = (epochUnix % NUMBEROFSECONDSPERDAY) / NUMBEROFSECONDSPERHOUR;
ntpMinutes = (epochUnix % NUMBEROFSECONDSPERHOUR) / NUMBEROFSECONDSPERMINUTE;
ntpSeconds = epochUnix % NUMBEROFSECONDSPERMINUTE;
attaching the file because it's more than 9000 chars. this is how it works
in the setup() I connect my ESP to my wifi network. here are the AT commands
AT+RESTORE wait for "ready" --> reset the ESP
AT wait for "OK" --> just to see if all is fine (useless)
AT+CWMODE=1 wait for "OK" --> Set the wireless mode
AT+CWQAP wait for "OK" --> disconnect, just to make sure (useless)
AT+CWJAP="YOURSSID","YOURPWD" wait for "OK" --> connect to wifi
AT+CIPMUX=0 wait for "OK" --> set the single connection mode
at that point you are connected to the Wifi (or you should).
Then when you want to querry the NTP time, I issue the following AT commands:
AT+CIPSTART="UDP","fr.pool.ntp.org",123 wait for "OK" --> start UDP session with NTP server
AT+CIPSEND=48 wait for "OK" --> will send 48 bytes
here you need to send the 48 bytes of a NTP request, but only the first 4 matters for us
so I write 0xEC0600E3 to the ESP and then another random 44 bytes, whatever is in memory
ESPSEPRIAL.write((char*) &ntpFirstFourBytes, NTP_PACKET_SIZE);
At that point the CIPSEND command got its 48 bytes, and the NTP server got its stuff and will answer with the NTP buffer. but you can't just read the 48 bytes and hope to get the NTP buffer. before you read the buffer you need to get rid of what the ESP tells you. Mine answers
Recv 48 bytes\r\n\r\nSEND OK\r\n\r\n+IPD,48[color=red][b]:[/b][/color]
so I wait until I see the : character and then the serial link gives me the 48 bytes, out of which I'm only interested in bytes 40,41,42,43 and 44. I read all the bytes and do
AT+CIPCLOSE wait for "OK" --> close the session
that's for the AT commands.
I would suggest you run them first all manually in a direct manual AT session with your ESP to see if you get similar answers from your version of the firmware. My code does not do error checking but the critical one is the : mentioned above as this is how I detect the start of the 48 bytes of the NTP buffer. if you are off when you read the buffer, then nothing will work. so try commands by hands, see what it spits out and if it looks like mine and adjust code accordingly.
I let you see the code for the maths to transform the 32 bits I capture into a time in seconds.
let me know if that works and have fun!
ntp.ino (11.4 KB)