Advertisement
zockerlein

EmpfangenUDP

Nov 22nd, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. /* Create a WiFi access point and provide a web server on it. */
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiClient.h>
  5. #include <ESP8266WebServer.h>
  6. #include <WiFiUDP.h>
  7.  
  8. /* Set these to your desired credentials. */
  9. const char *ssid = "ESPap";
  10. const char *password = "thereisnospoon";
  11.  
  12. // UDP variables
  13. unsigned int localPort = 8888;
  14. WiFiUDP UDP;
  15. boolean udpConnected = false;
  16. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  17. char ReplyBuffer[] = "acknowledged"; // a string to send back
  18.  
  19. ESP8266WebServer server(80);
  20.  
  21. /* Just a little test message.  Go to http://192.168.4.1 in a web browser
  22.  * connected to this access point to see it.
  23.  */
  24. void handleRoot() {
  25.     server.send(200, "text/html", "<h1>You are connected</h1>");
  26. }
  27.  
  28. void setup() {
  29.     delay(1000);
  30.     Serial.begin(115200);
  31.     Serial.println();
  32.     Serial.print("Configuring access point...");
  33.     /* You can remove the password parameter if you want the AP to be open. */
  34.     WiFi.softAP(ssid, password);
  35.  
  36.     IPAddress myIP = WiFi.softAPIP();
  37.     Serial.print("AP IP address: ");
  38.     Serial.println(myIP);
  39.     server.on("/", handleRoot);
  40.     server.begin();
  41.     Serial.println("HTTP server started");
  42.  
  43.   udpConnected = connectUDP();
  44.   if (udpConnected)
  45.   {
  46.     // initialise pins
  47.     pinMode(D5,OUTPUT);
  48.     pinMode(D0,OUTPUT);
  49.     digitalWrite(D0,HIGH);
  50.   }
  51.  
  52. }
  53.  
  54. void loop()
  55. {
  56.   server.handleClient();
  57.   if(udpConnected)
  58.     {
  59.      
  60.       // if there’s data available, read a packet
  61.       int packetSize = UDP.parsePacket();
  62.       if(packetSize)
  63.         {
  64.           // turn LED on
  65.           digitalWrite(D5,HIGH);
  66.           delay(50);
  67.           digitalWrite(D5,LOW);
  68.         }
  69.      delay(10);
  70.     }
  71. }
  72.  
  73. boolean connectUDP()
  74. {
  75.   boolean state = false;
  76.  
  77.   Serial.println("");
  78.   Serial.println("Connecting to UDP");
  79.  
  80.   if(UDP.begin(localPort) == 1)
  81.   {
  82.     Serial.println("Connection successful");
  83.     state = true;
  84.   }
  85.   else
  86.   {
  87.      Serial.println("Connection failed");
  88.   }
  89.   return state;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement