Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1759edb
feat: basic udp_bridge class implementing Udp
eigen-value Sep 22, 2025
354e503
mod: begin methods establish connection
eigen-value Sep 23, 2025
9fde055
Merge branch 'main' into 14-add-a-class-for-udp-client
eigen-value Sep 30, 2025
e3efe47
mod: rem atomic flags, increased udp buffer size
eigen-value Sep 30, 2025
5d692ce
mod: default buffer size
eigen-value Oct 3, 2025
cd023d5
impr: udp testing
eigen-value Oct 20, 2025
0d87a0c
fix: udp bridge using tcp method
eigen-value Oct 21, 2025
5fc1ec1
examples: udp echo test
eigen-value Oct 21, 2025
a4c8b5c
examples: fix udp_echo .py server. redundant socket
eigen-value Oct 22, 2025
43bf054
examples: independent python udp server
eigen-value Oct 24, 2025
e191122
feat: udp and tcp read_timeout
eigen-value Oct 27, 2025
138de80
fix: echo example using undefined method
eigen-value Oct 27, 2025
1825fee
Merge remote-tracking branch 'origin/main' into 14-add-a-class-for-ud…
eigen-value Oct 31, 2025
cc24225
mod: parsing packet header according to https://github.com/arduino/ar…
eigen-value Nov 6, 2025
d74e7ea
impr: rem repeated reads when temp_buffer has bytes
eigen-value Nov 10, 2025
a19ccc8
mod: UDP read_timeout default to 1 ms
eigen-value Nov 12, 2025
275d355
Added NTP Client example
cmaglie Nov 12, 2025
20f7bea
mod: moving udp examples to tests
eigen-value Nov 12, 2025
d204ab6
Merge branch 'main' into 14-add-a-class-for-udp-client
eigen-value Nov 12, 2025
79f5565
fix: restore hci.h
eigen-value Nov 12, 2025
1037bd9
fix: repeated calls to begin will change the port without signaling t…
eigen-value Nov 13, 2025
feebff6
mod: udp new methods beginPacket endPacket awaitPacket dropPacket
eigen-value Nov 14, 2025
0b36e5f
Added more checks on NTP client example
cmaglie Nov 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions examples/udp_ntp_client/udp_ntp_client.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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 https://en.wikipedia.org/wiki/Network_Time_Protocol

Example freely inspired from the Arduino Ethernet library
https://github.com/arduino-libraries/Ethernet/blob/master/examples/UdpNtpClient/UdpNtpClient.ino

This code is in the public domain.
*/

#include <Arduino_RouterBridge.h>

unsigned int localPort = 8888; // local port to listen for UDP packets
const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

// A UDP instance to let us send and receive packets over UDP
BridgeUDP<4096> Udp(Bridge);

void setup() {
Monitor.begin();
Udp.begin(localPort);
}

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

// wait one second to see if a reply is available
Udp.setTimeout(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
byte packetBuffer[NTP_PACKET_SIZE];
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;
Monitor.print("Seconds since Jan 1 1900 = ");
Monitor.println(secsSince1900);

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

// print the hour, minute and second:
Monitor.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Monitor.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Monitor.print(':');
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Monitor.print('0');
}
Monitor.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Monitor.print(':');
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Monitor.print('0');
}
Monitor.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
void sendNTPpacket(const char * address) {
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets

// 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:
if (Udp.beginPacket(address, 123)) { // NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
} else {
Monitor.println("Failed to send NTP request.");
}
}

Loading
Loading