From addbdd7949cfdd428742632525fd8fa3a8eb8c66 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 17 Sep 2010 14:17:01 +0000 Subject: [PATCH] updating UDP library and examples with M. Margolis' changes and examples. --- libraries/Ethernet/Udp.cpp | 7 + libraries/Ethernet/Udp.h | 3 + .../UDPSendReceiveString.pde | 107 ++++++++++++++++ .../examples/UdpNtpClient/UdpNtpClient.pde | 121 ++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.pde create mode 100644 libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.pde diff --git a/libraries/Ethernet/Udp.cpp b/libraries/Ethernet/Udp.cpp index 0585d6e3d..17eaac69c 100644 --- a/libraries/Ethernet/Udp.cpp +++ b/libraries/Ethernet/Udp.cpp @@ -122,6 +122,13 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t len) { return recvfrom(_sock,buf,len,ip,port); } +int UdpClass::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) { +uint16_t myPort; +uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort); +port = myPort; +return ret; +} + diff --git a/libraries/Ethernet/Udp.h b/libraries/Ethernet/Udp.h index 7e289d8db..5b88d3e06 100644 --- a/libraries/Ethernet/Udp.h +++ b/libraries/Ethernet/Udp.h @@ -53,6 +53,9 @@ public: uint16_t sendPacket(const char[], uint8_t *, uint16_t); //send a string as a packet to specified peer int readPacket(uint8_t *, uint16_t); // read a received packet int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *); // read a received packet, also return sender's ip and port + // readPacket that fills a character string buffer + int readPacket(char *, uint16_t, uint8_t *, uint16_t &); + }; extern UdpClass Udp; diff --git a/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.pde b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.pde new file mode 100644 index 000000000..cfe1b58b5 --- /dev/null +++ b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.pde @@ -0,0 +1,107 @@ +/* + UDPSendReceive.pde: + This sketch receives UDP message strings, prints them to the serial port + and sends an "acknowledge" string back to the sender + + A Processing sketch is included at the end of file that can be used to send + and received messages for testing with a computer. + + created 21 Aug 2010 + by Michael Margolis + + This code is in the public domain. + */ + + +#include // needed for Arduino versions later than 0018 +#include +#include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 + + +// Enter a MAC address and IP address for your controller below. +// The IP address will be dependent on your local network: +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { + 192,168,1,177 }; + +unsigned int localPort = 8888; // local port to listen on + +// the next two variables are set when a packet is received +byte remoteIp[4]; // holds received packet's originating IP +unsigned int remotePort; // holds received packet's originating port + +// buffers for receiving and sending data +char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, +char ReplyBuffer[] = "acknowledged"; // a string to send back + + +void setup() { + // start the Ethernet and UDP: + Ethernet.begin(mac,ip); + Udp.begin(localPort); + + Serial.begin(9600); +} + +void loop() { + // if there's data available, read a packet + int packetSize = Udp.available(); // note that this includes the UDP header + if(packetSize) + { + packetSize = packetSize - 8; // subtract the 8 byte header + Serial.print("Received packet of size "); + Serial.println(packetSize); + + // read the packet into packetBufffer and get the senders IP addr and port number + Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort); + Serial.println("Contents:"); + Serial.println(packetBuffer); + + Udp.sendPacket( ReplyBuffer, remoteIp, remotePort); + } + delay(10); +} + + +/* + Processing sketch to run with this example + ===================================================== + + // Processing UDP example to send and receive string data from Arduino + // press any key to send the "Hello Arduino" message + + + import hypermedia.net.*; + + UDP udp; // define the UDP object + + + void setup() { + udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000 + //udp.log( true ); // <-- printout the connection activity + udp.listen( true ); // and wait for incoming message + } + + void draw() + { + } + + void keyPressed() { + String ip = "192.168.1.177"; // the remote IP address + int port = 8888; // the destination port + + udp.send("Hello World", ip, port ); // the message to send + + } + + void receive( byte[] data ) { // <-- default handler + //void receive( byte[] data, String ip, int port ) { // <-- extended handler + + for(int i=0; i < data.length; i++) + print(char(data[i])); + println(); + } + */ + + diff --git a/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.pde b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.pde new file mode 100644 index 000000000..0b3832b5b --- /dev/null +++ b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.pde @@ -0,0 +1,121 @@ +/* + + 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 17 Sep 2010 + by Tom Igoe + + This code is in the public domain. + + */ + +#include +#include +#include + +// Enter a MAC address and IP address for your controller below. +// The IP address will be dependent on your local network: +byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +byte ip[] = { + 192,168,1,177 }; + + +unsigned int localPort = 8888; // local port to listen for UDP packets + +byte timeServer[] = { + 192, 43, 244, 18}; // time.nist.gov NTP server + +const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message + +byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets + +void setup() +{ + // start Ethernet and UDP + Ethernet.begin(mac,ip); + Udp.begin(localPort); + + Serial.begin(9600); +} + +void loop() +{ + sendNTPpacket(timeServer); // send an NTP packet to a time server + + // wait to see if a reply is available + delay(1000); + if ( Udp.available() ) { + Udp.readPacket(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, esxtract 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(':'); + Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) + Serial.print(':'); + 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(byte *address) +{ + // 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.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123 +} + + + + + + + + + +