How to get NTP server time via WIFI for Arduino Yun

Hi, I'm able to get the NTP server time from the local netwok via WIFI for an ESP 8266 (Node MCU) with code like attached. It is closed to the provided exemple with the IDE.
In the exemple ausgang1 switches on if time is equal to hour =18 and secound = 10 for one hour.
Unfortunately it work not with the Yun.
What makes the difference betweeon ESP and Yun.
Can somebody help?
Thanks - Peter

//#include <ESP8266WiFi.h>
//#include <WiFiUdp.h>
#include <ArduinoOTA.h>


char ssid[] = "FRITZ!Box 7490";  
char pass[] = "1111111111111111";     

int ausgang1 = 1; 

unsigned int localPort = 2390;    

IPAddress timeServer(129, 6, 15, 28); 

const int NTP_PACKET_SIZE = 48; 

byte packetBuffer[ NTP_PACKET_SIZE]; 



void setup()
{
  Serial.begin(115200);

  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  ArduinoOTA.setPassword("admin");
  udp.begin(localPort);

}

void loop()

{

 ArduinoOTA.handle();

  sendNTPpacket(timeServer); 
  delay(1000);

  
  int cb = udp.parsePacket();
  
    udp.read(packetBuffer, NTP_PACKET_SIZE); 

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);

    unsigned long secsSince1900 = highWord << 16 | lowWord;

    const unsigned long seventyYears = 2208988800UL;
    unsigned long epoch = secsSince1900 - seventyYears;
    
    int stund  = (epoch  % 86400L) / 3600 + 1;
    int minut   = (epoch  % 3600) / 60;
    int sekunde = epoch % 60;
    String stunde = String(stund);
    String minutestring = String(minut);
    if(minut < 10)
    {
      minutestring = "0" + minutestring;  
    }


    Serial.println(stunde);
    Serial.println("|");
    Serial.println(minutestring);
    Serial.println("|");
    Serial.println(sekunde);
    Serial.println("|");

if (stund == 18 and sekunde < 10)
{
  digitalWrite(ausgang1, LOW);
  delay (1000);
  digitalWrite(ausgang1, HIGH);
}


  delay(10000);

}
void sendNTPpacket(IPAddress& address)
{
  //Serial.println("sending NTP packet...");
  // 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();

}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.