Hola.
Tengo un problema con la consulta de hora ,resulta que cuando esta por dhcp y realizo el dnsLookup todo funciona pero cuando realizo la configuración manual de ip no
la parte que no me funciona del todo bien es:
{
Serial.println("\n[Get Static IP]");
if (ether.begin(sizeof Ethernet::buffer, myMac,10) == 0) moduleStatus = 255;
ether.staticSetup(myip, gwip, dnsip, mask);
//ether.dnsLookup(ntpServer);
}
Algo me debe estar faltando o estoy haciendo mal porque si habilito ether.dnsLookup(ntpServer); funciona pero la idea es que lo quiero poner en una red donde no hay servidor de DNS y el servidor de NTP es interno
el código del programa que estoy usando para probar es:
#include <EtherCard.h> // https://github.com/njh/EtherCard
const byte myMac[] PROGMEM = { 0x00, 0x01, 0x02, 0x03, 0x06, 0x05 };
// ipparaconfiguracin manual
static byte myip[] = { 192,168,1,29 };// { 192,168,1,203 };
static byte mask[]= { 255,255,255,0 };
static byte gwip[]= { 192,168,1,1 };
static byte dnsip []= { 192,168,1,1 };
//IP de servidoresde hora
const char ntpServer[] PROGMEM = "ar.pool.ntp.org"; //Servidor NTP uy.pool.ntp.org ntp.bit.nl
static byte ntpServer1[] = {162,159,200,1 }; // ar.pool.ntp.org
static byte ntpServer2[] ={192,168,1,3}; // ntp_interno
//static byte ntpServer2[] = {213,136,12,53}; // ntp_interno
const long gmtOffset_sec = -10800;// para definir el horario Urugauy -3 elvalor esta en segundos
const unsigned int NTP_REMOTEPORT = 123; // NTP requests are to port 123
const unsigned int NTP_LOCALPORT = 8888; // Local UDP port to use
const unsigned int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte Ethernet::buffer[350]; // Buffer must be 350 for DHCP to work
byte moduleStatus;
unsigned long Tiempo;
uint8_t ntpIp[IP_LEN];
#define Boton1 3 // entrada de pulso para consulta de reloj
#define Boton2 4 // SW para selexcionar ntp internoo externo
#define Boton3 5 // SW para selexcionar dhcp o ip manual
boolean Ant_Boton1;
unsigned long BT1_Tiempo;
//#define reset() __asm__ __volatile__ ("JMP 0x0000 \n")
void setup() {
pinMode(Boton2,INPUT_PULLUP);//ntpinternoo externo
pinMode(Boton3,INPUT_PULLUP);//dhcp o ip manual
Serial.begin(1200);
Serial.println(F("\n[EtherCard NTP Client]"));
if (digitalRead(Boton3) == HIGH) // en HIGH ip manual
{
Serial.println("\n[Get Static IP]");
if (ether.begin(sizeof Ethernet::buffer, myMac,10) == 0) moduleStatus = 255;
ether.staticSetup(myip, gwip, dnsip, mask);
//ether.dnsLookup(ntpServer);
}
else // configuracion por dhcp
{
Serial.println("\n[Get DHCP]");
if (ether.begin(sizeof Ethernet::buffer, myMac) == 0) moduleStatus = 254;
ether.dhcpSetup() ; ut
ether.dnsLookup(ntpServer) ;
}
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
ether.udpServerListenOnPort(&udpReceiveNtpPacket, NTP_LOCALPORT);
if (digitalRead(Boton2) == LOW)ether.printIp("NTP1: ", ntpServer1);
else ether.printIp("NTP2: ", ntpServer2);
if (digitalRead(Boton2) == LOW) sendNTPpacket(ntpServer1);
else sendNTPpacket(ntpServer2);
}
void loop() {
//boton1
if (digitalRead(Boton1) == LOW) //Pregunta si el pulsador está presionado
{
if (Ant_Boton1 == LOW) //Pregunta si el pulsador está presionado
{
if ((millis()-BT1_Tiempo)>=800) //Pregunta si el pulsador está presionado
{
BT1_Tiempo=millis();
Tiempo=millis();
if (digitalRead(Boton2) == LOW) sendNTPpacket(ntpServer1);
else sendNTPpacket(ntpServer2);
}
else{
Ant_Boton1= LOW;
BT1_Tiempo=millis();
}
Ant_Boton1= LOW;
}
else{
Ant_Boton1= HIGH;
}
}
ether.packetLoop(ether.packetReceive());
//delay(50);
}
// send an NTP request to the time server at the given address
void sendNTPpacket(const uint8_t* remoteAddress) {
// buffer to hold outgoing packet
byte packetBuffer[ NTP_PACKET_SIZE];
// 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:
ether.sendUdp(packetBuffer, NTP_PACKET_SIZE, NTP_LOCALPORT, remoteAddress, NTP_REMOTEPORT );
//Serial.println("NTP request sent.");
}
void udpReceiveNtpPacket(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *packetBuffer, uint16_t len) {
//Serial.print("NTP response received.");
// the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract 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;
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
// Serial.print("Unix UTC time = ");
// Serial.println(epoch);
epoch=epoch + gmtOffset_sec; //para pasar a horario local
// Serial.print("The Local time is "); // UTC es el tiempo universal o GMT
Serial.print((epoch % 86400L) / 3600); // Horas (86400 segundos por dia) - Se haya el modulo
// Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) {
// Añadir un 0 en los primeros 9 minutos
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // Minutos:
// Serial.print(':');
if ( (epoch % 60) < 10 ) {
// Añadir un 0 en los primeros 9 minutos
Serial.print('0');
}
Serial.println(epoch % 60); // Segundos
Serial.println (millis()-Tiempo);
}
muchas gracias por cualquier sujerencia