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