UdpSendReceive.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. WiFiEsp example: WiFi UDP Send and Receive String
  3. This sketch wait an UDP packet on localPort using a WiFi shield.
  4. When a packet is received an 'ACK' packet is sent to the client on port remotePort.
  5. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
  6. */
  7. #include <WiFiEsp.h>
  8. #include <WiFiEspUdp.h>
  9. // Emulate Serial1 on pins 6/7 if not present
  10. #ifndef HAVE_HWSERIAL1
  11. #include "SoftwareSerial.h"
  12. SoftwareSerial Serial1(6, 7); // RX, TX
  13. #endif
  14. char ssid[] = "Twim"; // your network SSID (name)
  15. char pass[] = "12345678"; // your network password
  16. int status = WL_IDLE_STATUS; // the Wifi radio's status
  17. unsigned int localPort = 10002; // local port to listen on
  18. char packetBuffer[255]; // buffer to hold incoming packet
  19. char ReplyBuffer[] = "ACK"; // a string to send back
  20. WiFiEspUDP Udp;
  21. void setup() {
  22. // initialize serial for debugging
  23. Serial.begin(115200);
  24. // initialize serial for ESP module
  25. Serial1.begin(9600);
  26. // initialize ESP module
  27. WiFi.init(&Serial1);
  28. // check for the presence of the shield:
  29. if (WiFi.status() == WL_NO_SHIELD) {
  30. Serial.println("WiFi shield not present");
  31. // don't continue:
  32. while (true);
  33. }
  34. // attempt to connect to WiFi network
  35. while ( status != WL_CONNECTED) {
  36. Serial.print("Attempting to connect to WPA SSID: ");
  37. Serial.println(ssid);
  38. // Connect to WPA/WPA2 network
  39. status = WiFi.begin(ssid, pass);
  40. }
  41. Serial.println("Connected to wifi");
  42. printWifiStatus();
  43. Serial.println("\nStarting connection to server...");
  44. // if you get a connection, report back via serial:
  45. Udp.begin(localPort);
  46. Serial.print("Listening on port ");
  47. Serial.println(localPort);
  48. }
  49. void loop() {
  50. // if there's data available, read a packet
  51. int packetSize = Udp.parsePacket();
  52. if (packetSize) {
  53. Serial.print("Received packet of size ");
  54. Serial.println(packetSize);
  55. Serial.print("From ");
  56. IPAddress remoteIp = Udp.remoteIP();
  57. Serial.print(remoteIp);
  58. Serial.print(", port ");
  59. Serial.println(Udp.remotePort());
  60. // read the packet into packetBufffer
  61. int len = Udp.read(packetBuffer, 255);
  62. if (len > 0) {
  63. packetBuffer[len] = 0;
  64. }
  65. Serial.println("Contents:");
  66. Serial.println(packetBuffer);
  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. }
  73. void printWifiStatus() {
  74. // print the SSID of the network you're attached to:
  75. Serial.print("SSID: ");
  76. Serial.println(WiFi.SSID());
  77. // print your WiFi shield's IP address:
  78. IPAddress ip = WiFi.localIP();
  79. Serial.print("IP Address: ");
  80. Serial.println(ip);
  81. // print the received signal strength:
  82. long rssi = WiFi.RSSI();
  83. Serial.print("signal strength (RSSI):");
  84. Serial.print(rssi);
  85. Serial.println(" dBm");
  86. }