Skip to content

Commit 95e1af4

Browse files
committed
libraries/WiFi: Add UDP Examples.
Signed-off-by: IFX-Anusha <[email protected]>
1 parent 7d78b6c commit 95e1af4

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
3+
UDP NTP Client
4+
5+
Get the time from a Network Time Protocol (NTP) time server
6+
Demonstrates use of UDP sendPacket and ReceivePacket
7+
For more on NTP time servers and the messages needed to communicate with them,
8+
see http://en.wikipedia.org/wiki/Network_Time_Protocol
9+
10+
created 4 Sep 2010
11+
by Michael Margolis
12+
modified 9 Apr 2012
13+
by Tom Igoe
14+
15+
This code is in the public domain.
16+
17+
modified 2025
18+
by Infineon Technologies AG
19+
*/
20+
21+
#include <WiFi.h>
22+
#include <WiFiUdp.h>
23+
#include "secrets.h"
24+
25+
int status = WL_IDLE_STATUS;
26+
char ssid[] = NET_SECRET_SSID; // your network SSID (name)
27+
char pass[] = NET_SECRET_PASSWORD; // your network password
28+
int keyIndex = 0; // your network key Index number (needed only for WEP)
29+
30+
unsigned int localPort = 2390; // local port to listen for UDP packets
31+
32+
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
33+
34+
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
35+
36+
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
37+
38+
// A UDP instance to let us send and receive packets over UDP
39+
WiFiUDP Udp;
40+
41+
void setup() {
42+
// Open serial communications and wait for port to open:
43+
Serial.begin(9600);
44+
while (!Serial) {
45+
; // wait for serial port to connect. Needed for native USB port only
46+
}
47+
48+
// attempt to connect to WiFi network:
49+
while (status != WL_CONNECTED) {
50+
Serial.print("Attempting to connect to SSID: ");
51+
Serial.println(ssid);
52+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
53+
status = WiFi.begin(ssid, pass);
54+
55+
// wait 10 seconds for connection:
56+
delay(10000);
57+
}
58+
59+
Serial.println("Connected to WiFi");
60+
printWifiStatus();
61+
62+
Serial.println("\nStarting connection to server...");
63+
Udp.begin(localPort);
64+
}
65+
66+
void loop() {
67+
sendNTPpacket(timeServer); // send an NTP packet to a time server
68+
// wait to see if a reply is available
69+
delay(1000);
70+
if (Udp.parsePacket()) {
71+
Serial.println("packet received");
72+
// We've received a packet, read the data from it
73+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
74+
75+
//the timestamp starts at byte 40 of the received packet and is four bytes,
76+
// or two words, long. First, extract the two words:
77+
78+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
79+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
80+
// combine the four bytes (two words) into a long integer
81+
// this is NTP time (seconds since Jan 1 1900):
82+
unsigned long secsSince1900 = highWord << 16 | lowWord;
83+
Serial.print("Seconds since Jan 1 1900 = ");
84+
Serial.println(secsSince1900);
85+
86+
// now convert NTP time into everyday time:
87+
Serial.print("Unix time = ");
88+
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
89+
const unsigned long seventyYears = 2208988800UL;
90+
// subtract seventy years:
91+
unsigned long epoch = secsSince1900 - seventyYears;
92+
// print Unix time:
93+
Serial.println(epoch);
94+
95+
96+
// print the hour, minute and second:
97+
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
98+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
99+
Serial.print(':');
100+
if (((epoch % 3600) / 60) < 10) {
101+
// In the first 10 minutes of each hour, we'll want a leading '0'
102+
Serial.print('0');
103+
}
104+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
105+
Serial.print(':');
106+
if ((epoch % 60) < 10) {
107+
// In the first 10 seconds of each minute, we'll want a leading '0'
108+
Serial.print('0');
109+
}
110+
Serial.println(epoch % 60); // print the second
111+
}
112+
// wait ten seconds before asking for the time again
113+
delay(10000);
114+
}
115+
116+
// send an NTP request to the time server at the given address
117+
void sendNTPpacket(IPAddress& address) {
118+
119+
// set all bytes in the buffer to 0
120+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
121+
// Initialize values needed to form NTP request
122+
// (see URL above for details on the packets)
123+
124+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
125+
packetBuffer[1] = 0; // Stratum, or type of clock
126+
packetBuffer[2] = 6; // Polling Interval
127+
packetBuffer[3] = 0xEC; // Peer Clock Precision
128+
// 8 bytes of zero for Root Delay & Root Dispersion
129+
packetBuffer[12] = 49;
130+
packetBuffer[13] = 0x4E;
131+
packetBuffer[14] = 49;
132+
packetBuffer[15] = 52;
133+
// all NTP fields have been given values, now
134+
// you can send a packet requesting a timestamp:
135+
Udp.beginPacket(address, 123); //NTP requests are to port 123
136+
137+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
138+
139+
Udp.endPacket();
140+
}
141+
142+
143+
void printWifiStatus() {
144+
// print the SSID of the network you're attached to:
145+
Serial.print("SSID: ");
146+
Serial.println(WiFi.SSID());
147+
148+
// print your WiFi local IP address:
149+
IPAddress ip = WiFi.localIP();
150+
Serial.print("IP Address: ");
151+
Serial.println(ip);
152+
153+
// print the received signal strength:
154+
long rssi = WiFi.RSSI();
155+
Serial.print("signal strength (RSSI):");
156+
Serial.print(rssi);
157+
Serial.println(" dBm");
158+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define NET_SECRET_SSID "Anu"
2+
#define NET_SECRET_PASSWORD "anu@12345"
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
WiFiUdpReceiver Example
3+
4+
This sketch wait an UDP packet on localPort
5+
When a packet is received an Acknowledge packet is sent to the client on port remotePort
6+
7+
created 30 December 2012
8+
by dlf (Metodo2 srl)
9+
10+
modified 2025
11+
by Infineon Technologies AG
12+
13+
*/
14+
15+
#include <WiFi.h>
16+
#include <WiFiUdp.h>
17+
#include "secrets.h" // Include your secrets file with SSID and password
18+
19+
int status = WL_IDLE_STATUS;
20+
char ssid[] = NET_SECRET_SSID; // your network SSID (name)
21+
char pass[] = NET_SECRET_PASSWORD; // your network password (use for WPA, or use as key for WEP)
22+
23+
unsigned int localPort = 1234; // local port to listen on
24+
25+
char packetBuffer[255]; //buffer to hold incoming packet
26+
char ReplyBuffer[] = "acknowledged"; // a string to send back
27+
28+
WiFiUDP Udp;
29+
30+
void setup() {
31+
//Initialize serial and wait for port to open:
32+
Serial.begin(9600);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
// attempt to connect to WiFi network:
38+
while (status != WL_CONNECTED) {
39+
Serial.print("Attempting to connect to SSID: ");
40+
Serial.println(ssid);
41+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
42+
status = WiFi.begin(ssid, pass);
43+
44+
// wait 10 seconds for connection:
45+
delay(10000);
46+
}
47+
Serial.println("Connected to WiFi");
48+
printWifiStatus();
49+
50+
Serial.println("\nStarting connection to server...");
51+
// if you get a connection, report back via serial:
52+
Udp.begin(localPort);
53+
}
54+
55+
void loop() {
56+
57+
// if there's data available, read a packet
58+
int packetSize = Udp.parsePacket();
59+
if (packetSize) {
60+
Serial.print("Received packet of size ");
61+
Serial.println(packetSize);
62+
Serial.print("From ");
63+
IPAddress remoteIp = Udp.remoteIP();
64+
Serial.print(remoteIp);
65+
Serial.print(", port ");
66+
Serial.println(Udp.remotePort());
67+
68+
// read the packet into packetBufffer
69+
int len = Udp.read(packetBuffer, 255);
70+
if (len > 0) {
71+
packetBuffer[len] = 0;
72+
}
73+
Serial.println("Contents:");
74+
Serial.println(packetBuffer);
75+
76+
// send a reply, to the IP address and port that sent us the packet we received
77+
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
78+
Udp.write(ReplyBuffer);
79+
Udp.endPacket();
80+
}
81+
}
82+
83+
84+
void printWifiStatus() {
85+
// print the SSID of the network you're attached to:
86+
Serial.print("SSID: ");
87+
Serial.println(WiFi.SSID());
88+
89+
// print your WiFi local IP address:
90+
IPAddress ip = WiFi.localIP();
91+
Serial.print("IP Address: ");
92+
Serial.println(ip);
93+
94+
// print the received signal strength:
95+
long rssi = WiFi.RSSI();
96+
Serial.print("signal strength (RSSI):");
97+
Serial.print(rssi);
98+
Serial.println(" dBm");
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define NET_SECRET_SSID "Anu"
2+
#define NET_SECRET_PASSWORD "anu@12345"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
WiFiUdpSender Example
3+
4+
This sketch sends a UDP packet to a server on remote port and waits for a reply.
5+
This example works together with the WiFiUdpReceiver example.
6+
7+
created 2025
8+
by Infineon Technologies AG
9+
*/
10+
11+
#include <WiFi.h>
12+
#include <WiFiUdp.h>
13+
#include "secrets.h" // Include your secrets file with SSID and password
14+
15+
int status = WL_IDLE_STATUS;
16+
char ssid[] = NET_SECRET_SSID;
17+
char pass[] = NET_SECRET_PASSWORD;
18+
19+
unsigned int localPort = 4321; // Local port to listen for replies
20+
const char* serverIP = " 192.168.38.107"; // Change to your server's IP. You can get it by doing WiFi.localIP() on server side
21+
unsigned int serverPort = 1234; // Server's listening port
22+
23+
char packetBuffer[255]; //buffer to hold incoming packet
24+
25+
WiFiUDP Udp;
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
while (!Serial);
30+
31+
// attempt to connect to WiFi network:
32+
while (status != WL_CONNECTED) {
33+
Serial.print("Attempting to connect to SSID: ");
34+
Serial.println(ssid);
35+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
36+
status = WiFi.begin(ssid, pass);
37+
38+
// wait 10 seconds for connection:
39+
delay(10000);
40+
}
41+
Serial.println("Connected to WiFi");
42+
printWifiStatus();
43+
44+
Udp.begin(localPort);
45+
Serial.println("UDP client started");
46+
}
47+
48+
void loop() {
49+
// Send a packet to the server
50+
const char* msg = "Hello from client!";
51+
Udp.beginPacket(serverIP, serverPort);
52+
Udp.write(msg);
53+
Udp.endPacket();
54+
Serial.println("Packet sent to server");
55+
56+
// Wait for a reply (with timeout)
57+
unsigned long start = millis();
58+
bool gotReply = false;
59+
while (millis() - start < 2000) { // 2 second timeout
60+
int packetSize = Udp.parsePacket();
61+
if (packetSize) {
62+
int len = Udp.read(packetBuffer, 254);
63+
if (len > 0) packetBuffer[len] = 0;
64+
Serial.print("Received reply: ");
65+
Serial.println(packetBuffer);
66+
gotReply = true;
67+
break;
68+
}
69+
delay(10);
70+
}
71+
if (!gotReply) {
72+
Serial.println("No reply received.");
73+
}
74+
75+
delay(5000); // Wait before sending again
76+
}
77+
78+
void printWifiStatus() {
79+
// print the SSID of the network you're attached to:
80+
Serial.print("SSID: ");
81+
Serial.println(WiFi.SSID());
82+
83+
// print your WiFi local IP address:
84+
IPAddress ip = WiFi.localIP();
85+
Serial.print("IP Address: ");
86+
Serial.println(ip);
87+
88+
// print the received signal strength:
89+
long rssi = WiFi.RSSI();
90+
Serial.print("signal strength (RSSI):");
91+
Serial.print(rssi);
92+
Serial.println(" dBm");
93+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define NET_SECRET_SSID "Anu"
2+
#define NET_SECRET_PASSWORD "anu@12345"

0 commit comments

Comments
 (0)