Forum: Mikrocontroller und Digitale Elektronik EthernetUDP Zeit von Raspberry Zeitserver abholen,


von Jürgen (Gast)


Lesenswert?

Hallo
ich habe hier im LAN einen Arduino (Mega) mit Ethernet-Lan W5100, der 
seine Zeit von einem Zeitserver der auf einem Raspberry im gleichen lan 
installiert ist, bekommen soll hängen im 192.168.2.0 LAN

alle h

Auf dem Raspberry sieht die ntp.con so aus
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server 
will
# pick a different set every time it starts up.  Please consider joining 
the
# pool: <http://www.pool.ntp.org/join.html>;
pool ntp1.hetzner.de  iburst
pool ntp2.hetzner.com iburst
pool 0.de.pool.ntp.org iburst
pool 1.de.pool.ntp.org iburst


# Access control configuration; see 
/usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page 
<http://support.ntp.org/bin/view/Support/AccessRestrictions>;
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a 
configuration
# that might be intended to block requests from certain clients could 
also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow 
configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
restrict 192.168.2.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next 
line.
# (Again, the address is an example only.)
broadcast 192.168.2.255

# If you want to listen to time broadcasts on your local subnet, 
de-comment the
# next lines.  Please do this only if you trust everybody on the 
network!
#disable auth
#broadcastclient


mit tcpdump erhalte ich folgendes
 sudo tcpdump -vvveni any udp port 123
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture 
size 262144 bytes
16:42:54.783503  In de:ad:be:ef:fe:ed ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 128, id 10574, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.9.8888 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 
(unspecified), poll 6 (64s), precision -20
        Root Delay: 0.000000, Root dispersion: 0.000000, Reference-ID: 
(unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   0.000000000
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 0.000000000
16:43:05.844961  In de:ad:be:ef:fe:ed ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 128, id 10576, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.9.8888 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 
(unspecified), poll 6 (64s), precision -20
        Root Delay: 0.000000, Root dispersion: 0.000000, Reference-ID: 
(unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   0.000000000
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 0.000000000
16:43:08.558536 Out b8:27:eb:67:ec:f4 ethertype IPv4 (0x0800), length 
92: (tos 0xb8, ttl 64, id 37665, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.10.123 > 192.168.2.255.123: [bad udp cksum 0x86a3 -> 
0xf154!] NTPv4, length 48
        Broadcast, Leap indicator:  (0), Stratum 3 (secondary 
reference), poll 6 (64s), precision -18
        Root Delay: 0.021270, Root dispersion: 0.056213, Reference-ID: 
94.16.115.123
          Reference Timestamp:  3766487654.572646037 (2019/05/10 
16:34:14)
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   3766488188.558358062 (2019/05/10 
16:43:08)
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 3766488188.558358062 
(2019/05/10 16:43:08)
16:43:16.906417  In de:ad:be:ef:fe:ed ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 128, id 10578, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.9.8888 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 
(unspecified), poll 6 (64s), precision -20
        Root Delay: 0.000000, Root dispersion: 0.000000, Reference-ID: 
(unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   0.000000000
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 0.000000000


der Code auf dem arduino sieht so aus

/*

 Udp NTP Client

 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket
 For more on NTP time servers and the messages needed to communicate 
with them,
 see http://en.wikipedia.org/wiki/Network_Time_Protocol

 created 4 Sep 2010
 by Michael Margolis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi

 This code is in the public domain.

 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the 
shield
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 2, 9);
IPAddress gateway(192,168,2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dnsServer(192,168,2, 1);


EthernetClient client;
unsigned int localPort = 8888;       // local port to listen for UDP 
packets

const char timeServer[] = "printer2"; // time.nist.gov 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;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port 
only
  }

  // start Ethernet and UDP
 Ethernet.begin(mac, ip, dnsServer, gateway, subnet);
  Udp.begin(localPort);
}

void loop() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server

  // wait to see if a reply is available
  delay(1000);
  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, 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;
    Serial.print("Seconds since Jan 1 1900 = ");
    Serial.println(secsSince1900);

    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // 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.println(epoch);


    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at 
Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 
equals secs per day)
    Serial.print(':');
    if (((epoch % 3600) / 60) < 10) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals 
secs per minute)
    Serial.print(':');
    if ((epoch % 60) < 10) {
      // In the first 10 seconds of each minute, we'll want a leading 
'0'
      Serial.print('0');
    }
    Serial.println(epoch % 60); // print the second
  }
  // wait ten seconds before asking for the time again
  delay(10000);
  Ethernet.maintain();
}

// send an NTP request to the time server at the given address
void sendNTPpacket(const char * 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();
}


im Terminal erhalte ich jedoch keine Ausgabe.
Er bekommt also irgendwie die Zeit nicht korrekt

sieht da jemand den Fehler den ich machte?

Grüße Jürgen

von 900ss (900ss)


Lesenswert?

Jürgen schrieb:
> der Code auf dem arduino

Du hattest keine Lust, deinen Code in einem File an dein Posting zu 
hängen. Dann wäre er gut leserlich von der Forensoftware dargestellt 
worden. Einrückungen sind für dich auch Luxus.
So ist das alles deutlich schlechter lesbar und macht dein Posting auch 
noch elendig lang.
Erwartest du dann Lust beim Leser, sich das genau anzusehen?

von Sascha W. (sascha-w)


Lesenswert?

Hallo,

"printer2" als Timeserver ist nicht gerade ein RFC-gerechter Name. 
Sicher das der von dem DNS im Stack aufgelöst werden kann?

Sascha

von ... (Gast)


Lesenswert?

Vermutlich kann der TO kein Englisch. Sonst waere er wohl schon
selber auf die Fehler in seiner Konfiguration gekommen.

> "printer2" als Timeserver ist nicht gerade ein RFC-gerechter Name.
> Sicher das der von dem DNS im Stack aufgelöst werden kann?
Das scheint ja noch zu klappen.

von Jürgen (Gast)


Lesenswert?

Sascha W. schrieb:
> "printer2" als Timeserver ist nicht gerade ein RFC-gerechter Name.
> Sicher das der von dem DNS im Stack aufgelöst werden kann?

mmmhh...
ok da kannst du recht haben das meine lokale Namensauflösung nicht 
funktioniert.
vom Win7 Rechner kann ich diesen Namen jedenfalls anpingen.
Ganz ehrlich ... ich wusste nicht genau wie ich die IP-Adresse der 
Variablen korrekt mitteile.

Werde ich gleich ändern wenn ich weiss  wie ich die zeile ändern muss
const char timeServer[] = "printer2"; // time.nist.gov NTP server


ps. sorry für den langen unformatierten Text... wusste nicht das wenn 
ich es als File hochlade es dann sauber in den Text formatiert wird...

von Jürgen (Gast)


Lesenswert?

... schrieb:
> Vermutlich kann der TO kein Englisch. Sonst waere er wohl schon
> selber auf die Fehler in seiner Konfiguration gekommen.

dann gebe mir doch bitte einen tipp wonach ich suchen soll ...
ich vermute es liegt wohl an der ntp.conf beim Raspberry oder?

Kleine Hilfe von Dir möglich?

von Jürgen (Gast)


Lesenswert?

... schrieb:
> Vermutlich kann der TO kein Englisch. Sonst waere er wohl schon
> selber auf die Fehler in seiner Konfiguration gekommen.

ok ok... habe wahrscheinlich ein paar dicke Fehler in der ntp.conf

habe nun in der ntp.conf  das geändert...
# If you want to listen to time broadcasts on your local subnet, 
de-comment the
# next lines.  Please do this only if you trust everybody on the 
network!
disable auth
broadcastclient



nun kommt in der tcpdump ausgabe folgendes

 192.168.2.10.123 > 144.76.60.190.123: [bad udp cksum 0x9006 -> 0x7f94!] 
NTPv4, length 48
        Client, Leap indicator:  (0), Stratum 3 (secondary reference), 
poll 6 (64s), precision -18
        Root Delay: 0.016799, Root dispersion: 0.199172, Reference-ID: 
195.50.171.101
          Reference Timestamp:  3766505836.883575895 (2019/05/10 
21:37:16)
          Originator Timestamp: 3766505896.881202259 (2019/05/10 
21:38:16)
          Receive Timestamp:    3766505896.888573260 (2019/05/10 
21:38:16)
          Transmit Timestamp:   3766505898.871441292 (2019/05/10 
21:38:18)
            Originator - Receive Timestamp:  +0.007371001
            Originator - Transmit Timestamp: +1.990239033
21:38:18.888973  In 5c:49:79:67:0e:eb ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 54, id 11264, offset 0, flags [DF], proto UDP (17), 
length 76)
    144.76.60.190.123 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), 
poll 6 (64s), precision -24
        Root Delay: 0.011550, Root dispersion: 0.031661, Reference-ID: 
131.188.3.221
          Reference Timestamp:  3766504772.918602548 (2019/05/10 
21:19:32)
          Originator Timestamp: 3766505898.871441292 (2019/05/10 
21:38:18)
          Receive Timestamp:    3766505898.881186940 (2019/05/10 
21:38:18)
          Transmit Timestamp:   3766505898.881243871 (2019/05/10 
21:38:18)
            Originator - Receive Timestamp:  +0.009745648
            Originator - Transmit Timestamp: +0.009802579
21:38:20.871560 Out b8:27:eb:67:ec:f4 ethertype IPv4 (0x0800), length 
92: (tos 0xb8, ttl 64, id 57525, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.10.123 > 144.76.60.190.123: [bad udp cksum 0x9006 -> 
0x6fc3!] NTPv4, length 48
        Client, Leap indicator:  (0), Stratum 3 (secondary reference), 
poll 6 (64s), precision -18
        Root Delay: 0.016799, Root dispersion: 0.199203, Reference-ID: 
195.50.171.101
          Reference Timestamp:  3766505836.883575895 (2019/05/10 
21:37:16)
          Originator Timestamp: 3766505898.881243871 (2019/05/10 
21:38:18)
          Receive Timestamp:    3766505898.888976272 (2019/05/10 
21:38:18)
          Transmit Timestamp:   3766505900.871440106 (2019/05/10 
21:38:20)
            Originator - Receive Timestamp:  +0.007732400
            Originator - Transmit Timestamp: +1.990196234
21:38:20.890073  In 5c:49:79:67:0e:eb ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 54, id 11490, offset 0, flags [DF], proto UDP (17), 
length 76)
    144.76.60.190.123 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), 
poll 6 (64s), precision -24
        Root Delay: 0.011550, Root dispersion: 0.031692, Reference-ID: 
131.188.3.221
          Reference Timestamp:  3766504772.918602548 (2019/05/10 
21:19:32)
          Originator Timestamp: 3766505900.871440106 (2019/05/10 
21:38:20)
          Receive Timestamp:    3766505900.881166210 (2019/05/10 
21:38:20)
          Transmit Timestamp:   3766505900.881216619 (2019/05/10 
21:38:20)
            Originator - Receive Timestamp:  +0.009726104
            Originator - Transmit Timestamp: +0.009776513
21:38:28.689154  In de:ad:be:ef:fe:ed ethertype IPv4 (0x0800), length 
92: (tos 0x0, ttl 128, id 6, offset 0, flags [DF], proto UDP (17), 
length 76)
    192.168.2.9.8888 > 192.168.2.10.123: [udp sum ok] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 
(unspecified), poll 6 (64s), precision -20
        Root Delay: 0.000000, Root dispersion: 0.000000, Reference-ID: 
(unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   0.000000000
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 0.000000000

das scheint es aber noch nicht ganz gewesen zu sein....

von Jürgen (Gast)


Lesenswert?

... schrieb:
> Vermutlich kann der TO kein Englisch. Sonst waere er wohl schon
> selber auf die Fehler in seiner Konfiguration gekommen.

es geht nun... muss nur noch was am Adruino ändern da die Zeit nicht die 
EU-ZEIT ist... und brauch auch noch das Datum

von Tippgeber (Gast)


Lesenswert?

restrict 192.168.2.0 mask 255.255.255.0 notrust

===>

restrict 192.168.2.0 mask 255.255.255.0

von Jürgen (Gast)


Lesenswert?

Tippgeber schrieb:
> restrict 192.168.2.0 mask 255.255.255.0

Tippgeber schrieb:
> restrict 192.168.2.0 mask 255.255.255.0

Danke

kann mir jemand erklären wie ich das zu verstehen habe wenn mein 
Zeitserver schon die korrekte EU-Zeit hat, der Arduino den Zeitserver 
abfragt und er dann immer noch keine Sommerzeit ausgibt?

ich vermute das hat was mit der UNIX-zeit zu tun die dann wohl erst vom 
Arduino umgerechnet wird.

gibt es hierfür einen einfache lib die ich dazwischenschalten kann die 
dies übernimmt?

von Tippgeber (Gast)


Lesenswert?

NTP macht immer UTC.
Auch die Systemzeit (RTC) sollte immer UTC sein.
Alles andere führt ins Chaos.

Erst eine gesetzte TZ in Verbindung mit dem System zeigt lokale Zeit.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.