root / arduino-1.0 / libraries / Ethernet / examples / UDPSendReceiveString / UDPSendReceiveString.ino @ 58d82c77
History | View | Annotate | Download (2.99 KB)
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 <EthernetUdp.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 |
IPAddress ip(192, 168, 1, 177); |
26 |
|
27 |
unsigned int localPort = 8888; // local port to listen on |
28 |
|
29 |
// buffers for receiving and sending data |
30 |
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, |
31 |
char ReplyBuffer[] = "acknowledged"; // a string to send back |
32 |
|
33 |
// An EthernetUDP instance to let us send and receive packets over UDP |
34 |
EthernetUDP Udp; |
35 |
|
36 |
void setup() { |
37 |
// start the Ethernet and UDP: |
38 |
Ethernet.begin(mac,ip); |
39 |
Udp.begin(localPort); |
40 |
|
41 |
Serial.begin(9600); |
42 |
} |
43 |
|
44 |
void loop() { |
45 |
// if there's data available, read a packet |
46 |
int packetSize = Udp.parsePacket(); |
47 |
if(packetSize) |
48 |
{ |
49 |
Serial.print("Received packet of size "); |
50 |
Serial.println(packetSize); |
51 |
Serial.print("From "); |
52 |
IPAddress remote = Udp.remoteIP(); |
53 |
for (int i =0; i < 4; i++) |
54 |
{ |
55 |
Serial.print(remote[i], DEC); |
56 |
if (i < 3) |
57 |
{ |
58 |
Serial.print("."); |
59 |
} |
60 |
} |
61 |
Serial.print(", port "); |
62 |
Serial.println(Udp.remotePort()); |
63 |
|
64 |
// read the packet into packetBufffer |
65 |
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); |
66 |
Serial.println("Contents:"); |
67 |
Serial.println(packetBuffer); |
68 |
|
69 |
// send a reply, to the IP address and port that sent us the packet we received |
70 |
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); |
71 |
Udp.write(ReplyBuffer); |
72 |
Udp.endPacket(); |
73 |
} |
74 |
delay(10); |
75 |
} |
76 |
|
77 |
|
78 |
/* |
79 |
Processing sketch to run with this example |
80 |
===================================================== |
81 |
|
82 |
// Processing UDP example to send and receive string data from Arduino |
83 |
// press any key to send the "Hello Arduino" message |
84 |
|
85 |
|
86 |
import hypermedia.net.*; |
87 |
|
88 |
UDP udp; // define the UDP object |
89 |
|
90 |
|
91 |
void setup() { |
92 |
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000 |
93 |
//udp.log( true ); // <-- printout the connection activity |
94 |
udp.listen( true ); // and wait for incoming message |
95 |
} |
96 |
|
97 |
void draw() |
98 |
{ |
99 |
} |
100 |
|
101 |
void keyPressed() { |
102 |
String ip = "192.168.1.177"; // the remote IP address |
103 |
int port = 8888; // the destination port |
104 |
|
105 |
udp.send("Hello World", ip, port ); // the message to send |
106 |
|
107 |
} |
108 |
|
109 |
void receive( byte[] data ) { // <-- default handler |
110 |
//void receive( byte[] data, String ip, int port ) { // <-- extended handler |
111 |
|
112 |
for(int i=0; i < data.length; i++) |
113 |
print(char(data[i])); |
114 |
println(); |
115 |
} |
116 |
*/ |
117 |
|
118 |
|