Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. //SOME CONTSTANS
  5. const char* ssid = "SlinkyBoard";
  6. const char* password = "letsmove";
  7.  
  8. ////////////WIFI UDP PART///////////
  9. WiFiUDP Udp;
  10. unsigned int localUdpPort = 4210;  // local port to listen on
  11. char incomingPacket[255];  // buffer for incoming packets
  12. char  replyPacket[] = "Hi there! Got the message :-)";  // a reply string to send back
  13.  
  14. void replyUDP();
  15. void getUDP();
  16.  
  17.  
  18. void setup()
  19. {
  20.   Serial.begin(115200);
  21.   Serial.println();
  22.  
  23. //Połaczenie z WiFi - niepotrzebne - robimy SoftAP//
  24.  
  25. /*  Serial.printf("Connecting to %s ", ssid);
  26.   WiFi.begin(ssid, password);
  27.   while (WiFi.status() != WL_CONNECTED)
  28.   {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.   Serial.println(" connected");
  33. */
  34.  
  35. ///////////Łączenie z soft AP/////////////////
  36.   Serial.print("Setting soft-AP ... ");
  37.   WiFi.softAP(ssid, password) ? "Ready" : "Failed!";
  38. /////////Rozpoczęcie nasłuchiwania po pakiet UDP///////
  39.   Udp.begin(localUdpPort);
  40.   Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
  41. }
  42.  
  43.  
  44. void loop()
  45. {
  46. char[] dataRecived
  47.  
  48.  
  49. dataRecived[] = getUDP();
  50. replyUDP();
  51. }
  52.  
  53.  
  54. void replyUDP()     // send back a reply, to the IP address and port we got the packet from
  55. {
  56.  
  57.     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  58.     Udp.write(replyPacket);
  59.     Udp.endPacket();
  60. }
  61.  
  62.  
  63. char[] getUDP()
  64. {
  65.     int packetSize = Udp.parsePacket();
  66.   if (packetSize)
  67.   {
  68.     // receive incoming UDP packets
  69.     Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
  70.     int len = Udp.read(incomingPacket, 255);
  71.     if (len > 0)
  72.     {
  73.       incomingPacket[len] = 0;
  74.     }
  75.     Serial.printf("UDP packet contents: %s\n", incomingPacket);
  76.     return incomingPacket;
  77.   }
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement