1 | /* |
---|
2 | UDPSendReceiveString: |
---|
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 | }; |
---|
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 | 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 | Serial.print(remote[i], DEC); |
---|
55 | if (i < 3) { |
---|
56 | Serial.print("."); |
---|
57 | } |
---|
58 | } |
---|
59 | Serial.print(", port "); |
---|
60 | Serial.println(Udp.remotePort()); |
---|
61 | |
---|
62 | // read the packet into packetBufffer |
---|
63 | Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); |
---|
64 | Serial.println("Contents:"); |
---|
65 | Serial.println(packetBuffer); |
---|
66 | |
---|
67 | // send a reply to the IP address and port that sent us the packet we received |
---|
68 | Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); |
---|
69 | Udp.write(ReplyBuffer); |
---|
70 | Udp.endPacket(); |
---|
71 | } |
---|
72 | delay(10); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /* |
---|
77 | Processing sketch to run with this example |
---|
78 | ===================================================== |
---|
79 | |
---|
80 | // Processing UDP example to send and receive string data from Arduino |
---|
81 | // press any key to send the "Hello Arduino" message |
---|
82 | |
---|
83 | |
---|
84 | import hypermedia.net.*; |
---|
85 | |
---|
86 | UDP udp; // define the UDP object |
---|
87 | |
---|
88 | |
---|
89 | void setup() { |
---|
90 | udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000 |
---|
91 | //udp.log( true ); // <-- printout the connection activity |
---|
92 | udp.listen( true ); // and wait for incoming message |
---|
93 | } |
---|
94 | |
---|
95 | void draw() |
---|
96 | { |
---|
97 | } |
---|
98 | |
---|
99 | void keyPressed() { |
---|
100 | String ip = "192.168.1.177"; // the remote IP address |
---|
101 | int port = 8888; // the destination port |
---|
102 | |
---|
103 | udp.send("Hello World", ip, port ); // the message to send |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | void receive( byte[] data ) { // <-- default handler |
---|
108 | //void receive( byte[] data, String ip, int port ) { // <-- extended handler |
---|
109 | |
---|
110 | for(int i=0; i < data.length; i++) |
---|
111 | print(char(data[i])); |
---|
112 | println(); |
---|
113 | } |
---|
114 | */ |
---|
115 | |
---|
116 | |
---|