UDPSendReceiveString.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. A Processing sketch is included at the end of file that can be used to send
  6. and received messages for testing with a computer.
  7. created 21 Aug 2010
  8. by Michael Margolis
  9. This code is in the public domain.
  10. */
  11. #include <SPI.h> // needed for Arduino versions later than 0018
  12. #include <Ethernet2.h>
  13. #include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  14. // Enter a MAC address and IP address for your controller below.
  15. // The IP address will be dependent on your local network:
  16. byte mac[] = {
  17. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  18. };
  19. IPAddress ip(192, 168, 1, 177);
  20. unsigned int localPort = 8888; // local port to listen on
  21. // buffers for receiving and sending data
  22. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  23. char ReplyBuffer[] = "acknowledged"; // a string to send back
  24. // An EthernetUDP instance to let us send and receive packets over UDP
  25. EthernetUDP Udp;
  26. void setup() {
  27. // start the Ethernet and UDP:
  28. Ethernet.begin(mac, ip);
  29. Udp.begin(localPort);
  30. Serial.begin(9600);
  31. }
  32. void loop() {
  33. // if there's data available, read a packet
  34. int packetSize = Udp.parsePacket();
  35. if (packetSize)
  36. {
  37. Serial.print("Received packet of size ");
  38. Serial.println(packetSize);
  39. Serial.print("From ");
  40. IPAddress remote = Udp.remoteIP();
  41. for (int i = 0; i < 4; i++)
  42. {
  43. Serial.print(remote[i], DEC);
  44. if (i < 3)
  45. {
  46. Serial.print(".");
  47. }
  48. }
  49. Serial.print(", port ");
  50. Serial.println(Udp.remotePort());
  51. // read the packet into packetBufffer
  52. Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  53. Serial.println("Contents:");
  54. Serial.println(packetBuffer);
  55. // send a reply, to the IP address and port that sent us the packet we received
  56. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  57. Udp.write(ReplyBuffer);
  58. Udp.endPacket();
  59. }
  60. delay(10);
  61. }
  62. /*
  63. Processing sketch to run with this example
  64. =====================================================
  65. // Processing UDP example to send and receive string data from Arduino
  66. // press any key to send the "Hello Arduino" message
  67. import hypermedia.net.*;
  68. UDP udp; // define the UDP object
  69. void setup() {
  70. udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
  71. //udp.log( true ); // <-- printout the connection activity
  72. udp.listen( true ); // and wait for incoming message
  73. }
  74. void draw()
  75. {
  76. }
  77. void keyPressed() {
  78. String ip = "192.168.1.177"; // the remote IP address
  79. int port = 8888; // the destination port
  80. udp.send("Hello World", ip, port ); // the message to send
  81. }
  82. void receive( byte[] data ) { // <-- default handler
  83. //void receive( byte[] data, String ip, int port ) { // <-- extended handler
  84. for(int i=0; i < data.length; i++)
  85. print(char(data[i]));
  86. println();
  87. }
  88. */