
Servidor NTP time server
Publicado por Juan (1 intervención) el 05/11/2017 20:17:57
Hola, desde hace unos tres años tengo instalado un sistema domotico con un Arduino mega, al iniciarse el programa realiza la lectura de la fecha y la hora a través de un servidor NTP, resulta que de repente esta lectura a dejado de funcionar. Pongo el código de ejemplo que utilice para incorporar a mi programa y que funcionaba correctamente hasta hace 4 días.
Por lo que he podido comprobar el problema está en la línea después del comentario de la marcada con //********************************************************
Podéis probar el código haber si os funciona y si detectáis el porque ha podido dejar de funcionar sin haber modificado el código
Gracias y un cordial saludo
Javo55
Por lo que he podido comprobar el problema está en la línea después del comentario de la marcada con //********************************************************
Podéis probar el código haber si os funciona y si detectáis el porque ha podido dejar de funcionar sin haber modificado el código
Gracias y un cordial saludo
Javo55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <SPI.h>
#include <Time.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Sunrise.h>
Sunrise mySunrise(41.37,2.18,+1);
#define offset 1970
byte mac [ ] = {
0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED }
;
IPAddress ip ( 192 , 168 , 0 , 17) ;
IPAddress dns1 ( 192 , 168 , 0 , 101 ) ;
IPAddress gateway ( 192 , 168 , 0 , 101 ) ;
IPAddress subnet ( 255 , 255 , 255 , 0 ) ;
EthernetServer server ( 82 ) ;
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(132, 163, 4, 101); // local NTP server
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
time_t prevDisplay = 0; // when the digital clock was displayed
long timeZoneOffset = -7200L;//-7200L; // set this to the offset in seconds to your local time;
char isodate[25]; // The current time in ISO format is being stored here
int minuIni=0;
void setup()
{
// Se abre puerto de comunicación serie:
//Ethernet.begin(mac); // ip dinamica
Ethernet . begin ( mac , ip , dns1 , gateway , subnet ) ; // ip fija
Serial.begin(9600);
Udp.begin(localPort);
Serial.println(Ethernet.localIP());
setSyncProvider(getNtpTime);
while(timeStatus()== timeNotSet);
// calcula salida y puesta de sol
minuIni=minute()+1;
}
void loop(){
// if (minute()==35) timeZoneOffset = -7200L;
// if (minute()==39) timeZoneOffset = -3600L;
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.println(second());
delay(1000);
}
/*-------- NTP code ----------*/
unsigned long getNtpTime()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
// ****************************************************************************************************************
// En la línea inferior Udp.parsePacket() siempre da valor 0 por lo que no está recibiendo respuesta del servidor
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into Unix time
// starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
return epoch - timeZoneOffset;
}
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
Valora esta pregunta


0