Hello everyone,
This is a code for getting the time from a NTP server using Arduino and Ethernet Shield.
I have already explained some parts of the code, but I'd like to understand every part of it. PRecisely the blond type, parts with comments in capital letters. Can anybody help me out?
#include <SPI.h>
#include <Ethernet.h> //Librería para usar la shield Ethernet
#include <EthernetUdp.h>
#include <Time.h>
/* Configurar la dirección MAC de la tarjeta Ethernet
Aseguraos de que no existe otro dispositivo con esta misma dirección*/
byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
/*Configuración del servidor NTP
us.pool.ntp.org NTP servidor
IP que Arduino tomará como servidor a la cual se accede para ver los datos
(Configurar la IP del servidor elegido) */
IPAddress timeServer(81,19,96,148);
/* Configuración de la hora. España->GTM+2 en segundos
horario de verano */
const long timeZoneOffset = 7200L;
/* Sincroniza con el servidor NTP cada 15 s. para realizar el testeo.
Aumentar el tiempo posteriormente.*/
unsigned int ntpSyncTime = 15;
/* ALTER THESE VARIABLES AT YOUR OWN RISK */
//Puerto para escuchar los paquetes UD.
unsigned int localPort = 8888; // WHY 8888? WHICH PORT IS IT? WHY UNSIGNED INT?
//La hora viene en los primeros 48 bytes del paquete UDP.
const int NTP_PACKET_SIZE= 48;
//Buffer para almacenar los paquetes entrantes y salientes.
byte packetBuffer[NTP_PACKET_SIZE];
// Sentencia que permite enviar y recibir paquetes UDP.
EthernetUDP Udp; //THIS SENTENCE ALLOWS TO SEND AND RECIEVE DATA, BUT WHY NOT DECLARED IT AT FIRST PLACE?
/* Comprobar el tiempo que ha pasado desde que se actualizo el sevidor
NTP por última vez.*/
unsigned long ntpLastUpdate = 0;
//Comprobar la última vez que el reloj mostró "Not in Production".
time_t prevDisplay = 0;
void setup() {
Serial.begin(9600);// Velocidad de la transmisión y apertura del puerto.
int i = 0;
int DHCP = 0;
DHCP = Ethernet.begin(mac); //Asigna direción MAC a la placa Ethernet.
//Se intenta 30 veces y si no es posible, se desiste.
while( DHCP == 0 && i < 30){
delay(1000);
DHCP = Ethernet.begin(mac);
i++;
}
//Si DHCP es falso (DHCP=0):
if(!DHCP){
Serial.println("DHCP FAILED");
for(;;); //Bucle infinito puesto que DHCP ha fallado.
}
Serial.println("DHCP Success"); //Ha sido exitosa la comunicación.
//Intentar obtener fecha y hora 10 veces.
int trys=0;
while(!getTimeAndDate() && trys<10) {
trys++;
}
// Inicializar el pin digital 5 como salida.
pinMode(5, OUTPUT);
}
// Obtener fecha y hora.
int getTimeAndDate() {
int flag=0;
Udp.begin(localPort); //ANOTHER PORT TO CONFIGURE? WHAT'S THIS?
sendNTPpacket(timeServer); //Envía un paquete NTP al servidor de tiempo.
delay(1000);
if (Udp.parsePacket()){//I JUST DON'T UNDERSTAND THIS WHOLE PART OF THE CODE BELOW. THE HIGHWORD/LOWWORD...WHY BUFFER [41]... ETC....
Udp.read(packetBuffer,NTP_PACKET_SIZE); // lee el paquete.
/* Variable long sin firmar almacena números, 32 bits (4 bytes).
Por el contrario que las variables long estándar, las unsigned long
no almacenan números negativos.*/
*/ Palabra alta: mitad más significativa (16 bits)
Palabra baja: mitad menos significativa (16 bits)
epoch: tiempo Unix */
unsigned long highWord, lowWord, epoch;
highWord = word(packetBuffer[40], packetBuffer[41]);
lowWord = word(packetBuffer[42], packetBuffer[43]);
epoch = highWord << 16 | lowWord;
// 2208988800 corresponde a 00:00 1 Enero 1970 GMT
epoch = epoch - 2208988800 + timeZoneOffset; //TILL HERE i don't understand it
flag=1;
setTime(epoch);//Config. hora
ntpLastUpdate = now();//Última actualización
}
return flag;
}
//Enviar un paquete al servidor de hora.
unsigned long sendNTPpacket(IPAddress& address) //SEND A PACKAGE TO THE NTP SERVER?
{
//Todos los bytes del buffer a 0.
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; //WHAT THE HELL¿? WHY LL THESE NUMBERS IN HEX.?
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //THIS TOO
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
// Muestra la hora y la fecha.
void clockDisplay(){
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
//Visualización del reloj: grabados anterior colon y 0 a la izquierda.
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// This is where all the magic happens...
void loop() {
/* Actualizar la hora a través del servidor NTP con la regularidad con que se
estableció arriba. */
[b]if(now()-ntpLastUpdate > ntpSyncTime) { //I got lost[/b]
int trys=0;
while(!getTimeAndDate() && trys<10){
trys++;
}
if(trys<10){
Serial.println("ntp server update success");
}
else{
Serial.println("ntp server update failed");
}
}
// Display the time if it has changed by more than a second.
if( now() != prevDisplay){
prevDisplay = now();
clockDisplay();
}
Thanks!!!