Problem R4 WIFI Zeitabfrage mit UDP und NTP mit WIFI.config

Warum verträgt sich die Zeile WiFi.config(config_IP); (Zeile 73) nicht mit der Zeitabfrage.
(Ist momentan auskommentiert)
Hängt sich bei:
Serial.println("\nStarting connection to server...");
Udp.begin(localPort);
komplett auf. Für Hilfe wäre ich dankbar.
Anbei der komplette Code.
Vielen Dank!

Es betrifft diesen Bereich:

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
//WiFi.config(config_IP); //An der Zeile scheitert Udp.begin
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
//status = WiFi.begin(config_SSID.c_str(), config_PWD.c_str());
status = WiFi.begin(ssid, pass); //funktioniert

// wait 10 seconds for connection:
delay(10000);

}

Serial.println("Connected to WiFi");
printWifiStatus();

Serial.println("\nStarting connection to server...");
Udp.begin(localPort);

/*
  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 May, 4th 2023
  by Daniele Aimo

  This code is in the public domain.

  Find the full UNO R4 WiFi Network documentation here:
  https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-udp-ntp-client
 */


#include <WiFiS3.h>
#include "RTClib.h"

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


int status = WL_IDLE_STATUS;
//#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "XXXXXXXXXXXXXXX";        // your network SSID (name)
char pass[] = "XXXXXXXXXXXXXXX";    // your network password (use for WPA, or use as key for WEP)
IPAddress config_IP = "10.0.0.200";

int keyIndex = 0;            // your network key index number (needed only for WEP)

unsigned int localPort = 2390;      // local port to listen for UDP packets

IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server

const int NTP_PACKET_SIZE = 48; // NTP timestamp 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
WiFiUDP Udp;

void setup() {
  // 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
  }
  
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    **//WiFi.config(config_IP);  //An der Zeile scheitert Udp.begin**
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    //status = WiFi.begin(config_SSID.c_str(), config_PWD.c_str());
    status = WiFi.begin(ssid, pass);  //funktioniert

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println("Connected to WiFi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  Udp.begin(localPort);

}

void loop() {
  readNTP();
  delay(10000);
}

void readNTP() {  //ehemals loop
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a reply is available
  delay(1000);
  if (Udp.parsePacket()) {
    Serial.println("packet received");
    // 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);
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) {
  //Serial.println("1");
  // 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)
  //Serial.println("2");
  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;

  //Serial.println("3");

  // 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
  //Serial.println("4");
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  //Serial.println("5");
  Udp.endPacket();
  //Serial.println("6");
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Was genau heißt das ?
Kommt da eine Fehlermeldung ? Diese bitte posten.
Steigt der Controller aus ? Wie äußert sich das ?
Gibt es irgendwelche weitere Anzeichen, das etwas nicht funktioniert ?

Es passiert rein gar nichts, nach Starting connection to server... passiert rein gar nichts mehr.
auch keine Fehlermeldung. Solange die Zeile mit config auskommentiert ist, funktioniert alles bestens, nur wenn ich eine fixe IP haben will, streikt das Programm. Das ist mir unverständlich.

So richtig schlau werde ich aus deinem Sketch leider auch nicht.
Der ist für mich sehr unübersichtlich.
Du nutzt NTP und eine externe RTC ?
Und wozu brauchst du UDP ?

Hast du aus dem 10er Netz Internetzugang?

Die interne ist zu ungenau!

Um den NTP Server anzuzapfen, der Code ist aus den offiziellen Beispielen vom R4

Jop, ist standard bei A1.

NOCHMALS: es liegt nur an "WIFI.config" mit fixer IP, dass es nicht funktioniert, DAS ist ausgetestet. Was macht diese blöde Zeile, dass ich dann den NTP-Server nicht abrufen kann? Vielen Dank im Voraus wenn das jemand beantworten kann.

Da es ein ESP32 ist auf dem Board, solltest du es mal mit üblichem ESP32 Code probieren. Da braucht es kein UDP und das läuft prima auf einem ESP32.

Liest sich gut an, gibts dazu vlt. wo ein Beispiel. Hab ehrlich geschrieben keine Ahnung, wie ich das angehen könnte.

Ja, ich nutze immer die Beispiele von Fips und dieser Einführung

ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)

Ich verwende const char* ntpServer = "fritz.box";, da meine Fritz!Box auch als NTP-Server fungieren kann und ich keine Zeitlimits beachten muß.

Gerade beim Testen wichtig :wink:

Du hast

Im Beispiel ist es :

WiFi.config(IPAddress(192,48,56,2));

10.0.0.200 ist die lokale Wlan IP, die andere ist der NTP server im Netz, das stimmt alles. Hab keine Fritzbox.

configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

muss ich probieren auf dem R4, schaut super einfach aus, vielen Dank!

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