Skip to content

Commit cccc0ad

Browse files
committed
updating UDP library and examples with M. Margolis' changes and examples.
1 parent 4249d0f commit cccc0ad

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

Udp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t len) {
122122
return recvfrom(_sock,buf,len,ip,port);
123123
}
124124

125+
int UdpClass::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
126+
uint16_t myPort;
127+
uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort);
128+
port = myPort;
129+
return ret;
130+
}
131+
125132

126133

127134

Udp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class UdpClass {
5353
uint16_t sendPacket(const char[], uint8_t *, uint16_t); //send a string as a packet to specified peer
5454
int readPacket(uint8_t *, uint16_t); // read a received packet
5555
int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *); // read a received packet, also return sender's ip and port
56+
// readPacket that fills a character string buffer
57+
int readPacket(char *, uint16_t, uint8_t *, uint16_t &);
58+
5659
};
5760

5861
extern UdpClass Udp;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
UDPSendReceive.pde:
3+
This sketch receives UDP message strings, prints them to the serial port
4+
and sends an "acknowledge" string back to the sender
5+
6+
A Processing sketch is included at the end of file that can be used to send
7+
and received messages for testing with a computer.
8+
9+
created 21 Aug 2010
10+
by Michael Margolis
11+
12+
This code is in the public domain.
13+
*/
14+
15+
16+
#include <SPI.h> // needed for Arduino versions later than 0018
17+
#include <Ethernet.h>
18+
#include <Udp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
19+
20+
21+
// Enter a MAC address and IP address for your controller below.
22+
// The IP address will be dependent on your local network:
23+
byte mac[] = {
24+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
25+
byte ip[] = {
26+
192,168,1,177 };
27+
28+
unsigned int localPort = 8888; // local port to listen on
29+
30+
// the next two variables are set when a packet is received
31+
byte remoteIp[4]; // holds received packet's originating IP
32+
unsigned int remotePort; // holds received packet's originating port
33+
34+
// buffers for receiving and sending data
35+
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
36+
char ReplyBuffer[] = "acknowledged"; // a string to send back
37+
38+
39+
void setup() {
40+
// start the Ethernet and UDP:
41+
Ethernet.begin(mac,ip);
42+
Udp.begin(localPort);
43+
44+
Serial.begin(9600);
45+
}
46+
47+
void loop() {
48+
// if there's data available, read a packet
49+
int packetSize = Udp.available(); // note that this includes the UDP header
50+
if(packetSize)
51+
{
52+
packetSize = packetSize - 8; // subtract the 8 byte header
53+
Serial.print("Received packet of size ");
54+
Serial.println(packetSize);
55+
56+
// read the packet into packetBufffer and get the senders IP addr and port number
57+
Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
58+
Serial.println("Contents:");
59+
Serial.println(packetBuffer);
60+
61+
Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);
62+
}
63+
delay(10);
64+
}
65+
66+
67+
/*
68+
Processing sketch to run with this example
69+
=====================================================
70+
71+
// Processing UDP example to send and receive string data from Arduino
72+
// press any key to send the "Hello Arduino" message
73+
74+
75+
import hypermedia.net.*;
76+
77+
UDP udp; // define the UDP object
78+
79+
80+
void setup() {
81+
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
82+
//udp.log( true ); // <-- printout the connection activity
83+
udp.listen( true ); // and wait for incoming message
84+
}
85+
86+
void draw()
87+
{
88+
}
89+
90+
void keyPressed() {
91+
String ip = "192.168.1.177"; // the remote IP address
92+
int port = 8888; // the destination port
93+
94+
udp.send("Hello World", ip, port ); // the message to send
95+
96+
}
97+
98+
void receive( byte[] data ) { // <-- default handler
99+
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
100+
101+
for(int i=0; i < data.length; i++)
102+
print(char(data[i]));
103+
println();
104+
}
105+
*/
106+
107+
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 17 Sep 2010
13+
by Tom Igoe
14+
15+
This code is in the public domain.
16+
17+
*/
18+
19+
#include <SPI.h>
20+
#include <Ethernet.h>
21+
#include <Udp.h>
22+
23+
// Enter a MAC address and IP address for your controller below.
24+
// The IP address will be dependent on your local network:
25+
byte mac[] = {
26+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
27+
byte ip[] = {
28+
192,168,1,177 };
29+
30+
31+
unsigned int localPort = 8888; // local port to listen for UDP packets
32+
33+
byte timeServer[] = {
34+
192, 43, 244, 18}; // time.nist.gov NTP server
35+
36+
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
37+
38+
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
39+
40+
void setup()
41+
{
42+
// start Ethernet and UDP
43+
Ethernet.begin(mac,ip);
44+
Udp.begin(localPort);
45+
46+
Serial.begin(9600);
47+
}
48+
49+
void loop()
50+
{
51+
sendNTPpacket(timeServer); // send an NTP packet to a time server
52+
53+
// wait to see if a reply is available
54+
delay(1000);
55+
if ( Udp.available() ) {
56+
Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
57+
58+
//the timestamp starts at byte 40 of the received packet and is four bytes,
59+
// or two words, long. First, esxtract the two words:
60+
61+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
62+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
63+
// combine the four bytes (two words) into a long integer
64+
// this is NTP time (seconds since Jan 1 1900):
65+
unsigned long secsSince1900 = highWord << 16 | lowWord;
66+
Serial.print("Seconds since Jan 1 1900 = " );
67+
Serial.println(secsSince1900);
68+
69+
// now convert NTP time into everyday time:
70+
Serial.print("Unix time = ");
71+
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
72+
const unsigned long seventyYears = 2208988800UL;
73+
// subtract seventy years:
74+
unsigned long epoch = secsSince1900 - seventyYears;
75+
// print Unix time:
76+
Serial.println(epoch);
77+
78+
79+
// print the hour, minute and second:
80+
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
81+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
82+
Serial.print(':');
83+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
84+
Serial.print(':');
85+
Serial.println(epoch %60); // print the second
86+
}
87+
// wait ten seconds before asking for the time again
88+
delay(10000);
89+
}
90+
91+
// send an NTP request to the time server at the given address
92+
unsigned long sendNTPpacket(byte *address)
93+
{
94+
// set all bytes in the buffer to 0
95+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
96+
// Initialize values needed to form NTP request
97+
// (see URL above for details on the packets)
98+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
99+
packetBuffer[1] = 0; // Stratum, or type of clock
100+
packetBuffer[2] = 6; // Polling Interval
101+
packetBuffer[3] = 0xEC; // Peer Clock Precision
102+
// 8 bytes of zero for Root Delay & Root Dispersion
103+
packetBuffer[12] = 49;
104+
packetBuffer[13] = 0x4E;
105+
packetBuffer[14] = 49;
106+
packetBuffer[15] = 52;
107+
108+
// all NTP fields have been given values, now
109+
// you can send a packet requesting a timestamp:
110+
Udp.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
111+
}
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+

0 commit comments

Comments
 (0)