Advertisement
Victoryus82

udp teszt

May 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. // config:
  5.  
  6. const char *ssid = "ESPWifiTeszt";  // You will connect your phone to this Access Point
  7. const char *pw = "nincs"; // and this is the password
  8. IPAddress ip(192, 168, 1, 10); // From RoboRemo app, connect to this IP
  9. IPAddress netmask(255, 255, 255, 0);
  10.  
  11. WiFiUDP UDPTestServer;
  12. unsigned int UDPPort = 4000;
  13. const int packetSize = 8;
  14. byte packetBuffer[packetSize];
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   delay(2000);
  19.   Serial.println();
  20.   WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
  21.   WiFi.softAP(ssid, pw); // configure ssid and password for softAP
  22.  
  23.   Serial.println("Wemos D1R2 UDP teszt");
  24.   Serial.println((String)"SSID: " + ssid + "  PASS: " + pw);
  25.   Serial.println((String)"RoboRemo app must connect to " + ip.toString() + ":" + UDPPort);
  26.  
  27.   UDPTestServer.begin(UDPPort);
  28.  
  29.   pinMode(LED_BUILTIN, OUTPUT);
  30.   digitalWrite(LED_BUILTIN, LOW);
  31.  
  32.  
  33. }
  34.  
  35. void loop() {  
  36.    handleUDPServer();
  37.    delay(1);
  38. }
  39.  
  40. void handleUDPServer() {
  41.   int cb = UDPTestServer.parsePacket();
  42.   if (cb) {
  43.     UDPTestServer.read(packetBuffer, packetSize);
  44.     String myData = "";
  45.     for(int i = 0; i < packetSize; i++) {
  46.       myData += (char)packetBuffer[i];
  47.     }
  48.     Serial.println(myData);
  49.  
  50.     if (myData == "led_on")
  51.       {
  52.          Serial.println("Ha led_on szöveg érkezik...");
  53.         digitalWrite(LED_BUILTIN, HIGH);
  54.         }
  55.      if (myData == "led_off")
  56.       {
  57.         Serial.println("Ha led_off szöveg érkezik...");
  58.         digitalWrite(LED_BUILTIN, LOW);
  59.         }
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement